refactor: migrate 'flowutils' to 'must' helpers
This commit is contained in:
@@ -60,8 +60,6 @@ linters:
|
|||||||
- -ST1000 # Re-enable this once we have docstrings
|
- -ST1000 # Re-enable this once we have docstrings
|
||||||
- -ST1003 # I like snake_case
|
- -ST1003 # I like snake_case
|
||||||
- -ST1013 # HTTP status codes are shorter and more readable than names
|
- -ST1013 # HTTP status codes are shorter and more readable than names
|
||||||
dot-import-whitelist:
|
|
||||||
- "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
|
||||||
exclusions:
|
exclusions:
|
||||||
generated: lax # Don't lint generated files
|
generated: lax # Don't lint generated files
|
||||||
paths:
|
paths:
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/codegen/modelgenerate"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/codegen/modelgenerate"
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,8 +23,8 @@ var generate_model = &cobra.Command{
|
|||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
path := Must(cmd.Flags().GetString("schema"))
|
path := must.Get(cmd.Flags().GetString("schema"))
|
||||||
modname := Must(cmd.Flags().GetString("modname"))
|
modname := must.Get(cmd.Flags().GetString("modname"))
|
||||||
sql, err := os.ReadFile(path)
|
sql, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("reading path %s: %w", path, err)
|
return fmt.Errorf("reading path %s: %w", path, err)
|
||||||
@@ -36,9 +36,9 @@ var generate_model = &cobra.Command{
|
|||||||
return ErrNoSuchTable
|
return ErrNoSuchTable
|
||||||
}
|
}
|
||||||
|
|
||||||
if Must(cmd.Flags().GetBool("test")) {
|
if must.Get(cmd.Flags().GetBool("test")) {
|
||||||
file2 := modelgenerate.GenerateModelTestAST(table, schema, modname)
|
file2 := modelgenerate.GenerateModelTestAST(table, schema, modname)
|
||||||
PanicIf(modelgenerate.FprintWithComments(os.Stdout, file2))
|
must.Do(modelgenerate.FprintWithComments(os.Stdout, file2))
|
||||||
} else {
|
} else {
|
||||||
decls := []ast.Decl{
|
decls := []ast.Decl{
|
||||||
&ast.GenDecl{
|
&ast.GenDecl{
|
||||||
@@ -94,7 +94,7 @@ var generate_model = &cobra.Command{
|
|||||||
Decls: decls,
|
Decls: decls,
|
||||||
}
|
}
|
||||||
|
|
||||||
PanicIf(modelgenerate.FprintWithComments(os.Stdout, file))
|
must.Do(modelgenerate.FprintWithComments(os.Stdout, file))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/codegen"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/codegen"
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd_init = &cobra.Command{
|
var cmd_init = &cobra.Command{
|
||||||
@@ -23,11 +23,11 @@ var cmd_init = &cobra.Command{
|
|||||||
var target string
|
var target string
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
target = args[0]
|
target = args[0]
|
||||||
PanicIf(os.MkdirAll(target, 0o755))
|
must.Do(os.MkdirAll(target, 0o755))
|
||||||
PanicIf(os.Chdir(target))
|
must.Do(os.Chdir(target))
|
||||||
} else {
|
} else {
|
||||||
// Default to current directory (".")
|
// Default to current directory (".")
|
||||||
target = Must(os.Getwd())
|
target = must.Get(os.Getwd())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all the config values
|
// Get all the config values
|
||||||
@@ -41,9 +41,9 @@ var cmd_init = &cobra.Command{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pkg_opts := codegen.PkgOpts{
|
pkg_opts := codegen.PkgOpts{
|
||||||
ModuleName: Must(cmd.Flags().GetString("module")),
|
ModuleName: must.Get(cmd.Flags().GetString("module")),
|
||||||
DBFilename: Must(cmd.Flags().GetString("db")),
|
DBFilename: must.Get(cmd.Flags().GetString("db")),
|
||||||
BinaryName: Must(cmd.Flags().GetString("binary")),
|
BinaryName: must.Get(cmd.Flags().GetString("binary")),
|
||||||
}
|
}
|
||||||
if pkg_opts.ModuleName == "" {
|
if pkg_opts.ModuleName == "" {
|
||||||
pkg_opts.ModuleName = filepath.Base(target)
|
pkg_opts.ModuleName = filepath.Base(target)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed "tpl"
|
//go:embed "tpl"
|
||||||
@@ -22,31 +22,31 @@ type PkgOpts struct {
|
|||||||
func InitPkg(opts PkgOpts) {
|
func InitPkg(opts PkgOpts) {
|
||||||
// Run `go mod init`
|
// Run `go mod init`
|
||||||
fmt.Printf("Running... `go mod init %s`\n", opts.ModuleName)
|
fmt.Printf("Running... `go mod init %s`\n", opts.ModuleName)
|
||||||
PanicIf(exec.Command("go", "mod", "init", opts.ModuleName).Run())
|
must.Do(exec.Command("go", "mod", "init", opts.ModuleName).Run())
|
||||||
|
|
||||||
// Run `git init`, if required
|
// Run `git init`, if required
|
||||||
if exec.Command("git", "status").Run() != nil {
|
if exec.Command("git", "status").Run() != nil {
|
||||||
// Not in a git repo yet; init one
|
// Not in a git repo yet; init one
|
||||||
fmt.Println("Running... `git init`")
|
fmt.Println("Running... `git init`")
|
||||||
PanicIf(exec.Command("git", "init").Run())
|
must.Do(exec.Command("git", "init").Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create package structure
|
// Create package structure
|
||||||
PanicIf(os.MkdirAll("pkg/db", 0o755))
|
must.Do(os.MkdirAll("pkg/db", 0o755))
|
||||||
PanicIf(os.MkdirAll("cmd", 0o755))
|
must.Do(os.MkdirAll("cmd", 0o755))
|
||||||
PanicIf(os.MkdirAll("doc", 0o755))
|
must.Do(os.MkdirAll("doc", 0o755))
|
||||||
PanicIf(os.MkdirAll("sample_data", 0o755))
|
must.Do(os.MkdirAll("sample_data", 0o755))
|
||||||
|
|
||||||
PanicIf(os.WriteFile("pkg/db/schema.sql", Must(tpl.ReadFile("tpl/schema.sql")), 0o664))
|
must.Do(os.WriteFile("pkg/db/schema.sql", must.Get(tpl.ReadFile("tpl/schema.sql")), 0o664))
|
||||||
PanicIf(os.WriteFile("pkg/db/db.go", Must(tpl.ReadFile("tpl/db.go.tpl")), 0o664))
|
must.Do(os.WriteFile("pkg/db/db.go", must.Get(tpl.ReadFile("tpl/db.go.tpl")), 0o664))
|
||||||
|
|
||||||
dbTest := Must(os.Create("pkg/db/db_test.go"))
|
dbTest := must.Get(os.Create("pkg/db/db_test.go"))
|
||||||
defer MustClose(dbTest)
|
defer must.Close(dbTest)
|
||||||
t := Must(template.ParseFS(tpl, "tpl/db_test.go.tpl"))
|
t := must.Get(template.ParseFS(tpl, "tpl/db_test.go.tpl"))
|
||||||
PanicIf(t.Execute(dbTest, opts))
|
must.Do(t.Execute(dbTest, opts))
|
||||||
|
|
||||||
PanicIf(os.WriteFile("sample_data/mount.sh", Must(tpl.ReadFile("tpl/mount.sh")), 0o775))
|
must.Do(os.WriteFile("sample_data/mount.sh", must.Get(tpl.ReadFile("tpl/mount.sh")), 0o775))
|
||||||
PanicIf(os.WriteFile("sample_data/reset.sh", Must(tpl.ReadFile("tpl/reset.sh")), 0o775))
|
must.Do(os.WriteFile("sample_data/reset.sh", must.Get(tpl.ReadFile("tpl/reset.sh")), 0o775))
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - create `pkg/db/errors.go`
|
// - create `pkg/db/errors.go`
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package flowutils
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
func PanicIf(err error) {
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Must[T any](val T, err error) T {
|
|
||||||
PanicIf(err)
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
func MustClose(closer io.Closer) {
|
|
||||||
PanicIf(closer.Close())
|
|
||||||
}
|
|
||||||
27
pkg/must/must.go
Normal file
27
pkg/must/must.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package must
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Do(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Get[T any](val T, err error) T {
|
||||||
|
Do(err)
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func Be(condition bool, msg string, args ...any) {
|
||||||
|
if !condition {
|
||||||
|
panic(fmt.Errorf(msg, args...)) //nolint:err113 // not a returned error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Close(closer io.Closer) {
|
||||||
|
Do(closer.Close())
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/db"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ func TestVerifyCorrectMigration(t *testing.T) {
|
|||||||
alter table t1 add column data3 integer;
|
alter table t1 add column data3 integer;
|
||||||
`
|
`
|
||||||
db2Config := db.Init(&baseSchema, &[]string{migration})
|
db2Config := db.Init(&baseSchema, &[]string{migration})
|
||||||
db2 := flowutils.Must(db2Config.Create(":memory:"))
|
db2 := must.Get(db2Config.Create(":memory:"))
|
||||||
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
||||||
db2Schema := schema.SchemaFromDB(db2)
|
db2Schema := schema.SchemaFromDB(db2)
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ func TestVerifyCorrectMigration(t *testing.T) {
|
|||||||
`
|
`
|
||||||
|
|
||||||
db2Config := db.Init(&baseSchema, &[]string{migration1, migration2})
|
db2Config := db.Init(&baseSchema, &[]string{migration1, migration2})
|
||||||
db2 := flowutils.Must(db2Config.Create(":memory:"))
|
db2 := must.Get(db2Config.Create(":memory:"))
|
||||||
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
||||||
db2Schema := schema.SchemaFromDB(db2)
|
db2Schema := schema.SchemaFromDB(db2)
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ func TestIncorrectMigrations(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("missing migration", func(t *testing.T) {
|
t.Run("missing migration", func(t *testing.T) {
|
||||||
db2Config := db.Init(&baseSchema, &[]string{})
|
db2Config := db.Init(&baseSchema, &[]string{})
|
||||||
db2 := flowutils.Must(db2Config.Create(":memory:"))
|
db2 := must.Get(db2Config.Create(":memory:"))
|
||||||
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
||||||
db2Schema := schema.SchemaFromDB(db2)
|
db2Schema := schema.SchemaFromDB(db2)
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ func TestIncorrectMigrations(t *testing.T) {
|
|||||||
rowid integer primary key
|
rowid integer primary key
|
||||||
);
|
);
|
||||||
`})
|
`})
|
||||||
db2 := flowutils.Must(db2Config.Create(":memory:"))
|
db2 := must.Get(db2Config.Create(":memory:"))
|
||||||
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
||||||
db2Schema := schema.SchemaFromDB(db2)
|
db2Schema := schema.SchemaFromDB(db2)
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ func TestIncorrectMigrations(t *testing.T) {
|
|||||||
);
|
);
|
||||||
alter table t1 add column data3 text;
|
alter table t1 add column data3 text;
|
||||||
`})
|
`})
|
||||||
db2 := flowutils.Must(db2Config.Create(":memory:"))
|
db2 := must.Get(db2Config.Create(":memory:"))
|
||||||
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
require.NoError(t, db2Config.CheckAndUpdateVersion(db2))
|
||||||
db2Schema := schema.SchemaFromDB(db2)
|
db2Schema := schema.SchemaFromDB(db2)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
. "git.offline-twitter.com/offline-labs/gas-stack/pkg/flowutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/textutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,20 +38,20 @@ func SchemaFromDB(db *sqlx.DB) Schema {
|
|||||||
db.MustExec(create_views)
|
db.MustExec(create_views)
|
||||||
|
|
||||||
var tables []Table
|
var tables []Table
|
||||||
PanicIf(db.Select(&tables, `select name, table_type, is_strict, is_without_rowid from tables`))
|
must.Do(db.Select(&tables, `select name, table_type, is_strict, is_without_rowid from tables`))
|
||||||
for _, tbl := range tables {
|
for _, tbl := range tables {
|
||||||
tbl.GoTypeName = TypenameFromTablename(tbl.TableName)
|
tbl.GoTypeName = TypenameFromTablename(tbl.TableName)
|
||||||
tbl.TypeIDName = tbl.GoTypeName + "ID"
|
tbl.TypeIDName = tbl.GoTypeName + "ID"
|
||||||
tbl.VarName = strings.ToLower(string(tbl.TableName[0]))
|
tbl.VarName = strings.ToLower(string(tbl.TableName[0]))
|
||||||
|
|
||||||
PanicIf(db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName))
|
must.Do(db.Select(&tbl.Columns, `select * from columns where table_name = ?`, tbl.TableName))
|
||||||
ret.Tables[tbl.TableName] = tbl
|
ret.Tables[tbl.TableName] = tbl
|
||||||
}
|
}
|
||||||
|
|
||||||
var indexes []Index
|
var indexes []Index
|
||||||
PanicIf(db.Select(&indexes, `select index_name, table_name, is_unique from indexes`))
|
must.Do(db.Select(&indexes, `select index_name, table_name, is_unique from indexes`))
|
||||||
for _, idx := range indexes {
|
for _, idx := range indexes {
|
||||||
PanicIf(db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name))
|
must.Do(db.Select(&idx.Columns, `select column_name from index_columns where index_name = ? order by rank`, idx.Name))
|
||||||
ret.Indexes[idx.Name] = idx
|
ret.Indexes[idx.Name] = idx
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
Reference in New Issue
Block a user