diff --git a/app/other/apis/tools/gen.go b/app/other/apis/tools/gen.go index 4bd9094d..689aa7f5 100644 --- a/app/other/apis/tools/gen.go +++ b/app/other/apis/tools/gen.go @@ -554,10 +554,11 @@ func renderAll(data any, tpls ...*template.Template) ([][]byte, error) { return out, nil } -// writeGenerated writes one generated file, creating its directory first. -// It stands in for pkg.FileCreate, which returns no error at all and, when -// the file cannot be created, closes a nil file and ends the process with -// log.Fatalln - one unwritable path took the whole server down with it. +// writeGenerated writes one generated file, creating its directory first, +// which pkg.FileCreate does not do. It replaced pkg.FileCreate here while +// that returned no error and, when the file could not be created, ended the +// process with log.Fatalln - one unwritable path took the whole server down +// with it. go-admin-core v2.11.0 made pkg.FileCreate return the error. func writeGenerated(path string, content []byte) error { if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { return err diff --git a/app/other/apis/tools/gen_errors_test.go b/app/other/apis/tools/gen_errors_test.go index 4df190f8..eb26653c 100644 --- a/app/other/apis/tools/gen_errors_test.go +++ b/app/other/apis/tools/gen_errors_test.go @@ -134,9 +134,9 @@ func TestNOActionsGenWritesNothingWhenATemplateFails(t *testing.T) { } } -// pkg.FileCreate, which this used to write through, ends the process with -// log.Fatalln when it cannot create the file. Reaching the assertions at all -// is half of this test. +// pkg.FileCreate, which this used to write through, ended the process with +// log.Fatalln when it could not create the file, until go-admin-core v2.11.0. +// Reaching the assertions at all is half of this test. func TestNOActionsGenReportsAFileItCannotWrite(t *testing.T) { dir := genWorkspace(t) blocker := filepath.Join(dir, "app", "admin", "models") diff --git a/cmd/app/server.go b/cmd/app/server.go index 7ff8f6e9..59284fb0 100644 --- a/cmd/app/server.go +++ b/cmd/app/server.go @@ -7,6 +7,7 @@ import ( "github.com/go-admin-team/go-admin-core/v2/sdk/pkg" "github.com/go-admin-team/go-admin-core/v2/sdk/pkg/utils" "github.com/spf13/cobra" + "os" "text/template" ) @@ -33,7 +34,10 @@ func run() { //1. 读取配置 fmt.Println(`generate migration file`) - _ = genFile() + if err := genFile(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } @@ -80,11 +84,19 @@ func genFile() error { m := map[string]string{} m["appName"] = appName var b1 bytes.Buffer - err = t1.Execute(&b1, m) - pkg.FileCreate(b1, "./cmd/api/"+appName+".go") + if err = t1.Execute(&b1, m); err != nil { + return err + } t2, err := template.ParseFiles("template/router.template") + if err != nil { + return err + } var b2 bytes.Buffer - err = t2.Execute(&b2, nil) - pkg.FileCreate(b2, appPath+"/router/router.go") - return nil + if err = t2.Execute(&b2, nil); err != nil { + return err + } + if err = pkg.FileCreate(b1, "./cmd/api/"+appName+".go"); err != nil { + return err + } + return pkg.FileCreate(b2, appPath+"/router/router.go") } diff --git a/cmd/migrate/genfile_test.go b/cmd/migrate/genfile_test.go new file mode 100644 index 00000000..9ecac71e --- /dev/null +++ b/cmd/migrate/genfile_test.go @@ -0,0 +1,54 @@ +package migrate + +import ( + "os" + "path/filepath" + "testing" +) + +// chdirWithTemplates runs the test from an empty directory that has the +// repository's template/ directory in it, which is what genFile reads from +// the working directory. +func chdirWithTemplates(t *testing.T) string { + t.Helper() + tmpl, err := filepath.Abs(filepath.Join("..", "..", "template")) + if err != nil { + t.Fatal(err) + } + dir := t.TempDir() + if err := os.Symlink(tmpl, filepath.Join(dir, "template")); err != nil { + t.Fatal(err) + } + t.Chdir(dir) + return dir +} + +func TestGenFileWritesTheMigration(t *testing.T) { + dir := chdirWithTemplates(t) + out := filepath.Join(dir, "cmd", "migrate", "migration", "version-local") + if err := os.MkdirAll(out, 0o755); err != nil { + t.Fatal(err) + } + + if err := genFile(); err != nil { + t.Fatalf("genFile: %v", err) + } + files, err := filepath.Glob(filepath.Join(out, "*_migrate.go")) + if err != nil { + t.Fatal(err) + } + if len(files) != 1 { + t.Fatalf("generated files = %v, want one", files) + } +} + +// Run from a directory without cmd/migrate/migration, writing the file fails. +// The failure used to end the process from inside pkg.FileCreate and, before +// that, was dropped by genFile's caller; it now reaches the caller. +func TestGenFileReportsAFileItCannotWrite(t *testing.T) { + chdirWithTemplates(t) + + if err := genFile(); err == nil { + t.Fatal("genFile returned nil with no directory to write into") + } +} diff --git a/cmd/migrate/server.go b/cmd/migrate/server.go index a4e60858..521a6b1f 100644 --- a/cmd/migrate/server.go +++ b/cmd/migrate/server.go @@ -106,7 +106,7 @@ func run() { ) } else { fmt.Println(`generate migration file`) - _ = genFile() + exitOnError(os.Stderr, genFile()) } } @@ -360,11 +360,11 @@ func genFile() error { m["Package"] = "version" } var b1 bytes.Buffer - err = t1.Execute(&b1, m) - if goAdmin { - pkg.FileCreate(b1, "./cmd/migrate/migration/version/"+m["GenerateTime"]+"_migrate.go") - } else { - pkg.FileCreate(b1, "./cmd/migrate/migration/version-local/"+m["GenerateTime"]+"_migrate.go") + if err = t1.Execute(&b1, m); err != nil { + return err } - return nil + if goAdmin { + return pkg.FileCreate(b1, "./cmd/migrate/migration/version/"+m["GenerateTime"]+"_migrate.go") + } + return pkg.FileCreate(b1, "./cmd/migrate/migration/version-local/"+m["GenerateTime"]+"_migrate.go") } diff --git a/example/app-order/go.mod b/example/app-order/go.mod index f91a095f..942bcfeb 100644 --- a/example/app-order/go.mod +++ b/example/app-order/go.mod @@ -5,7 +5,7 @@ go 1.27.1 require ( github.com/gin-gonic/gin v1.12.0 github.com/glebarez/sqlite v1.11.0 - github.com/go-admin-team/go-admin-core/v2 v2.10.0 + github.com/go-admin-team/go-admin-core/v2 v2.11.0 gorm.io/gorm v1.31.2 ) diff --git a/example/app-order/go.sum b/example/app-order/go.sum index 3a366855..6ef142a9 100644 --- a/example/app-order/go.sum +++ b/example/app-order/go.sum @@ -58,8 +58,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= -github.com/go-admin-team/go-admin-core/v2 v2.10.0 h1:MM1wl9s2iW3M4GFsT8SyaUWaj2Q8m1NSuundXc6DWzI= -github.com/go-admin-team/go-admin-core/v2 v2.10.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= +github.com/go-admin-team/go-admin-core/v2 v2.11.0 h1:kAiGaQWPHHiEnoa+UeKQqTmIQAptDPjEbdu6s4jBZ7A= +github.com/go-admin-team/go-admin-core/v2 v2.11.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= diff --git a/go.mod b/go.mod index 07597d3f..af3ac349 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/casbin/casbin/v3 v3.8.1 github.com/gin-gonic/gin v1.12.0 github.com/glebarez/sqlite v1.11.0 - github.com/go-admin-team/go-admin-core/v2 v2.10.0 + github.com/go-admin-team/go-admin-core/v2 v2.11.0 github.com/google/uuid v1.6.0 github.com/huaweicloud/huaweicloud-sdk-go-obs v3.26.6+incompatible github.com/mssola/user_agent v0.6.0 diff --git a/go.sum b/go.sum index a5d866c5..9f61ddb6 100644 --- a/go.sum +++ b/go.sum @@ -145,8 +145,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= -github.com/go-admin-team/go-admin-core/v2 v2.10.0 h1:MM1wl9s2iW3M4GFsT8SyaUWaj2Q8m1NSuundXc6DWzI= -github.com/go-admin-team/go-admin-core/v2 v2.10.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= +github.com/go-admin-team/go-admin-core/v2 v2.11.0 h1:kAiGaQWPHHiEnoa+UeKQqTmIQAptDPjEbdu6s4jBZ7A= +github.com/go-admin-team/go-admin-core/v2 v2.11.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o= diff --git a/test/e2e-apporder/go.mod b/test/e2e-apporder/go.mod index 270626c9..f71ce9aa 100644 --- a/test/e2e-apporder/go.mod +++ b/test/e2e-apporder/go.mod @@ -40,7 +40,7 @@ require ( github.com/gin-contrib/sse v1.1.1 // indirect github.com/gin-gonic/gin v1.12.0 // indirect github.com/glebarez/sqlite v1.11.0 // indirect - github.com/go-admin-team/go-admin-core/v2 v2.10.0 // indirect + github.com/go-admin-team/go-admin-core/v2 v2.11.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-openapi/jsonpointer v1.0.0 // indirect github.com/go-openapi/jsonreference v1.0.0 // indirect diff --git a/test/e2e-apporder/go.sum b/test/e2e-apporder/go.sum index a5d866c5..9f61ddb6 100644 --- a/test/e2e-apporder/go.sum +++ b/test/e2e-apporder/go.sum @@ -145,8 +145,8 @@ github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= -github.com/go-admin-team/go-admin-core/v2 v2.10.0 h1:MM1wl9s2iW3M4GFsT8SyaUWaj2Q8m1NSuundXc6DWzI= -github.com/go-admin-team/go-admin-core/v2 v2.10.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= +github.com/go-admin-team/go-admin-core/v2 v2.11.0 h1:kAiGaQWPHHiEnoa+UeKQqTmIQAptDPjEbdu6s4jBZ7A= +github.com/go-admin-team/go-admin-core/v2 v2.11.0/go.mod h1:Q0FfO+8pfPNkPqk9fcRpe2sSOHfir82cjZO8Nol/8SI= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o=