mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
fix config yaml gen (#25)
* optimized * format Co-authored-by: kingxt <dream4kingxt@163.com>
This commit is contained in:
parent
a987d12237
commit
38806e7237
@ -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")
|
||||
|
@ -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:]
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user