fix config yaml gen (#25)

* optimized

* format

Co-authored-by: kingxt <dream4kingxt@163.com>
master
kingxt 4 years ago committed by GitHub
parent a987d12237
commit 38806e7237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -140,20 +140,7 @@ func createGoModFileIfNeed(dir string) {
panic(err) panic(err)
} }
var tempPath = absDir _, hasGoMod := util.FindGoModPath(dir)
var hasGoMod = false
for {
if util.FileExists(filepath.Join(tempPath, goModeIdentifier)) {
hasGoMod = true
break
}
tempPath = filepath.Dir(tempPath)
if tempPath == filepath.Dir(tempPath) {
break
}
}
if !hasGoMod { if !hasGoMod {
gopath := os.Getenv("GOPATH") gopath := os.Getenv("GOPATH")
parent := path.Join(gopath, "src") parent := path.Join(gopath, "src")

@ -15,8 +15,6 @@ import (
goctlutil "github.com/tal-tech/go-zero/tools/goctl/util" goctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
) )
const goModeIdentifier = "go.mod"
func getParentPackage(dir string) (string, error) { func getParentPackage(dir string) (string, error) {
absDir, err := filepath.Abs(dir) absDir, err := filepath.Abs(dir)
if err != nil { if err != nil {
@ -24,27 +22,7 @@ func getParentPackage(dir string) (string, error) {
} }
absDir = strings.ReplaceAll(absDir, `\`, `/`) absDir = strings.ReplaceAll(absDir, `\`, `/`)
var rootPath string var rootPath, hasGoMod = goctlutil.FindGoModPath(dir)
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
}
}
if hasGoMod { if hasGoMod {
return rootPath, nil return rootPath, nil
} }
@ -54,7 +32,7 @@ func getParentPackage(dir string) (string, error) {
pos := strings.Index(absDir, parent) pos := strings.Index(absDir, parent)
if pos < 0 { if pos < 0 {
fmt.Printf("%s not in go.mod project path, or not in GOPATH of %s directory\n", absDir, gopath) 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:] rootPath = absDir[len(tempPath)+1:]
} else { } else {
rootPath = absDir[len(parent)+1:] rootPath = absDir[len(parent)+1:]

@ -10,26 +10,27 @@ import (
"text/template" "text/template"
"github.com/logrusorgru/aurora" "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" "github.com/urfave/cli"
) )
const configTemplate = `package main const configTemplate = `package main
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"{{.import}}" "{{.import}}"
"github.com/ghodss/yaml"
) )
func main() { func main() {
var c config.Config var c config.Config
template, err := json.MarshalIndent(c, "", " ") template, err := yaml.Marshal(c)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = ioutil.WriteFile("config.json", template, os.ModePerm) err = ioutil.WriteFile("config.yaml", template, os.ModePerm)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -41,9 +42,9 @@ func GenConfigCommand(c *cli.Context) error {
if err != nil { if err != nil {
return errors.New("abs failed: " + c.String("path")) return errors.New("abs failed: " + c.String("path"))
} }
xi := strings.Index(path, vars.ProjectName) goModPath, hasFound := util.FindGoModPath(path)
if xi <= 0 { if !hasFound {
return errors.New("path should the absolute path of config go file") return errors.New("go mod not initial")
} }
path = strings.TrimSuffix(path, "/config.go") path = strings.TrimSuffix(path, "/config.go")
location := path + "/tmp" location := path + "/tmp"
@ -62,16 +63,28 @@ func GenConfigCommand(c *cli.Context) error {
t := template.Must(template.New("template").Parse(configTemplate)) t := template.Must(template.New("template").Parse(configTemplate))
if err := t.Execute(fp, map[string]string{ if err := t.Execute(fp, map[string]string{
"import": path[xi:], "import": filepath.Dir(goModPath),
}); err != nil { }); err != nil {
return err return err
} }
cmd := exec.Command("go", "run", goPath) gen := exec.Command("go", "run", "config.go")
_, err = cmd.Output() gen.Dir = filepath.Dir(goPath)
gen.Stderr = os.Stderr
gen.Stdout = os.Stdout
err = gen.Run()
if err != nil { 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.")) fmt.Println(aurora.Green("Done."))
return nil return nil
} }

@ -4,12 +4,16 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"github.com/tal-tech/go-zero/tools/goctl/vars" "github.com/tal-tech/go-zero/tools/goctl/vars"
) )
const pkgSep = "/" const (
pkgSep = "/"
goModeIdentifier = "go.mod"
)
func JoinPackages(pkgs ...string) string { func JoinPackages(pkgs ...string) string {
return strings.Join(pkgs, pkgSep) return strings.Join(pkgs, pkgSep)
@ -43,3 +47,36 @@ func PathFromGoSrc() (string, error) {
// skip slash // skip slash
return dir[len(parent)+1:], nil 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…
Cancel
Save