|
|
@ -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
|
|
|
|
}
|
|
|
|
}
|