浏览代码

fix config yaml gen (#25)

* optimized

* format

Co-authored-by: kingxt <dream4kingxt@163.com>
kingxt 4 年之前
父节点
当前提交
38806e7237
共有 4 个文件被更改,包括 65 次插入50 次删除
  1. 1 14
      tools/goctl/api/gogen/gen.go
  2. 2 24
      tools/goctl/api/gogen/util.go
  3. 24 11
      tools/goctl/configgen/genconfig.go
  4. 38 1
      tools/goctl/util/path.go

+ 1 - 14
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")

+ 2 - 24
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:]

+ 24 - 11
tools/goctl/configgen/genconfigjson.go → 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
 }

+ 38 - 1
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
+}