From 89ce5e492b7a6659329e04c2622aba8593d93500 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Mon, 3 Jan 2022 21:32:40 +0800 Subject: [PATCH] refactor file|path (#1409) Co-authored-by: anqiansong --- tools/goctl/api/apigen/gen.go | 9 ++++---- tools/goctl/api/apigen/template.go | 10 ++++----- tools/goctl/api/docgen/gen.go | 4 ++-- tools/goctl/api/format/format.go | 2 +- tools/goctl/api/gogen/gen.go | 5 +++-- tools/goctl/api/gogen/genhandlers.go | 7 +++--- tools/goctl/api/gogen/genlogic.go | 2 +- tools/goctl/api/gogen/genmain.go | 2 +- tools/goctl/api/gogen/genroutes.go | 6 ++--- tools/goctl/api/gogen/gensvc.go | 2 +- tools/goctl/api/gogen/template.go | 10 ++++----- tools/goctl/api/gogen/template_test.go | 22 +++++++++---------- tools/goctl/api/gogen/util.go | 2 +- tools/goctl/api/javagen/gen.go | 4 ++-- tools/goctl/api/javagen/gencomponents.go | 5 +++-- tools/goctl/api/javagen/util.go | 3 ++- tools/goctl/api/new/newservice.go | 7 +++--- tools/goctl/api/new/template.go | 10 ++++----- .../api/parser/g4/test/apiparser_test.go | 16 +++++++------- tools/goctl/api/tsgen/gen.go | 4 ++-- tools/goctl/api/tsgen/gencomponents.go | 4 ++-- tools/goctl/api/tsgen/genpacket.go | 7 +++--- tools/goctl/api/util/util.go | 8 +++---- tools/goctl/docker/docker.go | 17 +++++++------- tools/goctl/docker/template.go | 8 +++---- tools/goctl/kube/kube.go | 15 +++++++------ tools/goctl/model/mongo/generate/generate.go | 5 +++-- .../model/mongo/generate/generate_test.go | 4 ++-- tools/goctl/model/mongo/generate/template.go | 10 ++++----- tools/goctl/model/mongo/mongo.go | 3 ++- tools/goctl/model/sql/command/command.go | 7 +++--- tools/goctl/model/sql/command/command_test.go | 14 ++++++------ tools/goctl/model/sql/gen/delete.go | 5 +++-- tools/goctl/model/sql/gen/field.go | 3 ++- tools/goctl/model/sql/gen/findone.go | 5 +++-- tools/goctl/model/sql/gen/findonebyfield.go | 15 +++++++------ tools/goctl/model/sql/gen/gen.go | 13 ++++++----- tools/goctl/model/sql/gen/gen_test.go | 8 +++---- tools/goctl/model/sql/gen/imports.go | 5 +++-- tools/goctl/model/sql/gen/insert.go | 5 +++-- tools/goctl/model/sql/gen/new.go | 3 ++- tools/goctl/model/sql/gen/tag.go | 3 ++- tools/goctl/model/sql/gen/template.go | 10 ++++----- tools/goctl/model/sql/gen/template_test.go | 22 +++++++++---------- tools/goctl/model/sql/gen/types.go | 3 ++- tools/goctl/model/sql/gen/update.go | 5 +++-- tools/goctl/model/sql/gen/vars.go | 3 ++- tools/goctl/model/sql/parser/parser_test.go | 2 +- tools/goctl/plugin/plugin.go | 4 ++-- tools/goctl/rpc/cli/cli.go | 7 +++--- tools/goctl/rpc/execx/execx.go | 6 ++--- tools/goctl/rpc/generator/gen.go | 4 ++-- tools/goctl/rpc/generator/gen_test.go | 4 ++-- tools/goctl/rpc/generator/gencall.go | 13 ++++++----- tools/goctl/rpc/generator/genconfig.go | 6 ++--- tools/goctl/rpc/generator/genetc.go | 3 ++- tools/goctl/rpc/generator/genlogic.go | 9 ++++---- tools/goctl/rpc/generator/genmain.go | 5 +++-- tools/goctl/rpc/generator/genserver.go | 9 ++++---- tools/goctl/rpc/generator/gensvc.go | 3 ++- tools/goctl/rpc/generator/mkdir.go | 4 ++-- tools/goctl/rpc/generator/prototmpl.go | 5 +++-- tools/goctl/rpc/generator/prototmpl_test.go | 6 ++--- tools/goctl/rpc/generator/template.go | 10 ++++----- tools/goctl/rpc/generator/template_test.go | 8 +++---- tools/goctl/tpl/templates.go | 12 +++++----- tools/goctl/update/update.go | 4 ++-- tools/goctl/util/ctx/gomod.go | 6 ++--- tools/goctl/util/ctx/gomod_test.go | 4 ++-- tools/goctl/util/ctx/gopath.go | 8 +++---- tools/goctl/util/ctx/gopath_test.go | 6 ++--- tools/goctl/util/ctx/modcheck_test.go | 6 ++--- tools/goctl/util/env/env_test.go | 10 ++++----- tools/goctl/util/git.go | 3 ++- tools/goctl/util/{ => pathx}/file.go | 2 +- tools/goctl/util/{ => pathx}/file_test.go | 2 +- tools/goctl/util/{ => pathx}/path.go | 2 +- tools/goctl/util/{ => pathx}/path_test.go | 2 +- .../util/{ => pathx}/readlink+polyfill.go | 2 +- tools/goctl/util/{ => pathx}/readlink.go | 2 +- tools/goctl/util/templatex.go | 3 ++- 81 files changed, 279 insertions(+), 245 deletions(-) rename tools/goctl/util/{ => pathx}/file.go (99%) rename tools/goctl/util/{ => pathx}/file_test.go (99%) rename tools/goctl/util/{ => pathx}/path.go (99%) rename tools/goctl/util/{ => pathx}/path_test.go (97%) rename tools/goctl/util/{ => pathx}/readlink+polyfill.go (88%) rename tools/goctl/util/{ => pathx}/readlink.go (98%) diff --git a/tools/goctl/api/apigen/gen.go b/tools/goctl/api/apigen/gen.go index fcf352c4..8fe56143 100644 --- a/tools/goctl/api/apigen/gen.go +++ b/tools/goctl/api/apigen/gen.go @@ -9,6 +9,7 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -46,7 +47,7 @@ func ApiCommand(c *cli.Context) error { return errors.New("missing -o") } - fp, err := util.CreateIfNotExist(apiFile) + fp, err := pathx.CreateIfNotExist(apiFile) if err != nil { return err } @@ -62,15 +63,15 @@ func ApiCommand(c *cli.Context) error { } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } - text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate) + text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate) if err != nil { return err } - baseName := util.FileNameWithoutExt(filepath.Base(apiFile)) + baseName := pathx.FileNameWithoutExt(filepath.Base(apiFile)) if strings.HasSuffix(strings.ToLower(baseName), "-api") { baseName = baseName[:len(baseName)-4] } else if strings.HasSuffix(strings.ToLower(baseName), "api") { diff --git a/tools/goctl/api/apigen/template.go b/tools/goctl/api/apigen/template.go index e2c88685..11629696 100644 --- a/tools/goctl/api/apigen/template.go +++ b/tools/goctl/api/apigen/template.go @@ -3,7 +3,7 @@ package apigen import ( "fmt" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -23,12 +23,12 @@ func Category() string { // Clean cleans the generated deployment files. func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates generates api template files. func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate reverts the given template file to the default value. @@ -37,7 +37,7 @@ func RevertTemplate(name string) error { if !ok { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Update updates the template files to the templates built in current goctl. @@ -47,5 +47,5 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } diff --git a/tools/goctl/api/docgen/gen.go b/tools/goctl/api/docgen/gen.go index 09f23de1..47976b80 100644 --- a/tools/goctl/api/docgen/gen.go +++ b/tools/goctl/api/docgen/gen.go @@ -8,7 +8,7 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/api/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -28,7 +28,7 @@ func DocCommand(c *cli.Context) error { } } - if !util.FileExists(dir) { + if !pathx.FileExists(dir) { return fmt.Errorf("dir %s not exsit", dir) } diff --git a/tools/goctl/api/format/format.go b/tools/goctl/api/format/format.go index 82b5b5e3..e4366545 100644 --- a/tools/goctl/api/format/format.go +++ b/tools/goctl/api/format/format.go @@ -14,7 +14,7 @@ import ( "github.com/tal-tech/go-zero/core/errorx" "github.com/tal-tech/go-zero/tools/goctl/api/parser" "github.com/tal-tech/go-zero/tools/goctl/api/util" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 88a9f4f7..99569193 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -18,6 +18,7 @@ import ( apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -40,7 +41,7 @@ func GoCommand(c *cli.Context) error { } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(apiFile) == 0 { return errors.New("missing -api") @@ -64,7 +65,7 @@ func DoGenProject(apiFile, dir, style string) error { return err } - logx.Must(util.MkdirIfNotExist(dir)) + logx.Must(pathx.MkdirIfNotExist(dir)) rootPkg, err := getParentPackage(dir) if err != nil { return err diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 9bd7920a..232ed5b3 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -10,6 +10,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/internal/version" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -122,10 +123,10 @@ func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) err func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string { var imports []string imports = append(imports, fmt.Sprintf("\"%s\"", - util.JoinPackages(parentPkg, getLogicFolderPath(group, route)))) - imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) + pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route)))) + imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir))) if len(route.RequestTypeName()) > 0 { - imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir))) + imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir))) } currentVersion := version.GetGoctlVersion() diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index 873a7aaa..c611ad38 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -9,8 +9,8 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/config" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 9f0df228..30076d7c 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -6,8 +6,8 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/config" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index cd41b220..ed486422 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -11,8 +11,8 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/config" - "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -151,7 +151,7 @@ rest.WithPrefix("%s"),`, g.prefix) func genRouteImports(parentPkg string, api *spec.ApiSpec) string { importSet := collection.NewSet() - importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) + importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir))) for _, group := range api.Service.Groups { for _, route := range group.Routes { folder := route.GetAnnotation(groupProperty) @@ -161,7 +161,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string { continue } } - importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), util.JoinPackages(parentPkg, handlerDir, folder))) + importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), pathx.JoinPackages(parentPkg, handlerDir, folder))) } } imports := importSet.KeysStr() diff --git a/tools/goctl/api/gogen/gensvc.go b/tools/goctl/api/gogen/gensvc.go index 45bb99b1..8400d607 100644 --- a/tools/goctl/api/gogen/gensvc.go +++ b/tools/goctl/api/gogen/gensvc.go @@ -6,8 +6,8 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/config" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) diff --git a/tools/goctl/api/gogen/template.go b/tools/goctl/api/gogen/template.go index e90268f5..96f514f5 100644 --- a/tools/goctl/api/gogen/template.go +++ b/tools/goctl/api/gogen/template.go @@ -3,7 +3,7 @@ package gogen import ( "fmt" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -33,12 +33,12 @@ func Category() string { // Clean cleans the generated deployment files. func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates generates api template files. func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate reverts the given template file to the default value. @@ -47,7 +47,7 @@ func RevertTemplate(name string) error { if !ok { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Update updates the template files to the templates built in current goctl. @@ -57,5 +57,5 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } diff --git a/tools/goctl/api/gogen/template_test.go b/tools/goctl/api/gogen/template_test.go index 0ab44d73..0370c6ce 100644 --- a/tools/goctl/api/gogen/template_test.go +++ b/tools/goctl/api/gogen/template_test.go @@ -6,13 +6,13 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestGenTemplates(t *testing.T) { - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, "main.tpl") data, err := ioutil.ReadFile(file) @@ -22,10 +22,10 @@ func TestGenTemplates(t *testing.T) { func TestRevertTemplate(t *testing.T) { name := "main.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -33,7 +33,7 @@ func TestRevertTemplate(t *testing.T) { assert.Nil(t, err) modifyData := string(data) + "modify" - err = util.CreateTemplate(category, name, modifyData) + err = pathx.CreateTemplate(category, name, modifyData) assert.Nil(t, err) data, err = ioutil.ReadFile(file) @@ -50,12 +50,12 @@ func TestRevertTemplate(t *testing.T) { func TestClean(t *testing.T) { name := "main.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) assert.Nil(t, Clean()) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -65,10 +65,10 @@ func TestClean(t *testing.T) { func TestUpdate(t *testing.T) { name := "main.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -76,7 +76,7 @@ func TestUpdate(t *testing.T) { assert.Nil(t, err) modifyData := string(data) + "modify" - err = util.CreateTemplate(category, name, modifyData) + err = pathx.CreateTemplate(category, name, modifyData) assert.Nil(t, err) data, err = ioutil.ReadFile(file) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index aa9ba8fc..65f98679 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -12,8 +12,8 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/ctx" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) type fileGenConfig struct { diff --git a/tools/goctl/api/javagen/gen.go b/tools/goctl/api/javagen/gen.go index 83ca6209..eddd92e6 100644 --- a/tools/goctl/api/javagen/gen.go +++ b/tools/goctl/api/javagen/gen.go @@ -8,7 +8,7 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/tools/goctl/api/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -30,7 +30,7 @@ func JavaCommand(c *cli.Context) error { api.Service = api.Service.JoinPrefix() packetName := strings.TrimSuffix(api.Service.Name, "-api") - logx.Must(util.MkdirIfNotExist(dir)) + logx.Must(pathx.MkdirIfNotExist(dir)) logx.Must(genPacket(dir, packetName, api)) logx.Must(genComponents(dir, packetName, api)) diff --git a/tools/goctl/api/javagen/gencomponents.go b/tools/goctl/api/javagen/gencomponents.go index f3842c80..a2632da4 100644 --- a/tools/goctl/api/javagen/gencomponents.go +++ b/tools/goctl/api/javagen/gencomponents.go @@ -14,6 +14,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const ( @@ -110,7 +111,7 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type modelFile := util.Title(ty.Name()) + ".java" filename := path.Join(dir, modelDir, modelFile) - if err := util.RemoveOrQuit(filename); err != nil { + if err := pathx.RemoveOrQuit(filename); err != nil { return err } @@ -270,7 +271,7 @@ func (c *componentsContext) buildConstructor() (string, string, error) { writeIndent(&constructorSetter, 2) constructorSetter.WriteString(fmt.Sprintf("this.%s = %s;", pn, util.Untitle(member.Name))) if index != len(c.members)-1 { - constructorSetter.WriteString(util.NL) + constructorSetter.WriteString(pathx.NL) } } return params.String(), constructorSetter.String(), nil diff --git a/tools/goctl/api/javagen/util.go b/tools/goctl/api/javagen/util.go index 57ee2fb5..2b427b50 100644 --- a/tools/goctl/api/javagen/util.go +++ b/tools/goctl/api/javagen/util.go @@ -8,12 +8,13 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func writeProperty(writer io.Writer, member spec.Member, indent int) error { if len(member.Comment) > 0 { writeIndent(writer, indent) - fmt.Fprint(writer, member.Comment+util.NL) + fmt.Fprint(writer, member.Comment+pathx.NL) } writeIndent(writer, indent) ty, err := specTypeToJava(member.Type) diff --git a/tools/goctl/api/new/newservice.go b/tools/goctl/api/new/newservice.go index 9ee6b18b..89118ac4 100644 --- a/tools/goctl/api/new/newservice.go +++ b/tools/goctl/api/new/newservice.go @@ -10,6 +10,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/gogen" conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -49,7 +50,7 @@ func CreateServiceCommand(c *cli.Context) error { return err } - err = util.MkdirIfNotExist(abs) + err = pathx.MkdirIfNotExist(abs) if err != nil { return err } @@ -74,10 +75,10 @@ func CreateServiceCommand(c *cli.Context) error { } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } - text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate) + text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate) if err != nil { return err } diff --git a/tools/goctl/api/new/template.go b/tools/goctl/api/new/template.go index c467c6f1..8c465431 100644 --- a/tools/goctl/api/new/template.go +++ b/tools/goctl/api/new/template.go @@ -3,7 +3,7 @@ package new import ( "fmt" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -23,12 +23,12 @@ func Category() string { // Clean cleans the generated deployment files. func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates generates api template files. func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate reverts the given template file to the default value. @@ -37,7 +37,7 @@ func RevertTemplate(name string) error { if !ok { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Update updates the template files to the templates built in current goctl. @@ -47,5 +47,5 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } diff --git a/tools/goctl/api/parser/g4/test/apiparser_test.go b/tools/goctl/api/parser/g4/test/apiparser_test.go index 13ebdffd..92fac555 100644 --- a/tools/goctl/api/parser/g4/test/apiparser_test.go +++ b/tools/goctl/api/parser/g4/test/apiparser_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) var ( @@ -119,7 +119,7 @@ func TestApiParser(t *testing.T) { }) t.Run("nestedImport", func(t *testing.T) { - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm) if err != nil { return @@ -149,7 +149,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousSyntax", func(t *testing.T) { - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) if err != nil { return @@ -163,7 +163,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousSyntax", func(t *testing.T) { - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm) if err != nil { return @@ -177,7 +177,7 @@ func TestApiParser(t *testing.T) { }) t.Run("ambiguousService", func(t *testing.T) { - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm) if err != nil { return @@ -207,7 +207,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm) if err != nil { return @@ -236,7 +236,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm) if err != nil { return @@ -260,7 +260,7 @@ func TestApiParser(t *testing.T) { `) assert.Error(t, err) - file := filepath.Join(util.MustTempDir(), "foo.api") + file := filepath.Join(pathx.MustTempDir(), "foo.api") err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm) if err != nil { return diff --git a/tools/goctl/api/tsgen/gen.go b/tools/goctl/api/tsgen/gen.go index 950e9265..5a361fda 100644 --- a/tools/goctl/api/tsgen/gen.go +++ b/tools/goctl/api/tsgen/gen.go @@ -7,7 +7,7 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/tools/goctl/api/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -33,7 +33,7 @@ func TsCommand(c *cli.Context) error { } api.Service = api.Service.JoinPrefix() - logx.Must(util.MkdirIfNotExist(dir)) + logx.Must(pathx.MkdirIfNotExist(dir)) logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI)) logx.Must(genComponents(dir, api)) diff --git a/tools/goctl/api/tsgen/gencomponents.go b/tools/goctl/api/tsgen/gencomponents.go index 1373fd37..4ac8e87c 100644 --- a/tools/goctl/api/tsgen/gencomponents.go +++ b/tools/goctl/api/tsgen/gencomponents.go @@ -7,7 +7,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const ( @@ -30,7 +30,7 @@ func genComponents(dir string, api *spec.ApiSpec) error { outputFile := apiutil.ComponentName(api) + ".ts" filename := path.Join(dir, outputFile) - if err := util.RemoveIfExist(filename); err != nil { + if err := pathx.RemoveIfExist(filename); err != nil { return err } diff --git a/tools/goctl/api/tsgen/genpacket.go b/tools/goctl/api/tsgen/genpacket.go index 6ffee466..37cb2846 100644 --- a/tools/goctl/api/tsgen/genpacket.go +++ b/tools/goctl/api/tsgen/genpacket.go @@ -9,6 +9,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const ( @@ -20,7 +21,7 @@ const ( func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error { filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts" - if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil { + if err := pathx.RemoveIfExist(path.Join(dir, filename)); err != nil { return err } fp, created, err := apiutil.MaybeCreateFile(dir, "", filename) @@ -46,11 +47,11 @@ func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) e if len(api.Types) != 0 { if len(imports) > 0 { - imports += util.NL + imports += pathx.NL } outputFile := apiutil.ComponentName(api) imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile) - imports += fmt.Sprintf(`%sexport * from "%s"`, util.NL, "./"+outputFile) + imports += fmt.Sprintf(`%sexport * from "%s"`, pathx.NL, "./"+outputFile) } apis, err := genAPI(api, caller) diff --git a/tools/goctl/api/util/util.go b/tools/goctl/api/util/util.go index 2a1a18a7..8afa2e1c 100644 --- a/tools/goctl/api/util/util.go +++ b/tools/goctl/api/util/util.go @@ -10,19 +10,19 @@ import ( "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/tools/goctl/api/spec" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) // MaybeCreateFile creates file if not exists func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) { - logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir))) + logx.Must(pathx.MkdirIfNotExist(path.Join(dir, subdir))) fpath := path.Join(dir, subdir, file) - if util.FileExists(fpath) { + if pathx.FileExists(fpath) { fmt.Printf("%s exists, ignored generation\n", fpath) return nil, false, nil } - fp, err = util.CreateIfNotExist(fpath) + fp, err = pathx.CreateIfNotExist(fpath) created = err == nil return } diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 3ffa381b..38f9a427 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -11,6 +11,7 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -57,14 +58,14 @@ func DockerCommand(c *cli.Context) (err error) { } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(goFile) == 0 { return errors.New("-go can't be empty") } - if !util.FileExists(goFile) { + if !pathx.FileExists(goFile) { return fmt.Errorf("file %q not found", goFile) } @@ -82,7 +83,7 @@ func DockerCommand(c *cli.Context) (err error) { return err } - projDir, ok := util.FindProjectPath(goFile) + projDir, ok := pathx.FindProjectPath(goFile) if ok { fmt.Printf("Hint: run \"docker build ...\" command in dir:\n %s\n", projDir) } @@ -129,13 +130,13 @@ func generateDockerfile(goFile string, port int, version string, args ...string) projPath = "." } - out, err := util.CreateIfNotExist(dockerfileName) + out, err := pathx.CreateIfNotExist(dockerfileName) if err != nil { return err } defer out.Close() - text, err := util.LoadTemplate(category, dockerTemplateFile, dockerTemplate) + text, err := pathx.LoadTemplate(category, dockerTemplateFile, dockerTemplate) if err != nil { return err } @@ -151,7 +152,7 @@ func generateDockerfile(goFile string, port int, version string, args ...string) Chinese: offset == cstOffset, GoRelPath: projPath, GoFile: goFile, - ExeFile: util.FileNameWithoutExt(filepath.Base(goFile)), + ExeFile: pathx.FileNameWithoutExt(filepath.Base(goFile)), HasPort: port > 0, Port: port, Argument: builder.String(), @@ -165,9 +166,9 @@ func getFilePath(file string) (string, error) { return "", err } - projPath, ok := util.FindGoModPath(filepath.Join(wd, file)) + projPath, ok := pathx.FindGoModPath(filepath.Join(wd, file)) if !ok { - projPath, err = util.PathFromGoSrc() + projPath, err = pathx.PathFromGoSrc() if err != nil { return "", errors.New("no go.mod found, or not in GOPATH") } diff --git a/tools/goctl/docker/template.go b/tools/goctl/docker/template.go index b4d721e6..51c62306 100644 --- a/tools/goctl/docker/template.go +++ b/tools/goctl/docker/template.go @@ -1,7 +1,7 @@ package docker import ( - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -43,7 +43,7 @@ CMD ["./{{.ExeFile}}"{{.Argument}}] // Clean deletes all templates files func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates creates docker template files @@ -58,7 +58,7 @@ func Category() string { // RevertTemplate recovers the deleted template files func RevertTemplate(name string) error { - return util.CreateTemplate(category, name, dockerTemplate) + return pathx.CreateTemplate(category, name, dockerTemplate) } // Update deletes and creates new template files @@ -72,7 +72,7 @@ func Update() error { } func initTemplate() error { - return util.InitTemplates(category, map[string]string{ + return pathx.InitTemplates(category, map[string]string{ dockerTemplateFile: dockerTemplate, }) } diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 421a3926..9626b8cc 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -7,6 +7,7 @@ import ( "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -50,7 +51,7 @@ func DeploymentCommand(c *cli.Context) error { } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } // 0 to disable the nodePort type @@ -58,12 +59,12 @@ func DeploymentCommand(c *cli.Context) error { return errors.New("nodePort should be between 30000 and 32767") } - text, err := util.LoadTemplate(category, deployTemplateFile, deploymentTemplate) + text, err := pathx.LoadTemplate(category, deployTemplateFile, deploymentTemplate) if err != nil { return err } - out, err := util.CreateIfNotExist(c.String("o")) + out, err := pathx.CreateIfNotExist(c.String("o")) if err != nil { return err } @@ -102,12 +103,12 @@ func Category() string { // Clean cleans the generated deployment files. func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates generates the deployment template files. func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, map[string]string{ + return pathx.InitTemplates(category, map[string]string{ deployTemplateFile: deploymentTemplate, jobTemplateFile: jobTmeplate, }) @@ -115,7 +116,7 @@ func GenTemplates(_ *cli.Context) error { // RevertTemplate reverts the given template file to the default value. func RevertTemplate(name string) error { - return util.CreateTemplate(category, name, deploymentTemplate) + return pathx.CreateTemplate(category, name, deploymentTemplate) } // Update updates the template files to the templates built in current goctl. @@ -125,7 +126,7 @@ func Update() error { return err } - return util.InitTemplates(category, map[string]string{ + return pathx.InitTemplates(category, map[string]string{ deployTemplateFile: deploymentTemplate, jobTemplateFile: jobTmeplate, }) diff --git a/tools/goctl/model/mongo/generate/generate.go b/tools/goctl/model/mongo/generate/generate.go index ce58a451..9dc7209e 100644 --- a/tools/goctl/model/mongo/generate/generate.go +++ b/tools/goctl/model/mongo/generate/generate.go @@ -8,6 +8,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/mongo/template" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) // Context defines the model generation data what they needs @@ -39,7 +40,7 @@ func generateModel(ctx *Context) error { return err } - text, err := util.LoadTemplate(category, modelTemplateFile, template.Text) + text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Text) if err != nil { return err } @@ -58,7 +59,7 @@ func generateModel(ctx *Context) error { } func generateError(ctx *Context) error { - text, err := util.LoadTemplate(category, errTemplateFile, template.Error) + text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) if err != nil { return err } diff --git a/tools/goctl/model/mongo/generate/generate_test.go b/tools/goctl/model/mongo/generate/generate_test.go index c8032fcd..23eb8c01 100644 --- a/tools/goctl/model/mongo/generate/generate_test.go +++ b/tools/goctl/model/mongo/generate/generate_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/config" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) var testTypes = ` @@ -19,7 +19,7 @@ func TestDo(t *testing.T) { cfg, err := config.NewConfig(config.DefaultFormat) assert.Nil(t, err) - tempDir := util.MustTempDir() + tempDir := pathx.MustTempDir() typesfile := filepath.Join(tempDir, "types.go") err = ioutil.WriteFile(typesfile, []byte(testTypes), 0o666) assert.Nil(t, err) diff --git a/tools/goctl/model/mongo/generate/template.go b/tools/goctl/model/mongo/generate/template.go index a422ed71..1194f528 100644 --- a/tools/goctl/model/mongo/generate/template.go +++ b/tools/goctl/model/mongo/generate/template.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/tal-tech/go-zero/tools/goctl/model/mongo/template" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -26,12 +26,12 @@ func Category() string { // Clean cleans the mongo templates. func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // Templates initializes the mongo templates. func Templates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate reverts the given template. @@ -41,7 +41,7 @@ func RevertTemplate(name string) error { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Update cleans and updates the templates. @@ -51,5 +51,5 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index 8bf93928..dcfb7f64 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -8,6 +8,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate" file "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -26,7 +27,7 @@ func Action(ctx *cli.Context) error { } } if len(home) > 0 { - file.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(tp) == 0 { diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index 2fae4f0c..b6af5945 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -15,6 +15,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" file "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -50,7 +51,7 @@ func MysqlDDL(ctx *cli.Context) error { } } if len(home) > 0 { - file.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } cfg, err := config.NewConfig(style) if err != nil { @@ -76,7 +77,7 @@ func MySqlDataSource(ctx *cli.Context) error { } } if len(home) > 0 { - file.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } pattern := strings.TrimSpace(ctx.String(flagTable)) @@ -105,7 +106,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error { } } if len(home) > 0 { - file.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(schema) == 0 { diff --git a/tools/goctl/model/sql/command/command_test.go b/tools/goctl/model/sql/command/command_test.go index ebdcf501..201f3c42 100644 --- a/tools/goctl/model/sql/command/command_test.go +++ b/tools/goctl/model/sql/command/command_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) var ( @@ -23,12 +23,12 @@ func TestFromDDl(t *testing.T) { err := gen.Clean() assert.Nil(t, err) - err = fromDDL("./user.sql", util.MustTempDir(), cfg, true, false, "go_zero") + err = fromDDL("./user.sql", pathx.MustTempDir(), cfg, true, false, "go_zero") assert.Equal(t, errNotMatched, err) // case dir is not exists - unknownDir := filepath.Join(util.MustTempDir(), "test", "user.sql") - err = fromDDL(unknownDir, util.MustTempDir(), cfg, true, false, "go_zero") + unknownDir := filepath.Join(pathx.MustTempDir(), "test", "user.sql") + err = fromDDL(unknownDir, pathx.MustTempDir(), cfg, true, false, "go_zero") assert.True(t, func() bool { switch err.(type) { case *os.PathError: @@ -39,13 +39,13 @@ func TestFromDDl(t *testing.T) { }()) // case empty src - err = fromDDL("", util.MustTempDir(), cfg, true, false, "go_zero") + err = fromDDL("", pathx.MustTempDir(), cfg, true, false, "go_zero") if err != nil { assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error()) } - tempDir := filepath.Join(util.MustTempDir(), "test") - err = util.MkdirIfNotExist(tempDir) + tempDir := filepath.Join(pathx.MustTempDir(), "test") + err = pathx.MkdirIfNotExist(tempDir) if err != nil { return } diff --git a/tools/goctl/model/sql/gen/delete.go b/tools/goctl/model/sql/gen/delete.go index c7ee7b79..47e9fb31 100644 --- a/tools/goctl/model/sql/gen/delete.go +++ b/tools/goctl/model/sql/gen/delete.go @@ -6,6 +6,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -20,7 +21,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) } camel := table.Name.ToCamel() - text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete) + text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete) if err != nil { return "", "", err } @@ -43,7 +44,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error) } // interface method - text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod) + text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod) if err != nil { return "", "", err } diff --git a/tools/goctl/model/sql/gen/field.go b/tools/goctl/model/sql/gen/field.go index c4a96ffd..bca2f17d 100644 --- a/tools/goctl/model/sql/gen/field.go +++ b/tools/goctl/model/sql/gen/field.go @@ -6,6 +6,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/parser" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func genFields(fields []*parser.Field) (string, error) { @@ -29,7 +30,7 @@ func genField(field *parser.Field) (string, error) { return "", err } - text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field) + text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/gen/findone.go b/tools/goctl/model/sql/gen/findone.go index c07b9a9c..a5acd62a 100644 --- a/tools/goctl/model/sql/gen/findone.go +++ b/tools/goctl/model/sql/gen/findone.go @@ -3,12 +3,13 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) { camel := table.Name.ToCamel() - text, err := util.LoadTemplate(category, findOneTemplateFile, template.FindOne) + text, err := pathx.LoadTemplate(category, findOneTemplateFile, template.FindOne) if err != nil { return "", "", err } @@ -30,7 +31,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) return "", "", err } - text, err = util.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod) + text, err = pathx.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod) if err != nil { return "", "", err } diff --git a/tools/goctl/model/sql/gen/findonebyfield.go b/tools/goctl/model/sql/gen/findonebyfield.go index 9a020084..35373d9a 100644 --- a/tools/goctl/model/sql/gen/findonebyfield.go +++ b/tools/goctl/model/sql/gen/findonebyfield.go @@ -6,6 +6,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -16,7 +17,7 @@ type findOneCode struct { } func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, error) { - text, err := util.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField) + text, err := pathx.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField) if err != nil { return nil, err } @@ -47,7 +48,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e list = append(list, output.String()) } - text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod) + text, err = pathx.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod) if err != nil { return nil, err } @@ -79,7 +80,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e } if withCache { - text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod) + text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod) if err != nil { return nil, err } @@ -96,15 +97,15 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e } return &findOneCode{ - findOneMethod: strings.Join(list, util.NL), - findOneInterfaceMethod: strings.Join(listMethod, util.NL), + findOneMethod: strings.Join(list, pathx.NL), + findOneInterfaceMethod: strings.Join(listMethod, pathx.NL), cacheExtra: out.String(), }, nil } return &findOneCode{ - findOneMethod: strings.Join(list, util.NL), - findOneInterfaceMethod: strings.Join(listMethod, util.NL), + findOneMethod: strings.Join(list, pathx.NL), + findOneInterfaceMethod: strings.Join(listMethod, pathx.NL), }, nil } diff --git a/tools/goctl/model/sql/gen/gen.go b/tools/goctl/model/sql/gen/gen.go index b70e3383..8f2b50b6 100644 --- a/tools/goctl/model/sql/gen/gen.go +++ b/tools/goctl/model/sql/gen/gen.go @@ -16,6 +16,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -62,7 +63,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul dir = dirAbs pkg := filepath.Base(dirAbs) - err = util.MkdirIfNotExist(dir) + err = pathx.MkdirIfNotExist(dir) if err != nil { return nil, err } @@ -134,7 +135,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error { g.dir = dirAbs g.pkg = filepath.Base(dirAbs) - err = util.MkdirIfNotExist(dirAbs) + err = pathx.MkdirIfNotExist(dirAbs) if err != nil { return err } @@ -148,7 +149,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error { name := util.SafeString(modelFilename) + ".go" filename := filepath.Join(dirAbs, name) - if util.FileExists(filename) { + if pathx.FileExists(filename) { g.Warning("%s already exists, ignored.", name) continue } @@ -165,7 +166,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error { } filename := filepath.Join(dirAbs, varFilename+".go") - text, err := util.LoadTemplate(category, errTemplateFile, template.Error) + text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error) if err != nil { return err } @@ -261,7 +262,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er var list []string list = append(list, insertCodeMethod, findOneCodeMethod, ret.findOneInterfaceMethod, updateCodeMethod, deleteCodeMethod) - typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), util.NL), withCache) + typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), pathx.NL), withCache) if err != nil { return "", err } @@ -292,7 +293,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er } func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) { - text, err := util.LoadTemplate(category, modelTemplateFile, template.Model) + text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model) if err != nil { return nil, err } diff --git a/tools/goctl/model/sql/gen/gen_test.go b/tools/goctl/model/sql/gen/gen_test.go index a2a59178..dbe2069c 100644 --- a/tools/goctl/model/sql/gen/gen_test.go +++ b/tools/goctl/model/sql/gen/gen_test.go @@ -14,7 +14,7 @@ import ( "github.com/tal-tech/go-zero/core/stringx" "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" @@ -23,11 +23,11 @@ func TestCacheModel(t *testing.T) { logx.Disable() _ = Clean() - sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql") + sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) assert.Nil(t, err) - dir := filepath.Join(util.MustTempDir(), "./testmodel") + dir := filepath.Join(pathx.MustTempDir(), "./testmodel") cacheDir := filepath.Join(dir, "cache") noCacheDir := filepath.Join(dir, "nocache") g, err := NewDefaultGenerator(cacheDir, &config.Config{ @@ -58,7 +58,7 @@ func TestNamingModel(t *testing.T) { logx.Disable() _ = Clean() - sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql") + sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql") err := ioutil.WriteFile(sqlFile, []byte(source), 0o777) assert.Nil(t, err) diff --git a/tools/goctl/model/sql/gen/imports.go b/tools/goctl/model/sql/gen/imports.go index 1916113b..a89cb21b 100644 --- a/tools/goctl/model/sql/gen/imports.go +++ b/tools/goctl/model/sql/gen/imports.go @@ -3,11 +3,12 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func genImports(withCache, timeImport bool) (string, error) { if withCache { - text, err := util.LoadTemplate(category, importsTemplateFile, template.Imports) + text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports) if err != nil { return "", err } @@ -22,7 +23,7 @@ func genImports(withCache, timeImport bool) (string, error) { return buffer.String(), nil } - text, err := util.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache) + text, err := pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/gen/insert.go b/tools/goctl/model/sql/gen/insert.go index ada44819..c2bd2cf0 100644 --- a/tools/goctl/model/sql/gen/insert.go +++ b/tools/goctl/model/sql/gen/insert.go @@ -7,6 +7,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -45,7 +46,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) } camel := table.Name.ToCamel() - text, err := util.LoadTemplate(category, insertTemplateFile, template.Insert) + text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert) if err != nil { return "", "", err } @@ -67,7 +68,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error) } // interface method - text, err = util.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod) + text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod) if err != nil { return "", "", err } diff --git a/tools/goctl/model/sql/gen/new.go b/tools/goctl/model/sql/gen/new.go index 50e1dd09..fec03df3 100644 --- a/tools/goctl/model/sql/gen/new.go +++ b/tools/goctl/model/sql/gen/new.go @@ -5,10 +5,11 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func genNew(table Table, withCache, postgreSql bool) (string, error) { - text, err := util.LoadTemplate(category, modelNewTemplateFile, template.New) + text, err := pathx.LoadTemplate(category, modelNewTemplateFile, template.New) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/gen/tag.go b/tools/goctl/model/sql/gen/tag.go index fe09622a..612089ac 100644 --- a/tools/goctl/model/sql/gen/tag.go +++ b/tools/goctl/model/sql/gen/tag.go @@ -3,6 +3,7 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func genTag(in string) (string, error) { @@ -10,7 +11,7 @@ func genTag(in string) (string, error) { return in, nil } - text, err := util.LoadTemplate(category, tagTemplateFile, template.Tag) + text, err := pathx.LoadTemplate(category, tagTemplateFile, template.Tag) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/gen/template.go b/tools/goctl/model/sql/gen/template.go index 6b06b534..c04eae50 100644 --- a/tools/goctl/model/sql/gen/template.go +++ b/tools/goctl/model/sql/gen/template.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -62,12 +62,12 @@ func Category() string { // Clean deletes all template files func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // GenTemplates creates template files if not exists func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate recovers the delete template files @@ -77,7 +77,7 @@ func RevertTemplate(name string) error { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Update provides template clean and init @@ -87,5 +87,5 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } diff --git a/tools/goctl/model/sql/gen/template_test.go b/tools/goctl/model/sql/gen/template_test.go index 60b5d2ae..987ff8ea 100644 --- a/tools/goctl/model/sql/gen/template_test.go +++ b/tools/goctl/model/sql/gen/template_test.go @@ -7,13 +7,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestGenTemplates(t *testing.T) { - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, "model-new.tpl") data, err := ioutil.ReadFile(file) @@ -23,10 +23,10 @@ func TestGenTemplates(t *testing.T) { func TestRevertTemplate(t *testing.T) { name := "model-new.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -34,7 +34,7 @@ func TestRevertTemplate(t *testing.T) { assert.Nil(t, err) modifyData := string(data) + "modify" - err = util.CreateTemplate(category, name, modifyData) + err = pathx.CreateTemplate(category, name, modifyData) assert.Nil(t, err) data, err = ioutil.ReadFile(file) @@ -51,12 +51,12 @@ func TestRevertTemplate(t *testing.T) { func TestClean(t *testing.T) { name := "model-new.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) assert.Nil(t, Clean()) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -66,10 +66,10 @@ func TestClean(t *testing.T) { func TestUpdate(t *testing.T) { name := "model-new.tpl" - err := util.InitTemplates(category, templates) + err := pathx.InitTemplates(category, templates) assert.Nil(t, err) - dir, err := util.GetTemplateDir(category) + dir, err := pathx.GetTemplateDir(category) assert.Nil(t, err) file := filepath.Join(dir, name) @@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) { assert.Nil(t, err) modifyData := string(data) + "modify" - err = util.CreateTemplate(category, name, modifyData) + err = pathx.CreateTemplate(category, name, modifyData) assert.Nil(t, err) data, err = ioutil.ReadFile(file) diff --git a/tools/goctl/model/sql/gen/types.go b/tools/goctl/model/sql/gen/types.go index 71a67056..57d3a37f 100644 --- a/tools/goctl/model/sql/gen/types.go +++ b/tools/goctl/model/sql/gen/types.go @@ -3,6 +3,7 @@ package gen import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func genTypes(table Table, methods string, withCache bool) (string, error) { @@ -12,7 +13,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) { return "", err } - text, err := util.LoadTemplate(category, typesTemplateFile, template.Types) + text, err := pathx.LoadTemplate(category, typesTemplateFile, template.Types) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/gen/update.go b/tools/goctl/model/sql/gen/update.go index 7887826a..de2c6230 100644 --- a/tools/goctl/model/sql/gen/update.go +++ b/tools/goctl/model/sql/gen/update.go @@ -6,6 +6,7 @@ import ( "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -39,7 +40,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel()) } camelTableName := table.Name.ToCamel() - text, err := util.LoadTemplate(category, updateTemplateFile, template.Update) + text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update) if err != nil { return "", "", err } @@ -63,7 +64,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error) } // update interface method - text, err = util.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod) + text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod) if err != nil { return "", "", err } diff --git a/tools/goctl/model/sql/gen/vars.go b/tools/goctl/model/sql/gen/vars.go index 4d8cede3..58deff06 100644 --- a/tools/goctl/model/sql/gen/vars.go +++ b/tools/goctl/model/sql/gen/vars.go @@ -5,6 +5,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/template" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -16,7 +17,7 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) { } camel := table.Name.ToCamel() - text, err := util.LoadTemplate(category, varTemplateFile, template.Vars) + text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars) if err != nil { return "", err } diff --git a/tools/goctl/model/sql/parser/parser_test.go b/tools/goctl/model/sql/parser/parser_test.go index 8621d34a..72898f87 100644 --- a/tools/goctl/model/sql/parser/parser_test.go +++ b/tools/goctl/model/sql/parser/parser_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/tools/goctl/model/sql/model" "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" - ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestParsePlainText(t *testing.T) { diff --git a/tools/goctl/plugin/plugin.go b/tools/goctl/plugin/plugin.go index 3ab8783b..d681e060 100644 --- a/tools/goctl/plugin/plugin.go +++ b/tools/goctl/plugin/plugin.go @@ -16,7 +16,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/parser" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -73,7 +73,7 @@ func prepareArgs(c *cli.Context) ([]byte, error) { apiPath := c.String("api") var transferData Plugin - if len(apiPath) > 0 && util.FileExists(apiPath) { + if len(apiPath) > 0 && pathx.FileExists(apiPath) { api, err := parser.Parse(apiPath) if err != nil { return nil, err diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index 99da817b..d4247da7 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -9,6 +9,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/generator" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/env" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -34,7 +35,7 @@ func RPC(c *cli.Context) error { } } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(src) == 0 { @@ -87,7 +88,7 @@ func RPCNew(c *cli.Context) error { } } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } protoName := rpcname + ".proto" @@ -122,7 +123,7 @@ func RPCTemplate(c *cli.Context) error { } } if len(home) > 0 { - util.RegisterGoctlHome(home) + pathx.RegisterGoctlHome(home) } if len(protoFile) == 0 { diff --git a/tools/goctl/rpc/execx/execx.go b/tools/goctl/rpc/execx/execx.go index 2c674d2a..1a1073d7 100644 --- a/tools/goctl/rpc/execx/execx.go +++ b/tools/goctl/rpc/execx/execx.go @@ -8,7 +8,7 @@ import ( "runtime" "strings" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -39,10 +39,10 @@ func Run(arg, dir string, in ...*bytes.Buffer) (string, error) { err := cmd.Run() if err != nil { if stderr.Len() > 0 { - return "", errors.New(strings.TrimSuffix(stderr.String(), util.NL)) + return "", errors.New(strings.TrimSuffix(stderr.String(), pathx.NL)) } return "", err } - return strings.TrimSuffix(stdout.String(), util.NL), nil + return strings.TrimSuffix(stdout.String(), pathx.NL), nil } diff --git a/tools/goctl/rpc/generator/gen.go b/tools/goctl/rpc/generator/gen.go index 02b32b74..d0808357 100644 --- a/tools/goctl/rpc/generator/gen.go +++ b/tools/goctl/rpc/generator/gen.go @@ -5,9 +5,9 @@ import ( conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/tal-tech/go-zero/tools/goctl/util/ctx" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) // RPCGenerator defines a generator and configure @@ -42,7 +42,7 @@ func (g *RPCGenerator) Generate(src, target string, protoImportPath []string, go return err } - err = util.MkdirIfNotExist(abs) + err = pathx.MkdirIfNotExist(abs) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/gen_test.go b/tools/goctl/rpc/generator/gen_test.go index eb8cdefa..d4afb286 100644 --- a/tools/goctl/rpc/generator/gen_test.go +++ b/tools/goctl/rpc/generator/gen_test.go @@ -12,7 +12,7 @@ import ( "github.com/tal-tech/go-zero/core/stringx" conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) var cfg = &conf.Config{ @@ -58,7 +58,7 @@ func TestRpcGenerate(t *testing.T) { // case go mod t.Run("GOMOD", func(t *testing.T) { - workDir := util.MustTempDir() + workDir := pathx.MustTempDir() name := filepath.Base(workDir) _, err = execx.Run("go mod init "+name, workDir) if err != nil { diff --git a/tools/goctl/rpc/generator/gencall.go b/tools/goctl/rpc/generator/gencall.go index 85a37f02..7a141ce0 100644 --- a/tools/goctl/rpc/generator/gencall.go +++ b/tools/goctl/rpc/generator/gencall.go @@ -12,6 +12,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf return err } - text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText) + text, err := pathx.LoadTemplate(category, callTemplateFile, callTemplateText) if err != nil { return err } @@ -100,13 +101,13 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf sort.Strings(aliasKeys) err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{ "name": callFilename, - "alias": strings.Join(aliasKeys, util.NL), + "alias": strings.Join(aliasKeys, pathx.NL), "head": head, "filePackage": dir.Base, "package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package), "serviceName": stringx.From(service.Name).ToCamel(), - "functions": strings.Join(functions, util.NL), - "interface": strings.Join(iFunctions, util.NL), + "functions": strings.Join(functions, pathx.NL), + "interface": strings.Join(iFunctions, pathx.NL), }, filename, true) return err } @@ -137,7 +138,7 @@ func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service) functions := make([]string, 0) for _, rpc := range service.RPC { - text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate) + text, err := pathx.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate) if err != nil { return nil, err } @@ -171,7 +172,7 @@ func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Se functions := make([]string, 0) for _, rpc := range service.RPC { - text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate) + text, err := pathx.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate) if err != nil { return nil, err } diff --git a/tools/goctl/rpc/generator/genconfig.go b/tools/goctl/rpc/generator/genconfig.go index b255bade..35f4a06e 100644 --- a/tools/goctl/rpc/generator/genconfig.go +++ b/tools/goctl/rpc/generator/genconfig.go @@ -7,8 +7,8 @@ import ( conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const configTemplate = `package config @@ -32,11 +32,11 @@ func (g *DefaultGenerator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.C } fileName := filepath.Join(dir.Filename, configFilename+".go") - if util.FileExists(fileName) { + if pathx.FileExists(fileName) { return nil } - text, err := util.LoadTemplate(category, configTemplateFileFile, configTemplate) + text, err := pathx.LoadTemplate(category, configTemplateFileFile, configTemplate) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/genetc.go b/tools/goctl/rpc/generator/genetc.go index 60ebe31e..51f9e401 100644 --- a/tools/goctl/rpc/generator/genetc.go +++ b/tools/goctl/rpc/generator/genetc.go @@ -9,6 +9,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -31,7 +32,7 @@ func (g *DefaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Conf fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename)) - text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate) + text, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/genlogic.go b/tools/goctl/rpc/generator/genlogic.go index aa04707e..1bb22baf 100644 --- a/tools/goctl/rpc/generator/genlogic.go +++ b/tools/goctl/rpc/generator/genlogic.go @@ -10,6 +10,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -67,14 +68,14 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con imports := collection.NewSet() imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)) imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package)) - text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate) + text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate) if err != nil { return err } err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{ "logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()), "functions": functions, - "imports": strings.Join(imports.KeysStr(), util.NL), + "imports": strings.Join(imports.KeysStr(), pathx.NL), }, filename, false) if err != nil { return err @@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) { functions := make([]string, 0) - text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate) + text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate) if err != nil { return "", err } @@ -111,5 +112,5 @@ func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc * } functions = append(functions, buffer.String()) - return strings.Join(functions, util.NL), nil + return strings.Join(functions, pathx.NL), nil } diff --git a/tools/goctl/rpc/generator/genmain.go b/tools/goctl/rpc/generator/genmain.go index 19ee9570..f85e2f30 100644 --- a/tools/goctl/rpc/generator/genmain.go +++ b/tools/goctl/rpc/generator/genmain.go @@ -9,6 +9,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -65,7 +66,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package) configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package) imports = append(imports, configImport, pbImport, remoteImport, svcImport) - text, err := util.LoadTemplate(category, mainTemplateFile, mainTemplate) + text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate) if err != nil { return err } @@ -77,7 +78,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{ "serviceName": etcFileName, - "imports": strings.Join(imports, util.NL), + "imports": strings.Join(imports, pathx.NL), "pkg": proto.PbPackage, "serviceNew": stringx.From(proto.Service.Name).ToCamel(), "service": parser.CamelCase(proto.Service.Name), diff --git a/tools/goctl/rpc/generator/genserver.go b/tools/goctl/rpc/generator/genserver.go index fb5aba0b..b974e659 100644 --- a/tools/goctl/rpc/generator/genserver.go +++ b/tools/goctl/rpc/generator/genserver.go @@ -10,6 +10,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -68,7 +69,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co return err } - text, err := util.LoadTemplate(category, serverTemplateFile, serverTemplate) + text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate) if err != nil { return err } @@ -84,8 +85,8 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{ "head": head, "server": stringx.From(service.Name).ToCamel(), - "imports": strings.Join(imports.KeysStr(), util.NL), - "funcs": strings.Join(funcList, util.NL), + "imports": strings.Join(imports.KeysStr(), pathx.NL), + "funcs": strings.Join(funcList, pathx.NL), "notStream": notStream, }, serverFile, true) return err @@ -94,7 +95,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co func (g *DefaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) { var functionList []string for _, rpc := range service.RPC { - text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate) + text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate) if err != nil { return nil, err } diff --git a/tools/goctl/rpc/generator/gensvc.go b/tools/goctl/rpc/generator/gensvc.go index 8bc1a9e1..b4368f99 100644 --- a/tools/goctl/rpc/generator/gensvc.go +++ b/tools/goctl/rpc/generator/gensvc.go @@ -8,6 +8,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/format" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const svcTemplate = `package svc @@ -35,7 +36,7 @@ func (g *DefaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Conf } fileName := filepath.Join(dir.Filename, svcFilename+".go") - text, err := util.LoadTemplate(category, svcTemplateFile, svcTemplate) + text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/mkdir.go b/tools/goctl/rpc/generator/mkdir.go index dd39d684..f99c7acc 100644 --- a/tools/goctl/rpc/generator/mkdir.go +++ b/tools/goctl/rpc/generator/mkdir.go @@ -6,8 +6,8 @@ import ( conf "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/rpc/parser" - "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/ctx" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -111,7 +111,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config) (DirCont Base: filepath.Base(callDir), } for _, v := range inner { - err := util.MkdirIfNotExist(v.Filename) + err := pathx.MkdirIfNotExist(v.Filename) if err != nil { return nil, err } diff --git a/tools/goctl/rpc/generator/prototmpl.go b/tools/goctl/rpc/generator/prototmpl.go index 637cb03d..cf7cc04b 100644 --- a/tools/goctl/rpc/generator/prototmpl.go +++ b/tools/goctl/rpc/generator/prototmpl.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/util/stringx" ) @@ -29,13 +30,13 @@ service {{.serviceName}} { func ProtoTmpl(out string) error { protoFilename := filepath.Base(out) serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename))) - text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText) + text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText) if err != nil { return err } dir := filepath.Dir(out) - err = util.MkdirIfNotExist(dir) + err = pathx.MkdirIfNotExist(dir) if err != nil { return err } diff --git a/tools/goctl/rpc/generator/prototmpl_test.go b/tools/goctl/rpc/generator/prototmpl_test.go index 6a13a3e5..f3d04fbf 100644 --- a/tools/goctl/rpc/generator/prototmpl_test.go +++ b/tools/goctl/rpc/generator/prototmpl_test.go @@ -5,17 +5,17 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestProtoTmpl(t *testing.T) { _ = Clean() // exists dir - err := ProtoTmpl(util.MustTempDir()) + err := ProtoTmpl(pathx.MustTempDir()) assert.Nil(t, err) // not exist dir - dir := filepath.Join(util.MustTempDir(), "test") + dir := filepath.Join(pathx.MustTempDir(), "test") err = ProtoTmpl(dir) assert.Nil(t, err) } diff --git a/tools/goctl/rpc/generator/template.go b/tools/goctl/rpc/generator/template.go index dbf8faaa..da482875 100644 --- a/tools/goctl/rpc/generator/template.go +++ b/tools/goctl/rpc/generator/template.go @@ -3,7 +3,7 @@ package generator import ( "fmt" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -41,7 +41,7 @@ var templates = map[string]string{ // GenTemplates is the entry for command goctl template, // it will create the specified category func GenTemplates(_ *cli.Context) error { - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // RevertTemplate restores the deleted template files @@ -50,12 +50,12 @@ func RevertTemplate(name string) error { if !ok { return fmt.Errorf("%s: no such file name", name) } - return util.CreateTemplate(category, name, content) + return pathx.CreateTemplate(category, name, content) } // Clean deletes all template files func Clean() error { - return util.Clean(category) + return pathx.Clean(category) } // Update is used to update the template files, it will delete the existing old templates at first, @@ -66,7 +66,7 @@ func Update() error { return err } - return util.InitTemplates(category, templates) + return pathx.InitTemplates(category, templates) } // Category returns a const string value for rpc template category diff --git a/tools/goctl/rpc/generator/template_test.go b/tools/goctl/rpc/generator/template_test.go index db519d5f..951cb625 100644 --- a/tools/goctl/rpc/generator/template_test.go +++ b/tools/goctl/rpc/generator/template_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestGenTemplates(t *testing.T) { @@ -20,7 +20,7 @@ func TestRevertTemplate(t *testing.T) { _ = Clean() err := GenTemplates(nil) assert.Nil(t, err) - fp, err := util.GetTemplateDir(category) + fp, err := pathx.GetTemplateDir(category) if err != nil { return } @@ -61,7 +61,7 @@ func TestClean(t *testing.T) { _ = Clean() err := GenTemplates(nil) assert.Nil(t, err) - fp, err := util.GetTemplateDir(category) + fp, err := pathx.GetTemplateDir(category) if err != nil { return } @@ -80,7 +80,7 @@ func TestUpdate(t *testing.T) { _ = Clean() err := GenTemplates(nil) assert.Nil(t, err) - fp, err := util.GetTemplateDir(category) + fp, err := pathx.GetTemplateDir(category) if err != nil { return } diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go index a3ecbe58..463fbc9c 100644 --- a/tools/goctl/tpl/templates.go +++ b/tools/goctl/tpl/templates.go @@ -14,7 +14,7 @@ import ( mongogen "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate" modelgen "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" rpcgen "github.com/tal-tech/go-zero/tools/goctl/rpc/generator" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/urfave/cli" ) @@ -24,7 +24,7 @@ const templateParentPath = "/" func GenTemplates(ctx *cli.Context) error { path := ctx.String("home") if len(path) != 0 { - util.RegisterGoctlHome(path) + pathx.RegisterGoctlHome(path) } if err := errorx.Chain( @@ -56,7 +56,7 @@ func GenTemplates(ctx *cli.Context) error { return err } - dir, err := util.GetTemplateDir(templateParentPath) + dir, err := pathx.GetTemplateDir(templateParentPath) if err != nil { return err } @@ -76,7 +76,7 @@ func GenTemplates(ctx *cli.Context) error { func CleanTemplates(ctx *cli.Context) error { path := ctx.String("home") if len(path) != 0 { - util.RegisterGoctlHome(path) + pathx.RegisterGoctlHome(path) } err := errorx.Chain( @@ -119,7 +119,7 @@ func UpdateTemplates(ctx *cli.Context) (err error) { path := ctx.String("home") category := ctx.String("category") if len(path) != 0 { - util.RegisterGoctlHome(path) + pathx.RegisterGoctlHome(path) } defer func() { @@ -156,7 +156,7 @@ func RevertTemplates(ctx *cli.Context) (err error) { category := ctx.String("category") filename := ctx.String("name") if len(path) != 0 { - util.RegisterGoctlHome(path) + pathx.RegisterGoctlHome(path) } defer func() { diff --git a/tools/goctl/update/update.go b/tools/goctl/update/update.go index a1b2d95e..4063aa30 100644 --- a/tools/goctl/update/update.go +++ b/tools/goctl/update/update.go @@ -10,7 +10,7 @@ import ( "github.com/tal-tech/go-zero/core/hash" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/tools/goctl/update/config" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const ( @@ -22,7 +22,7 @@ var configFile = flag.String("f", "etc/update-api.json", "the config file") func forChksumHandler(file string, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !util.FileExists(file) { + if !pathx.FileExists(file) { logx.Errorf("file %q not exist", file) http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable) return diff --git a/tools/goctl/util/ctx/gomod.go b/tools/goctl/util/ctx/gomod.go index 52b9563f..5e0cebc8 100644 --- a/tools/goctl/util/ctx/gomod.go +++ b/tools/goctl/util/ctx/gomod.go @@ -7,7 +7,7 @@ import ( "github.com/tal-tech/go-zero/core/jsonx" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) // Module contains the relative data of go module, @@ -31,7 +31,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) { return nil, err } - workDir, err := util.ReadLink(workDir) + workDir, err := pathx.ReadLink(workDir) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func projectFromGoMod(workDir string) (*ProjectContext, error) { var ret ProjectContext ret.WorkDir = workDir ret.Name = filepath.Base(m.Dir) - dir, err := util.ReadLink(m.Dir) + dir, err := pathx.ReadLink(m.Dir) if err != nil { return nil, err } diff --git a/tools/goctl/util/ctx/gomod_test.go b/tools/goctl/util/ctx/gomod_test.go index bd86642b..97818a75 100644 --- a/tools/goctl/util/ctx/gomod_test.go +++ b/tools/goctl/util/ctx/gomod_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/stringx" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestProjectFromGoMod(t *testing.T) { @@ -20,7 +20,7 @@ func TestProjectFromGoMod(t *testing.T) { } projectName := stringx.Rand() dir := filepath.Join(gp, "src", projectName) - err := util.MkdirIfNotExist(dir) + err := pathx.MkdirIfNotExist(dir) if err != nil { return } diff --git a/tools/goctl/util/ctx/gopath.go b/tools/goctl/util/ctx/gopath.go index 88959be4..8d0ad267 100644 --- a/tools/goctl/util/ctx/gopath.go +++ b/tools/goctl/util/ctx/gopath.go @@ -7,7 +7,7 @@ import ( "path/filepath" "strings" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) // projectFromGoPath is used to find the main module and project file path @@ -21,20 +21,20 @@ func projectFromGoPath(workDir string) (*ProjectContext, error) { return nil, err } - workDir, err := util.ReadLink(workDir) + workDir, err := pathx.ReadLink(workDir) if err != nil { return nil, err } buildContext := build.Default goPath := buildContext.GOPATH - goPath, err = util.ReadLink(goPath) + goPath, err = pathx.ReadLink(goPath) if err != nil { return nil, err } goSrc := filepath.Join(goPath, "src") - if !util.FileExists(goSrc) { + if !pathx.FileExists(goSrc) { return nil, errModuleCheck } diff --git a/tools/goctl/util/ctx/gopath_test.go b/tools/goctl/util/ctx/gopath_test.go index 1f0f0780..54f938bc 100644 --- a/tools/goctl/util/ctx/gopath_test.go +++ b/tools/goctl/util/ctx/gopath_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/stringx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestProjectFromGoPath(t *testing.T) { @@ -19,7 +19,7 @@ func TestProjectFromGoPath(t *testing.T) { } projectName := stringx.Rand() dir := filepath.Join(gp, "src", projectName) - err := util.MkdirIfNotExist(dir) + err := pathx.MkdirIfNotExist(dir) if err != nil { return } @@ -41,7 +41,7 @@ func TestProjectFromGoPathNotInGoSrc(t *testing.T) { } projectName := stringx.Rand() dir := filepath.Join(gp, "src", projectName) - err := util.MkdirIfNotExist(dir) + err := pathx.MkdirIfNotExist(dir) if err != nil { return } diff --git a/tools/goctl/util/ctx/modcheck_test.go b/tools/goctl/util/ctx/modcheck_test.go index b651af99..be5ec1c1 100644 --- a/tools/goctl/util/ctx/modcheck_test.go +++ b/tools/goctl/util/ctx/modcheck_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/tal-tech/go-zero/core/stringx" "github.com/tal-tech/go-zero/tools/goctl/rpc/execx" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func TestIsGoMod(t *testing.T) { @@ -21,7 +21,7 @@ func TestIsGoMod(t *testing.T) { } projectName := stringx.Rand() dir := filepath.Join(gp, "src", projectName) - err := util.MkdirIfNotExist(dir) + err := pathx.MkdirIfNotExist(dir) if err != nil { return } @@ -45,7 +45,7 @@ func TestIsGoModNot(t *testing.T) { } projectName := stringx.Rand() dir := filepath.Join(gp, "src", projectName) - err := util.MkdirIfNotExist(dir) + err := pathx.MkdirIfNotExist(dir) if err != nil { return } diff --git a/tools/goctl/util/env/env_test.go b/tools/goctl/util/env/env_test.go index 8de8e9c6..d634498f 100644 --- a/tools/goctl/util/env/env_test.go +++ b/tools/goctl/util/env/env_test.go @@ -9,7 +9,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/tal-tech/go-zero/tools/goctl/util" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -19,7 +19,7 @@ func TestLookUpGo(t *testing.T) { return } - assert.True(t, util.FileExists(xGo)) + assert.True(t, pathx.FileExists(xGo)) output, errOutput, err := execCommand(xGo, "version") if err != nil { return @@ -37,7 +37,7 @@ func TestLookUpProtoc(t *testing.T) { return } - assert.True(t, util.FileExists(xProtoc)) + assert.True(t, pathx.FileExists(xProtoc)) output, errOutput, err := execCommand(xProtoc, "--version") if err != nil { return @@ -54,7 +54,7 @@ func TestLookUpProtocGenGo(t *testing.T) { if err != nil { return } - assert.True(t, util.FileExists(xProtocGenGo)) + assert.True(t, pathx.FileExists(xProtocGenGo)) } func TestLookPath(t *testing.T) { @@ -62,7 +62,7 @@ func TestLookPath(t *testing.T) { if err != nil { return } - assert.True(t, util.FileExists(xGo)) + assert.True(t, pathx.FileExists(xGo)) } func TestCanExec(t *testing.T) { diff --git a/tools/goctl/util/git.go b/tools/goctl/util/git.go index ef457f9a..627a6ebf 100644 --- a/tools/goctl/util/git.go +++ b/tools/goctl/util/git.go @@ -9,10 +9,11 @@ import ( "strings" "github.com/tal-tech/go-zero/tools/goctl/util/env" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) func CloneIntoGitHome(url string) (dir string, err error) { - gitHome, err := GetGitHome() + gitHome, err := pathx.GetGitHome() if err != nil { return "", err } diff --git a/tools/goctl/util/file.go b/tools/goctl/util/pathx/file.go similarity index 99% rename from tools/goctl/util/file.go rename to tools/goctl/util/pathx/file.go index 4032f339..28e93558 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/pathx/file.go @@ -1,4 +1,4 @@ -package util +package pathx import ( "bufio" diff --git a/tools/goctl/util/file_test.go b/tools/goctl/util/pathx/file_test.go similarity index 99% rename from tools/goctl/util/file_test.go rename to tools/goctl/util/pathx/file_test.go index 77aa5a59..7d87a291 100644 --- a/tools/goctl/util/file_test.go +++ b/tools/goctl/util/pathx/file_test.go @@ -1,4 +1,4 @@ -package util +package pathx import ( "io/ioutil" diff --git a/tools/goctl/util/path.go b/tools/goctl/util/pathx/path.go similarity index 99% rename from tools/goctl/util/path.go rename to tools/goctl/util/pathx/path.go index 671c7544..80f8f3c7 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/pathx/path.go @@ -1,4 +1,4 @@ -package util +package pathx import ( "fmt" diff --git a/tools/goctl/util/path_test.go b/tools/goctl/util/pathx/path_test.go similarity index 97% rename from tools/goctl/util/path_test.go rename to tools/goctl/util/pathx/path_test.go index c3f103d7..67b5a4b5 100644 --- a/tools/goctl/util/path_test.go +++ b/tools/goctl/util/pathx/path_test.go @@ -1,4 +1,4 @@ -package util +package pathx import ( "io/ioutil" diff --git a/tools/goctl/util/readlink+polyfill.go b/tools/goctl/util/pathx/readlink+polyfill.go similarity index 88% rename from tools/goctl/util/readlink+polyfill.go rename to tools/goctl/util/pathx/readlink+polyfill.go index 4022dbd6..6ca6533d 100644 --- a/tools/goctl/util/readlink+polyfill.go +++ b/tools/goctl/util/pathx/readlink+polyfill.go @@ -1,7 +1,7 @@ //go:build windows // +build windows -package util +package pathx func ReadLink(name string) (string, error) { return name, nil diff --git a/tools/goctl/util/readlink.go b/tools/goctl/util/pathx/readlink.go similarity index 98% rename from tools/goctl/util/readlink.go rename to tools/goctl/util/pathx/readlink.go index 255b955f..e451de4c 100644 --- a/tools/goctl/util/readlink.go +++ b/tools/goctl/util/pathx/readlink.go @@ -1,7 +1,7 @@ //go:build linux || darwin // +build linux darwin -package util +package pathx import ( "os" diff --git a/tools/goctl/util/templatex.go b/tools/goctl/util/templatex.go index 33d2dc23..f1d4acea 100644 --- a/tools/goctl/util/templatex.go +++ b/tools/goctl/util/templatex.go @@ -7,6 +7,7 @@ import ( "text/template" "github.com/tal-tech/go-zero/tools/goctl/internal/errorx" + "github.com/tal-tech/go-zero/tools/goctl/util/pathx" ) const regularPerm = 0o666 @@ -39,7 +40,7 @@ func (t *DefaultTemplate) GoFmt(format bool) *DefaultTemplate { // SaveTo writes the codes to the target path func (t *DefaultTemplate) SaveTo(data interface{}, path string, forceUpdate bool) error { - if FileExists(path) && !forceUpdate { + if pathx.FileExists(path) && !forceUpdate { return nil }