From 68a81fea8ae652b7e05e093a2c25a9361559c753 Mon Sep 17 00:00:00 2001 From: anqiansong Date: Sat, 5 Mar 2022 22:52:32 +0800 Subject: [PATCH] Fix #1609 (#1617) --- tools/goctl/api/apigen/gen.go | 3 +- tools/goctl/api/gogen/gen.go | 3 +- tools/goctl/api/new/newservice.go | 3 +- tools/goctl/docker/docker.go | 3 +- tools/goctl/goctl.go | 52 ++++++++++++++++++++++++ tools/goctl/kube/kube.go | 3 +- tools/goctl/model/mongo/mongo.go | 3 +- tools/goctl/model/sql/command/command.go | 21 ++++++---- tools/goctl/rpc/cli/cli.go | 9 ++-- tools/goctl/rpc/cli/zrpc.go | 3 +- tools/goctl/util/git.go | 12 +++++- 11 files changed, 95 insertions(+), 20 deletions(-) diff --git a/tools/goctl/api/apigen/gen.go b/tools/goctl/api/apigen/gen.go index 50cb3d78..afdba15e 100644 --- a/tools/goctl/api/apigen/gen.go +++ b/tools/goctl/api/apigen/gen.go @@ -55,8 +55,9 @@ func ApiCommand(c *cli.Context) error { home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 2d3a0f6a..5b767750 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -33,8 +33,9 @@ func GoCommand(c *cli.Context) error { namingStyle := c.String("style") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/api/new/newservice.go b/tools/goctl/api/new/newservice.go index f978aeec..83b6fd20 100644 --- a/tools/goctl/api/new/newservice.go +++ b/tools/goctl/api/new/newservice.go @@ -67,8 +67,9 @@ func CreateServiceCommand(c *cli.Context) error { home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/docker/docker.go b/tools/goctl/docker/docker.go index e17165a7..e4ac358a 100644 --- a/tools/goctl/docker/docker.go +++ b/tools/goctl/docker/docker.go @@ -46,8 +46,9 @@ func DockerCommand(c *cli.Context) (err error) { home := c.String("home") version := c.String("version") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index f9af586a..c5cefa0c 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -111,6 +111,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: apigen.ApiCommand, Subcommands: []cli.Command{ @@ -130,6 +134,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, cli.StringFlag{ Name: "style", Usage: "the file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]", @@ -209,6 +217,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: gogen.GoCommand, }, @@ -345,6 +357,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, cli.StringFlag{ Name: "version", Usage: "the goctl builder golang image version", @@ -445,6 +461,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, cli.StringFlag{ Name: "serviceAccount", Usage: "the ServiceAccount for the deployment", @@ -482,6 +502,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: rpc.RPCNew, }, @@ -504,6 +528,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: rpc.RPCTemplate, }, @@ -548,6 +576,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, }, { @@ -590,6 +622,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: rpc.RPC, }, @@ -642,6 +678,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: model.MysqlDDL, }, @@ -684,6 +724,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: model.MySqlDataSource, }, @@ -736,6 +780,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: model.PostgreSqlDataSource, }, @@ -772,6 +820,10 @@ var commands = []cli.Command{ "if they are, --remote has higher priority\n\tThe git repo directory must be consistent with the " + "https://github.com/zeromicro/go-zero-template directory structure", }, + cli.StringFlag{ + Name: "branch", + Usage: "the branch of the remote repo, it does work with --remote", + }, }, Action: mongo.Action, }, diff --git a/tools/goctl/kube/kube.go b/tools/goctl/kube/kube.go index adf97147..79575bff 100644 --- a/tools/goctl/kube/kube.go +++ b/tools/goctl/kube/kube.go @@ -44,8 +44,9 @@ func DeploymentCommand(c *cli.Context) error { nodePort := c.Int("nodePort") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index 6d807476..861c804f 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -20,8 +20,9 @@ func Action(ctx *cli.Context) error { s := ctx.String("style") home := ctx.String("home") remote := ctx.String("remote") + branch := ctx.String("branch") if len(remote) > 0 { - repo, _ := file.CloneIntoGitHome(remote) + repo, _ := file.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/model/sql/command/command.go b/tools/goctl/model/sql/command/command.go index f313072e..6e484c6d 100644 --- a/tools/goctl/model/sql/command/command.go +++ b/tools/goctl/model/sql/command/command.go @@ -30,6 +30,8 @@ const ( flagDatabase = "database" flagSchema = "schema" flagHome = "home" + flagRemote = "remote" + flagBranch = "branch" ) var errNotMatched = errors.New("sql not matched") @@ -43,9 +45,10 @@ func MysqlDDL(ctx *cli.Context) error { style := ctx.String(flagStyle) database := ctx.String(flagDatabase) home := ctx.String(flagHome) - remote := ctx.String("remote") + remote := ctx.String(flagRemote) + branch := ctx.String(flagBranch) if len(remote) > 0 { - repo, _ := file.CloneIntoGitHome(remote) + repo, _ := file.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } @@ -68,10 +71,11 @@ func MySqlDataSource(ctx *cli.Context) error { cache := ctx.Bool(flagCache) idea := ctx.Bool(flagIdea) style := ctx.String(flagStyle) - home := ctx.String("home") - remote := ctx.String("remote") + home := ctx.String(flagHome) + remote := ctx.String(flagRemote) + branch := ctx.String(flagBranch) if len(remote) > 0 { - repo, _ := file.CloneIntoGitHome(remote) + repo, _ := file.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } @@ -97,10 +101,11 @@ func PostgreSqlDataSource(ctx *cli.Context) error { idea := ctx.Bool(flagIdea) style := ctx.String(flagStyle) schema := ctx.String(flagSchema) - home := ctx.String("home") - remote := ctx.String("remote") + home := ctx.String(flagHome) + remote := ctx.String(flagRemote) + branch := ctx.String(flagBranch) if len(remote) > 0 { - repo, _ := file.CloneIntoGitHome(remote) + repo, _ := file.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/rpc/cli/cli.go b/tools/goctl/rpc/cli/cli.go index a139056d..0229ff6a 100644 --- a/tools/goctl/rpc/cli/cli.go +++ b/tools/goctl/rpc/cli/cli.go @@ -33,8 +33,9 @@ func RPC(c *cli.Context) error { goOptions := c.StringSlice("go_opt") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } @@ -89,8 +90,9 @@ func RPCNew(c *cli.Context) error { style := c.String("style") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } @@ -124,8 +126,9 @@ func RPCTemplate(c *cli.Context) error { protoFile := c.String("o") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/rpc/cli/zrpc.go b/tools/goctl/rpc/cli/zrpc.go index f48bd87e..19c10a46 100644 --- a/tools/goctl/rpc/cli/zrpc.go +++ b/tools/goctl/rpc/cli/zrpc.go @@ -54,8 +54,9 @@ func ZRPC(c *cli.Context) error { style := c.String("style") home := c.String("home") remote := c.String("remote") + branch := c.String("branch") if len(remote) > 0 { - repo, _ := util.CloneIntoGitHome(remote) + repo, _ := util.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } diff --git a/tools/goctl/util/git.go b/tools/goctl/util/git.go index 0db1f5e6..5eed43e5 100644 --- a/tools/goctl/util/git.go +++ b/tools/goctl/util/git.go @@ -12,7 +12,7 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) -func CloneIntoGitHome(url string) (dir string, err error) { +func CloneIntoGitHome(url string, branch string) (dir string, err error) { gitHome, err := pathx.GetGitHome() if err != nil { return "", err @@ -21,6 +21,9 @@ func CloneIntoGitHome(url string) (dir string, err error) { ext := filepath.Ext(url) repo := strings.TrimSuffix(filepath.Base(url), ext) dir = filepath.Join(gitHome, repo) + if pathx.FileExists(dir) { + os.RemoveAll(dir) + } path, err := env.LookPath("git") if err != nil { return "", err @@ -28,7 +31,12 @@ func CloneIntoGitHome(url string) (dir string, err error) { if !env.CanExec() { return "", fmt.Errorf("os %q can not call 'exec' command", runtime.GOOS) } - cmd := exec.Command(path, "clone", url, dir) + args := []string{"clone"} + if len(branch) > 0 { + args = append(args, "-b", branch) + } + args = append(args, url, dir) + cmd := exec.Command(path, args...) cmd.Env = os.Environ() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr