Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2bd95aa007 | ||
|
|
e8376936d5 |
@@ -5,10 +5,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// DefaultFormat defines a default naming style
|
||||||
// DefaultFormat defines a default naming style
|
const DefaultFormat = "gozero"
|
||||||
DefaultFormat = "gozero"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config defines the file naming style
|
// Config defines the file naming style
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
// GenConfigCommand provides the entry of goctl config
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/tools/goctl/api/new"
|
"github.com/tal-tech/go-zero/tools/goctl/api/new"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
|
"github.com/tal-tech/go-zero/tools/goctl/api/tsgen"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/api/validate"
|
"github.com/tal-tech/go-zero/tools/goctl/api/validate"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/configgen"
|
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/docker"
|
"github.com/tal-tech/go-zero/tools/goctl/docker"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
|
"github.com/tal-tech/go-zero/tools/goctl/internal/errorx"
|
||||||
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
|
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
|
||||||
@@ -569,17 +568,6 @@ var commands = []cli.Command{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Name: "config",
|
|
||||||
Usage: "generate config json",
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "path, p",
|
|
||||||
Usage: "the target config go file",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: configgen.GenConfigCommand,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Name: "template",
|
Name: "template",
|
||||||
Usage: "template operation",
|
Usage: "template operation",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// BuildVersion is the version of goctl.
|
// BuildVersion is the version of goctl.
|
||||||
const BuildVersion = "1.2.0"
|
const BuildVersion = "1.2.1"
|
||||||
|
|
||||||
// GetGoctlVersion returns BuildVersion
|
// GetGoctlVersion returns BuildVersion
|
||||||
func GetGoctlVersion() string {
|
func GetGoctlVersion() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user