From 38806e723796d8000f05dce6f61ca192d2d9405f Mon Sep 17 00:00:00 2001 From: kingxt Date: Thu, 27 Aug 2020 15:23:19 +0800 Subject: [PATCH] fix config yaml gen (#25) * optimized * format Co-authored-by: kingxt --- tools/goctl/api/gogen/gen.go | 15 +------ tools/goctl/api/gogen/util.go | 26 +------------ .../{genconfigjson.go => genconfig.go} | 35 +++++++++++------ tools/goctl/util/path.go | 39 ++++++++++++++++++- 4 files changed, 65 insertions(+), 50 deletions(-) rename tools/goctl/configgen/{genconfigjson.go => genconfig.go} (62%) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 95a2a00f..dcd2a160 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -140,20 +140,7 @@ func createGoModFileIfNeed(dir string) { panic(err) } - var tempPath = absDir - var hasGoMod = false - for { - if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) { - hasGoMod = true - break - } - - tempPath = filepath.Dir(tempPath) - if tempPath == filepath.Dir(tempPath) { - break - } - } - + _, hasGoMod := util.FindGoModPath(dir) if !hasGoMod { gopath := os.Getenv("GOPATH") parent := path.Join(gopath, "src") diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 1e8789b2..cbfabf31 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -15,8 +15,6 @@ import ( goctlutil "github.com/tal-tech/go-zero/tools/goctl/util" ) -const goModeIdentifier = "go.mod" - func getParentPackage(dir string) (string, error) { absDir, err := filepath.Abs(dir) if err != nil { @@ -24,27 +22,7 @@ func getParentPackage(dir string) (string, error) { } absDir = strings.ReplaceAll(absDir, `\`, `/`) - var rootPath string - var tempPath = absDir - var hasGoMod = false - for { - if goctlutil.FileExists(filepath.Join(tempPath, goModeIdentifier)) { - tempPath = filepath.Dir(tempPath) - rootPath = absDir[len(tempPath)+1:] - hasGoMod = true - break - } - - if tempPath == filepath.Dir(tempPath) { - break - } - - tempPath = filepath.Dir(tempPath) - if tempPath == string(filepath.Separator) { - break - } - } - + var rootPath, hasGoMod = goctlutil.FindGoModPath(dir) if hasGoMod { return rootPath, nil } @@ -54,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) - tempPath = filepath.Dir(absDir) + var tempPath = filepath.Dir(absDir) rootPath = absDir[len(tempPath)+1:] } else { rootPath = absDir[len(parent)+1:] diff --git a/tools/goctl/configgen/genconfigjson.go b/tools/goctl/configgen/genconfig.go similarity index 62% rename from tools/goctl/configgen/genconfigjson.go rename to tools/goctl/configgen/genconfig.go index 538c2d6d..6aae2bae 100644 --- a/tools/goctl/configgen/genconfigjson.go +++ b/tools/goctl/configgen/genconfig.go @@ -10,26 +10,27 @@ import ( "text/template" "github.com/logrusorgru/aurora" - "github.com/tal-tech/go-zero/tools/goctl/vars" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) const configTemplate = `package main import ( - "encoding/json" "io/ioutil" "os" "{{.import}}" + + "github.com/ghodss/yaml" ) func main() { var c config.Config - template, err := json.MarshalIndent(c, "", " ") + template, err := yaml.Marshal(c) if err != nil { panic(err) } - err = ioutil.WriteFile("config.json", template, os.ModePerm) + err = ioutil.WriteFile("config.yaml", template, os.ModePerm) if err != nil { panic(err) } @@ -41,9 +42,9 @@ func GenConfigCommand(c *cli.Context) error { if err != nil { return errors.New("abs failed: " + c.String("path")) } - xi := strings.Index(path, vars.ProjectName) - if xi <= 0 { - return errors.New("path should the absolute path of config go file") + goModPath, hasFound := util.FindGoModPath(path) + if !hasFound { + return errors.New("go mod not initial") } path = strings.TrimSuffix(path, "/config.go") location := path + "/tmp" @@ -62,16 +63,28 @@ func GenConfigCommand(c *cli.Context) error { t := template.Must(template.New("template").Parse(configTemplate)) if err := t.Execute(fp, map[string]string{ - "import": path[xi:], + "import": filepath.Dir(goModPath), }); err != nil { return err } - cmd := exec.Command("go", "run", goPath) - _, err = cmd.Output() + gen := exec.Command("go", "run", "config.go") + gen.Dir = filepath.Dir(goPath) + gen.Stderr = os.Stderr + gen.Stdout = os.Stdout + err = gen.Run() if err != nil { - return err + panic(err) + } + path, err = os.Getwd() + if err != nil { + panic(err) } + err = os.Rename(filepath.Dir(goPath)+"/config.yaml", path+"/config.yaml") + if err != nil { + panic(err) + } + fmt.Println(aurora.Green("Done.")) return nil } diff --git a/tools/goctl/util/path.go b/tools/goctl/util/path.go index 9c092688..55520b2c 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/path.go @@ -4,12 +4,16 @@ import ( "fmt" "os" "path" + "path/filepath" "strings" "github.com/tal-tech/go-zero/tools/goctl/vars" ) -const pkgSep = "/" +const ( + pkgSep = "/" + goModeIdentifier = "go.mod" +) func JoinPackages(pkgs ...string) string { return strings.Join(pkgs, pkgSep) @@ -43,3 +47,36 @@ func PathFromGoSrc() (string, error) { // skip slash return dir[len(parent)+1:], nil } + +func FindGoModPath(dir string) (string, bool) { + absDir, err := filepath.Abs(dir) + if err != nil { + return "", false + } + + absDir = strings.ReplaceAll(absDir, `\`, `/`) + var rootPath string + var tempPath = absDir + var hasGoMod = false + for { + if FileExists(filepath.Join(tempPath, goModeIdentifier)) { + tempPath = filepath.Dir(tempPath) + rootPath = absDir[len(tempPath)+1:] + hasGoMod = true + break + } + + if tempPath == filepath.Dir(tempPath) { + break + } + + tempPath = filepath.Dir(tempPath) + if tempPath == string(filepath.Separator) { + break + } + } + if hasGoMod { + return rootPath, true + } + return "", false +}