diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index cbfabf31..5b21e9d6 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -22,7 +22,7 @@ func getParentPackage(dir string) (string, error) { } absDir = strings.ReplaceAll(absDir, `\`, `/`) - var rootPath, hasGoMod = goctlutil.FindGoModPath(dir) + rootPath, hasGoMod := goctlutil.FindGoModPath(dir) if hasGoMod { return rootPath, nil } @@ -32,7 +32,7 @@ func getParentPackage(dir string) (string, error) { pos := strings.Index(absDir, parent) if pos < 0 { fmt.Printf("%s not in go.mod project path, or not in GOPATH of %s directory\n", absDir, gopath) - var tempPath = filepath.Dir(absDir) + tempPath := filepath.Dir(absDir) rootPath = absDir[len(tempPath)+1:] } else { rootPath = absDir[len(parent)+1:] @@ -61,7 +61,7 @@ func writeProperty(writer io.Writer, name, tp, tag, comment string, indent int) } func getAuths(api *spec.ApiSpec) []string { - var authNames = collection.NewSet() + authNames := collection.NewSet() for _, g := range api.Service.Groups { if value, ok := util.GetAnnotationValue(g.Annotations, "server", "jwt"); ok { authNames.Add(value) @@ -78,5 +78,6 @@ func formatCode(code string) string { if err != nil { return code } + return string(ret) } diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index f0815367..f3098fda 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -9,14 +9,9 @@ import ( func DockerCommand(c *cli.Context) error { goFile := c.String("go") - namespace := c.String("namespace") - if len(goFile) == 0 || len(namespace) == 0 { - return errors.New("-go and -namespace can't be empty") + if len(goFile) == 0 { + return errors.New("-go can't be empty") } - if err := gen.GenerateDockerfile(goFile, "-f", "etc/config.json"); err != nil { - return err - } - - return gen.GenerateMakefile(goFile, namespace) + return gen.GenerateDockerfile(goFile, "-f", "etc/config.yaml") } diff --git a/tools/goctl/gen/dockerfile.go b/tools/goctl/gen/dockerfile.go index a8616381..1aa6fe3c 100644 --- a/tools/goctl/gen/dockerfile.go +++ b/tools/goctl/gen/dockerfile.go @@ -9,7 +9,7 @@ import ( ) func GenerateDockerfile(goFile string, args ...string) error { - relPath, err := util.PathFromGoSrc() + projPath, err := getFilePath(goFile) if err != nil { return err } @@ -28,7 +28,7 @@ func GenerateDockerfile(goFile string, args ...string) error { t := template.Must(template.New("dockerfile").Parse(dockerTemplate)) return t.Execute(out, map[string]string{ "projectName": vars.ProjectName, - "goRelPath": relPath, + "goRelPath": projPath, "goFile": goFile, "exeFile": util.FileNameWithoutExt(goFile), "argument": builder.String(), diff --git a/tools/goctl/gen/filepath.go b/tools/goctl/gen/filepath.go new file mode 100644 index 00000000..79ec2149 --- /dev/null +++ b/tools/goctl/gen/filepath.go @@ -0,0 +1,26 @@ +package gen + +import ( + "errors" + "os" + "path/filepath" + + "github.com/tal-tech/go-zero/tools/goctl/util" +) + +func getFilePath(file string) (string, error) { + wd, err := os.Getwd() + if err != nil { + return "", err + } + + projPath, ok := util.FindGoModPath(filepath.Join(wd, file)) + if !ok { + projPath, err = util.PathFromGoSrc() + if err != nil { + return "", errors.New("no go.mod found, or not in GOPATH") + } + } + + return projPath, nil +} diff --git a/tools/goctl/gen/makefile.go b/tools/goctl/gen/makefile.go deleted file mode 100644 index 9bc014f9..00000000 --- a/tools/goctl/gen/makefile.go +++ /dev/null @@ -1,52 +0,0 @@ -package gen - -import ( - "strings" - "text/template" - - "github.com/tal-tech/go-zero/tools/goctl/util" -) - -func GenerateMakefile(goFile, namespace string) error { - relPath, err := util.PathFromGoSrc() - if err != nil { - return err - } - - movePath, err := getMovePath() - if err != nil { - return err - } - - out, err := util.CreateIfNotExist("Makefile") - if err != nil { - return err - } - defer out.Close() - - t := template.Must(template.New("makefile").Parse(makefileTemplate)) - return t.Execute(out, map[string]string{ - "rootRelPath": movePath, - "relPath": relPath, - "exeFile": util.FileNameWithoutExt(goFile), - "namespace": namespace, - }) -} - -func getMovePath() (string, error) { - relPath, err := util.PathFromGoSrc() - if err != nil { - return "", err - } - - var builder strings.Builder - for range strings.Split(relPath, "/") { - builder.WriteString("../") - } - - if move := builder.String(); len(move) == 0 { - return ".", nil - } else { - return move, nil - } -}