From 9f4a882a1b1417c179b85624e36a0c4b6061d4de Mon Sep 17 00:00:00 2001 From: anqiansong Date: Wed, 28 Jul 2021 16:32:15 +0800 Subject: [PATCH] fix issue #831 (#850) * fix issue #831 * fix typo Co-authored-by: anqiansong --- tools/goctl/api/gogen/gen.go | 4 ++ tools/goctl/docker/docker.go | 6 +++ tools/goctl/goctl.go | 64 ++++++++++++++++++++++-- tools/goctl/kube/kube.go | 6 +++ tools/goctl/model/mongo/mongo.go | 7 +++ tools/goctl/model/sql/command/command.go | 18 +++++++ tools/goctl/rpc/cli/cli.go | 13 +++++ tools/goctl/tpl/templates.go | 30 ++++++++++- tools/goctl/util/file.go | 10 ++++ 9 files changed, 152 insertions(+), 6 deletions(-) diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 07f8ffd8..18a6136b 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -30,7 +30,11 @@ func GoCommand(c *cli.Context) error { apiFile := c.String("api") dir := c.String("dir") namingStyle := c.String("style") + home := c.String("home") + if len(home) > 0 { + util.RegisterGoctlHome(home) + } if len(apiFile) == 0 { return errors.New("missing -api") } diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index 32d6dbf3..2d36583f 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -42,6 +42,12 @@ func DockerCommand(c *cli.Context) (err error) { }() goFile := c.String("go") + home := c.String("home") + + if len(home) > 0 { + util.RegisterGoctlHome(home) + } + if len(goFile) == 0 { return errors.New("-go can't be empty") } diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 1dd3c335..b33d4d74 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -117,6 +117,10 @@ var ( Name: "style", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: gogen.GoCommand, }, @@ -234,6 +238,10 @@ var ( Usage: "the port to expose, default none", Value: 0, }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: docker.DockerCommand, }, @@ -319,6 +327,10 @@ var ( Usage: "the max replicas of deploy", Value: 10, }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: kube.DeploymentCommand, }, @@ -340,6 +352,10 @@ var ( Name: "idea", Usage: "whether the command execution environment is from idea plugin. [optional]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: rpc.RPCNew, }, @@ -382,6 +398,10 @@ var ( Name: "idea", Usage: "whether the command execution environment is from idea plugin. [optional]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: rpc.RPC, }, @@ -423,6 +443,10 @@ var ( Name: "database, db", Usage: "the name of database [optional]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: model.MysqlDDL, }, @@ -454,6 +478,10 @@ var ( Name: "idea", Usage: "for idea plugin [optional]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: model.MySqlDataSource, }, @@ -495,6 +523,10 @@ var ( Name: "idea", Usage: "for idea plugin [optional]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: model.PostgreSqlDataSource, }, @@ -520,6 +552,10 @@ var ( Name: "style", Usage: "the file naming format, see [https://github.com/tal-tech/go-zero/tree/master/tools/goctl/config/readme.md]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: mongo.Action, }, @@ -541,13 +577,25 @@ var ( Usage: "template operation", Subcommands: []cli.Command{ { - Name: "init", - Usage: "initialize the all templates(force update)", + Name: "init", + Usage: "initialize the all templates(force update)", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, + }, Action: tpl.GenTemplates, }, { - Name: "clean", - Usage: "clean the all cache templates", + Name: "clean", + Usage: "clean the all cache templates", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, + }, Action: tpl.CleanTemplates, }, { @@ -558,6 +606,10 @@ var ( Name: "category,c", Usage: "the category of template, enum [api,rpc,model,docker,kube]", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: tpl.UpdateTemplates, }, @@ -573,6 +625,10 @@ var ( Name: "name,n", Usage: "the target file name of template", }, + cli.StringFlag{ + Name: "home", + Usage: "the goctl home path of the template", + }, }, Action: tpl.RevertTemplates, }, diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index 9417031c..dccba392 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -40,6 +40,12 @@ type Deployment struct { // DeploymentCommand is used to generate the kubernetes deployment yaml files. func DeploymentCommand(c *cli.Context) error { nodePort := c.Int("nodePort") + home := c.String("home") + + if len(home) > 0 { + util.RegisterGoctlHome(home) + } + // 0 to disable the nodePort type if nodePort != 0 && (nodePort < basePort || nodePort > portLimit) { return errors.New("nodePort should be between 30000 and 32767") diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index b22d1264..3f77acd9 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -7,6 +7,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/config" "github.com/tal-tech/go-zero/tools/goctl/model/mongo/generate" + file "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) @@ -16,6 +17,12 @@ func Action(ctx *cli.Context) error { c := ctx.Bool("cache") o := strings.TrimSpace(ctx.String("dir")) s := ctx.String("style") + home := ctx.String("home") + + if len(home) > 0 { + file.RegisterGoctlHome(home) + } + if len(tp) == 0 { return errors.New("missing type") } diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index 282b6989..e56e0322 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -13,6 +13,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/model/sql/gen" "github.com/tal-tech/go-zero/tools/goctl/model/sql/model" "github.com/tal-tech/go-zero/tools/goctl/model/sql/util" + file "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/console" "github.com/urfave/cli" ) @@ -39,6 +40,11 @@ func MysqlDDL(ctx *cli.Context) error { idea := ctx.Bool(flagIdea) style := ctx.String(flagStyle) database := ctx.String(flagDatabase) + home := ctx.String("home") + + if len(home) > 0 { + file.RegisterGoctlHome(home) + } cfg, err := config.NewConfig(style) if err != nil { return err @@ -54,6 +60,12 @@ func MySqlDataSource(ctx *cli.Context) error { cache := ctx.Bool(flagCache) idea := ctx.Bool(flagIdea) style := ctx.String(flagStyle) + home := ctx.String("home") + + if len(home) > 0 { + file.RegisterGoctlHome(home) + } + pattern := strings.TrimSpace(ctx.String(flagTable)) cfg, err := config.NewConfig(style) if err != nil { @@ -71,6 +83,12 @@ func PostgreSqlDataSource(ctx *cli.Context) error { idea := ctx.Bool(flagIdea) style := ctx.String(flagStyle) schema := ctx.String(flagSchema) + home := ctx.String("home") + + if len(home) > 0 { + file.RegisterGoctlHome(home) + } + if len(schema) == 0 { schema = "public" } diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index df4d3570..690ba4cd 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/tal-tech/go-zero/tools/goctl/rpc/generator" + "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/urfave/cli" ) @@ -18,9 +19,16 @@ func RPC(c *cli.Context) error { style := c.String("style") protoImportPath := c.StringSlice("proto_path") goOptions := c.StringSlice("go_opt") + home := c.String("home") + + if len(home) > 0 { + util.RegisterGoctlHome(home) + } + if len(src) == 0 { return errors.New("missing -src") } + if len(out) == 0 { return errors.New("missing -dir") } @@ -42,6 +50,11 @@ func RPCNew(c *cli.Context) error { return fmt.Errorf("unexpected ext: %s", ext) } style := c.String("style") + home := c.String("home") + + if len(home) > 0 { + util.RegisterGoctlHome(home) + } protoName := rpcname + ".proto" filename := filepath.Join(".", rpcname, protoName) diff --git a/tools/goctl/tpl/templates.go b/tools/goctl/tpl/templates.go index 48cba9df..ce6c4e97 100644 --- a/tools/goctl/tpl/templates.go +++ b/tools/goctl/tpl/templates.go @@ -2,6 +2,7 @@ package tpl import ( "fmt" + "path/filepath" "github.com/logrusorgru/aurora" "github.com/tal-tech/go-zero/core/errorx" @@ -19,6 +20,11 @@ const templateParentPath = "/" // GenTemplates writes the latest template text into file which is not exists func GenTemplates(ctx *cli.Context) error { + path := ctx.String("home") + if len(path) != 0 { + util.RegisterGoctlHome(path) + } + if err := errorx.Chain( func() error { return gogen.GenTemplates(ctx) @@ -47,14 +53,24 @@ func GenTemplates(ctx *cli.Context) error { return err } - fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(dir), + abs, err := filepath.Abs(dir) + if err != nil { + return err + } + + fmt.Printf("Templates are generated in %s, %s\n", aurora.Green(abs), aurora.Red("edit on your risk!")) return nil } // CleanTemplates deletes all templates -func CleanTemplates(_ *cli.Context) error { +func CleanTemplates(ctx *cli.Context) error { + path := ctx.String("home") + if len(path) != 0 { + util.RegisterGoctlHome(path) + } + err := errorx.Chain( func() error { return gogen.Clean() @@ -86,7 +102,12 @@ func CleanTemplates(_ *cli.Context) error { // UpdateTemplates writes the latest template text into file, // it will delete the older templates if there are exists func UpdateTemplates(ctx *cli.Context) (err error) { + path := ctx.String("home") category := ctx.String("category") + if len(path) != 0 { + util.RegisterGoctlHome(path) + } + defer func() { if err == nil { fmt.Println(aurora.Green(fmt.Sprintf("%s template are update!", category)).String()) @@ -113,8 +134,13 @@ func UpdateTemplates(ctx *cli.Context) (err error) { // RevertTemplates will overwrite the old template content with the new template func RevertTemplates(ctx *cli.Context) (err error) { + path := ctx.String("home") category := ctx.String("category") filename := ctx.String("name") + if len(path) != 0 { + util.RegisterGoctlHome(path) + } + defer func() { if err == nil { fmt.Println(aurora.Green(fmt.Sprintf("%s template are reverted!", filename)).String()) diff --git a/tools/goctl/util/file.go b/tools/goctl/util/file.go index cb820a6a..d8320e15 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/file.go @@ -17,6 +17,12 @@ const ( goctlDir = ".goctl" ) +var goctlHome string + +func RegisterGoctlHome(home string) { + goctlHome = home +} + // CreateIfNotExist creates a file if it is not exists func CreateIfNotExist(file string) (*os.File, error) { _, err := os.Stat(file) @@ -62,6 +68,10 @@ func FileNameWithoutExt(file string) string { // GetGoctlHome returns the path value of the goctl home where Join $HOME with .goctl func GetGoctlHome() (string, error) { + if len(goctlHome) != 0 { + return goctlHome, nil + } + home, err := os.UserHomeDir() if err != nil { return "", err