12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package configgen
- import (
- "errors"
- "fmt"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- "text/template"
- "github.com/logrusorgru/aurora"
- "github.com/tal-tech/go-zero/tools/goctl/util"
- "github.com/urfave/cli"
- )
- const configTemplate = `package main
- import (
- "io/ioutil"
- "os"
- "{{.import}}"
- "github.com/ghodss/yaml"
- )
- func main() {
- var c config.Config
- template, err := yaml.Marshal(c)
- if err != nil {
- panic(err)
- }
- err = ioutil.WriteFile("config.yaml", template, os.ModePerm)
- if err != nil {
- panic(err)
- }
- }
- `
- func GenConfigCommand(c *cli.Context) error {
- path, err := filepath.Abs(c.String("path"))
- if err != nil {
- return errors.New("abs failed: " + c.String("path"))
- }
- goModPath, found := util.FindGoModPath(path)
- if !found {
- return errors.New("go mod not initial")
- }
- path = strings.TrimSuffix(path, "/config.go")
- location := filepath.Join(path, "tmp")
- err = os.MkdirAll(location, os.ModePerm)
- if err != nil {
- return err
- }
- goPath := filepath.Join(location, "config.go")
- fp, err := os.Create(goPath)
- if err != nil {
- return err
- }
- defer fp.Close()
- defer os.RemoveAll(location)
- t := template.Must(template.New("template").Parse(configTemplate))
- if err := t.Execute(fp, map[string]string{
- "import": filepath.Dir(goModPath),
- }); err != nil {
- return err
- }
- 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 {
- 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
- }
|