feat: Add --remote (#1387)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
master
anqiansong 3 years ago committed by GitHub
parent 23deaf50e6
commit b8ea16a88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -52,7 +52,16 @@ func ApiCommand(c *cli.Context) error {
}
defer fp.Close()
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
}

@ -31,6 +31,13 @@ func GoCommand(c *cli.Context) error {
dir := c.String("dir")
namingStyle := c.String("style")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)

@ -65,6 +65,14 @@ func CreateServiceCommand(c *cli.Context) error {
defer fp.Close()
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
}

@ -44,6 +44,13 @@ func DockerCommand(c *cli.Context) (err error) {
goFile := c.String("go")
home := c.String("home")
version := c.String("version")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(version) > 0 {
version = version + "-"

@ -54,8 +54,15 @@ var commands = []cli.Command{
Usage: "the output api file",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: apigen.ApiCommand,
@ -66,8 +73,15 @@ var commands = []cli.Command{
Action: new.CreateServiceCommand,
Flags: []cli.Flag{
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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: "style",
@ -138,8 +152,15 @@ var commands = []cli.Command{
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",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: gogen.GoCommand,
@ -259,8 +280,15 @@ var commands = []cli.Command{
Value: 0,
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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: "version",
@ -352,8 +380,15 @@ var commands = []cli.Command{
Value: 10,
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: kube.DeploymentCommand,
@ -377,8 +412,15 @@ var commands = []cli.Command{
Usage: "whether the command execution environment is from idea plugin. [optional]",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: rpc.RPCNew,
@ -392,8 +434,15 @@ var commands = []cli.Command{
Usage: "the target path of proto",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
" if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: rpc.RPCTemplate,
@ -427,8 +476,15 @@ var commands = []cli.Command{
Usage: "whether the command execution environment is from idea plugin. [optional]",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: rpc.RPC,
@ -472,8 +528,15 @@ var commands = []cli.Command{
Usage: "the name of database [optional]",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: model.MysqlDDL,
@ -507,8 +570,15 @@ var commands = []cli.Command{
Usage: "for idea plugin [optional]",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: model.MySqlDataSource,
@ -552,8 +622,15 @@ var commands = []cli.Command{
Usage: "for idea plugin [optional]",
},
cli.StringFlag{
Name: "home",
Usage: "the goctl home path of the template",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time, " +
"if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: model.PostgreSqlDataSource,
@ -581,8 +658,15 @@ var commands = []cli.Command{
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",
Name: "home",
Usage: "the goctl home path of the template, --home and --remote cannot be set at the same time," +
" if they are, --remote has higher priority",
},
cli.StringFlag{
Name: "remote",
Usage: "the remote git repo of the template, --home and --remote cannot be set at the same time, " +
"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",
},
},
Action: mongo.Action,

@ -41,6 +41,13 @@ type Deployment struct {
func DeploymentCommand(c *cli.Context) error {
nodePort := c.Int("nodePort")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)

@ -18,7 +18,13 @@ func Action(ctx *cli.Context) error {
o := strings.TrimSpace(ctx.String("dir"))
s := ctx.String("style")
home := ctx.String("home")
remote := ctx.String("remote")
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
}

@ -42,7 +42,13 @@ func MysqlDDL(ctx *cli.Context) error {
style := ctx.String(flagStyle)
database := ctx.String(flagDatabase)
home := ctx.String(flagHome)
remote := ctx.String("remote")
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
}
@ -62,7 +68,13 @@ func MySqlDataSource(ctx *cli.Context) error {
idea := ctx.Bool(flagIdea)
style := ctx.String(flagStyle)
home := ctx.String("home")
remote := ctx.String("remote")
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
}
@ -85,7 +97,13 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
style := ctx.String(flagStyle)
schema := ctx.String(flagSchema)
home := ctx.String("home")
remote := ctx.String("remote")
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
}

@ -26,7 +26,13 @@ func RPC(c *cli.Context) error {
protoImportPath := c.StringSlice("proto_path")
goOptions := c.StringSlice("go_opt")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
}
@ -73,7 +79,13 @@ func RPCNew(c *cli.Context) error {
}
style := c.String("style")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
}
@ -102,7 +114,13 @@ func RPCNew(c *cli.Context) error {
func RPCTemplate(c *cli.Context) error {
protoFile := c.String("o")
home := c.String("home")
remote := c.String("remote")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote)
if len(repo) > 0 {
home = repo
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
}

@ -17,6 +17,7 @@ import (
const (
NL = "\n"
goctlDir = ".goctl"
gitDir = ".git"
)
var goctlHome string
@ -82,6 +83,16 @@ func GetGoctlHome() (string, error) {
return filepath.Join(home, goctlDir), nil
}
// GetGitHome returns the git home of goctl.
func GetGitHome() (string, error) {
goctlH, err := GetGoctlHome()
if err != nil {
return "", err
}
return filepath.Join(goctlH, gitDir), nil
}
// GetTemplateDir returns the category path value in GoctlHome where could get it by GetGoctlHome
func GetTemplateDir(category string) (string, error) {
goctlHome, err := GetGoctlHome()

@ -0,0 +1,23 @@
package util
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetGitHome(t *testing.T) {
homeDir, err := os.UserHomeDir()
if err != nil {
return
}
actual, err := GetGitHome()
if err != nil {
return
}
expected := filepath.Join(homeDir, goctlDir, gitDir)
assert.Equal(t, expected, actual)
}

@ -0,0 +1,36 @@
package util
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/util/env"
)
func CloneIntoGitHome(url string) (dir string, err error) {
gitHome, err := GetGitHome()
if err != nil {
return "", err
}
os.RemoveAll(gitHome)
ext := filepath.Ext(url)
repo := strings.TrimSuffix(filepath.Base(url), ext)
dir = filepath.Join(gitHome, repo)
path, err := env.LookPath("git")
if err != nil {
return "", err
}
if !env.CanExec() {
return "", fmt.Errorf("os %q can not call 'exec' command", runtime.GOOS)
}
cmd := exec.Command(path, "clone", url, dir)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
return
}
Loading…
Cancel
Save