Skip to content

Commit 1904af2

Browse files
authored
feat(goctl): Support gateway sample generation (zeromicro#3049)
1 parent 95b8533 commit 1904af2

32 files changed

Lines changed: 1410 additions & 524 deletions

File tree

‎tools/goctl/api/cmd.go‎

Lines changed: 77 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -13,166 +13,91 @@ import (
1313
"github.com/zeromicro/go-zero/tools/goctl/api/tsgen"
1414
"github.com/zeromicro/go-zero/tools/goctl/api/validate"
1515
"github.com/zeromicro/go-zero/tools/goctl/config"
16+
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
1617
"github.com/zeromicro/go-zero/tools/goctl/plugin"
1718
)
1819

1920
var (
2021
// Cmd describes an api command.
21-
Cmd = &cobra.Command{
22-
Use: "api",
23-
Short: "Generate api related files",
24-
RunE: apigen.CreateApiTemplate,
25-
}
26-
27-
dartCmd = &cobra.Command{
28-
Use: "dart",
29-
Short: "Generate dart files for provided api in api file",
30-
RunE: dartgen.DartCommand,
31-
}
32-
33-
docCmd = &cobra.Command{
34-
Use: "doc",
35-
Short: "Generate doc files",
36-
RunE: docgen.DocCommand,
37-
}
38-
39-
formatCmd = &cobra.Command{
40-
Use: "format",
41-
Short: "Format api files",
42-
RunE: format.GoFormatApi,
43-
}
44-
45-
goCmd = &cobra.Command{
46-
Use: "go",
47-
Short: "Generate go files for provided api in api file",
48-
RunE: gogen.GoCommand,
49-
}
50-
51-
newCmd = &cobra.Command{
52-
Use: "new",
53-
Short: "Fast create api service",
54-
Example: "goctl api new [options] service-name",
55-
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
56-
RunE: func(cmd *cobra.Command, args []string) error {
57-
return new.CreateServiceCommand(args)
58-
},
59-
}
60-
61-
validateCmd = &cobra.Command{
62-
Use: "validate",
63-
Short: "Validate api file",
64-
RunE: validate.GoValidateApi,
65-
}
66-
67-
javaCmd = &cobra.Command{
68-
Use: "java",
69-
Short: "Generate java files for provided api in api file",
70-
Hidden: true,
71-
RunE: javagen.JavaCommand,
72-
}
73-
74-
ktCmd = &cobra.Command{
75-
Use: "kt",
76-
Short: "Generate kotlin code for provided api file",
77-
RunE: ktgen.KtCommand,
78-
}
79-
80-
pluginCmd = &cobra.Command{
81-
Use: "plugin",
82-
Short: "Custom file generator",
83-
RunE: plugin.PluginCommand,
84-
}
85-
86-
tsCmd = &cobra.Command{
87-
Use: "ts",
88-
Short: "Generate ts files for provided api in api file",
89-
RunE: tsgen.TsCommand,
90-
}
22+
Cmd = cobrax.NewCommand("api", cobrax.WithRunE(apigen.CreateApiTemplate))
23+
dartCmd = cobrax.NewCommand("dart", cobrax.WithRunE(dartgen.DartCommand))
24+
docCmd = cobrax.NewCommand("doc", cobrax.WithRunE(docgen.DocCommand))
25+
formatCmd = cobrax.NewCommand("format", cobrax.WithRunE(format.GoFormatApi))
26+
goCmd = cobrax.NewCommand("go", cobrax.WithRunE(gogen.GoCommand))
27+
newCmd = cobrax.NewCommand("new", cobrax.WithRunE(new.CreateServiceCommand),
28+
cobrax.WithArgs(cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)))
29+
validateCmd = cobrax.NewCommand("validate", cobrax.WithRunE(validate.GoValidateApi))
30+
javaCmd = cobrax.NewCommand("java", cobrax.WithRunE(javagen.JavaCommand), cobrax.WithHidden())
31+
ktCmd = cobrax.NewCommand("kt", cobrax.WithRunE(ktgen.KtCommand))
32+
pluginCmd = cobrax.NewCommand("plugin", cobrax.WithRunE(plugin.PluginCommand))
33+
tsCmd = cobrax.NewCommand("ts", cobrax.WithRunE(tsgen.TsCommand))
9134
)
9235

9336
func init() {
94-
Cmd.Flags().StringVar(&apigen.VarStringOutput, "o", "", "Output a sample api file")
95-
Cmd.Flags().StringVar(&apigen.VarStringHome, "home", "", "The goctl home path of the"+
96-
" template, --home and --remote cannot be set at the same time, if they are, --remote has "+
97-
"higher priority")
98-
Cmd.Flags().StringVar(&apigen.VarStringRemote, "remote", "", "The remote git repo of the"+
99-
" template, --home and --remote cannot be set at the same time, if they are, --remote has higher"+
100-
" priority\nThe git repo directory must be consistent with the"+
101-
" https://github.com/zeromicro/go-zero-template directory structure")
102-
Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+
103-
"remote repo, it does work with --remote")
104-
105-
dartCmd.Flags().StringVar(&dartgen.VarStringDir, "dir", "", "The target dir")
106-
dartCmd.Flags().StringVar(&dartgen.VarStringAPI, "api", "", "The api file")
107-
dartCmd.Flags().BoolVar(&dartgen.VarStringLegacy, "legacy", false, "Legacy generator for flutter v1")
108-
dartCmd.Flags().StringVar(&dartgen.VarStringHostname, "hostname", "", "hostname of the server")
109-
dartCmd.Flags().StringVar(&dartgen.VarStringScheme, "scheme", "", "scheme of the server")
110-
111-
docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir")
112-
docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory")
113-
114-
formatCmd.Flags().StringVar(&format.VarStringDir, "dir", "", "The format target dir")
115-
formatCmd.Flags().BoolVar(&format.VarBoolIgnore, "iu", false, "Ignore update")
116-
formatCmd.Flags().BoolVar(&format.VarBoolUseStdin, "stdin", false, "Use stdin to input api"+
117-
" doc content, press \"ctrl + d\" to send EOF")
118-
formatCmd.Flags().BoolVar(&format.VarBoolSkipCheckDeclare, "declare", false, "Use to skip check "+
119-
"api types already declare")
120-
121-
goCmd.Flags().StringVar(&gogen.VarStringDir, "dir", "", "The target dir")
122-
goCmd.Flags().StringVar(&gogen.VarStringAPI, "api", "", "The api file")
123-
goCmd.Flags().StringVar(&gogen.VarStringHome, "home", "", "The goctl home path of "+
124-
"the template, --home and --remote cannot be set at the same time, if they are, --remote "+
125-
"has higher priority")
126-
goCmd.Flags().StringVar(&gogen.VarStringRemote, "remote", "", "The remote git repo "+
127-
"of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
128-
" has higher priority\nThe git repo directory must be consistent with the "+
129-
"https://github.com/zeromicro/go-zero-template directory structure")
130-
goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
131-
"the remote repo, it does work with --remote")
132-
goCmd.Flags().StringVar(&gogen.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
133-
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
134-
135-
javaCmd.Flags().StringVar(&javagen.VarStringDir, "dir", "", "The target dir")
136-
javaCmd.Flags().StringVar(&javagen.VarStringAPI, "api", "", "The api file")
137-
138-
ktCmd.Flags().StringVar(&ktgen.VarStringDir, "dir", "", "The target dir")
139-
ktCmd.Flags().StringVar(&ktgen.VarStringAPI, "api", "", "The api file")
140-
ktCmd.Flags().StringVar(&ktgen.VarStringPKG, "pkg", "", "Define package name for kotlin file")
141-
142-
newCmd.Flags().StringVar(&new.VarStringHome, "home", "", "The goctl home path of "+
143-
"the template, --home and --remote cannot be set at the same time, if they are, --remote "+
144-
"has higher priority")
145-
newCmd.Flags().StringVar(&new.VarStringRemote, "remote", "", "The remote git repo "+
146-
"of the template, --home and --remote cannot be set at the same time, if they are, --remote"+
147-
" has higher priority\n\tThe git repo directory must be consistent with the "+
148-
"https://github.com/zeromicro/go-zero-template directory structure")
149-
newCmd.Flags().StringVar(&new.VarStringBranch, "branch", "", "The branch of "+
150-
"the remote repo, it does work with --remote")
151-
newCmd.Flags().StringVar(&new.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
152-
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
153-
154-
pluginCmd.Flags().StringVarP(&plugin.VarStringPlugin, "plugin", "p", "", "The plugin file")
155-
pluginCmd.Flags().StringVar(&plugin.VarStringDir, "dir", "", "The target dir")
156-
pluginCmd.Flags().StringVar(&plugin.VarStringAPI, "api", "", "The api file")
157-
pluginCmd.Flags().StringVar(&plugin.VarStringStyle, "style", "",
158-
"The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
159-
160-
tsCmd.Flags().StringVar(&tsgen.VarStringDir, "dir", "", "The target dir")
161-
tsCmd.Flags().StringVar(&tsgen.VarStringAPI, "api", "", "The api file")
162-
tsCmd.Flags().StringVar(&tsgen.VarStringCaller, "caller", "", "The web api caller")
163-
tsCmd.Flags().BoolVar(&tsgen.VarBoolUnWrap, "unwrap", false, "Unwrap the webapi caller for import")
164-
165-
validateCmd.Flags().StringVar(&validate.VarStringAPI, "api", "", "Validate target api file")
37+
var (
38+
apiCmdFlags = Cmd.Flags()
39+
dartCmdFlags = dartCmd.Flags()
40+
docCmdFlags = docCmd.Flags()
41+
formatCmdFlags = formatCmd.Flags()
42+
goCmdFlags = goCmd.Flags()
43+
javaCmdFlags = javaCmd.Flags()
44+
ktCmdFlags = ktCmd.Flags()
45+
newCmdFlags = newCmd.Flags()
46+
pluginCmdFlags = pluginCmd.Flags()
47+
tsCmdFlags = tsCmd.Flags()
48+
validateCmdFlags = validateCmd.Flags()
49+
)
50+
51+
apiCmdFlags.StringVar(&apigen.VarStringOutput, "o")
52+
apiCmdFlags.StringVar(&apigen.VarStringHome, "home")
53+
apiCmdFlags.StringVar(&apigen.VarStringRemote, "remote")
54+
apiCmdFlags.StringVar(&apigen.VarStringBranch, "branch")
55+
56+
dartCmdFlags.StringVar(&dartgen.VarStringDir, "dir")
57+
dartCmdFlags.StringVar(&dartgen.VarStringAPI, "api")
58+
dartCmdFlags.BoolVar(&dartgen.VarStringLegacy, "legacy")
59+
dartCmdFlags.StringVar(&dartgen.VarStringHostname, "hostname")
60+
dartCmdFlags.StringVar(&dartgen.VarStringScheme, "scheme")
61+
62+
docCmdFlags.StringVar(&docgen.VarStringDir, "dir")
63+
docCmdFlags.StringVar(&docgen.VarStringOutput, "o")
64+
65+
formatCmdFlags.StringVar(&format.VarStringDir, "dir")
66+
formatCmdFlags.BoolVar(&format.VarBoolIgnore, "iu")
67+
formatCmdFlags.BoolVar(&format.VarBoolUseStdin, "stdin")
68+
formatCmdFlags.BoolVar(&format.VarBoolSkipCheckDeclare, "declare")
69+
70+
goCmdFlags.StringVar(&gogen.VarStringDir, "dir")
71+
goCmdFlags.StringVar(&gogen.VarStringAPI, "api")
72+
goCmdFlags.StringVar(&gogen.VarStringHome, "home")
73+
goCmdFlags.StringVar(&gogen.VarStringRemote, "remote")
74+
goCmdFlags.StringVar(&gogen.VarStringBranch, "branch")
75+
goCmdFlags.StringVarWithDefaultValue(&gogen.VarStringStyle, "style", config.DefaultFormat)
76+
77+
javaCmdFlags.StringVar(&javagen.VarStringDir, "dir")
78+
javaCmdFlags.StringVar(&javagen.VarStringAPI, "api")
79+
80+
ktCmdFlags.StringVar(&ktgen.VarStringDir, "dir")
81+
ktCmdFlags.StringVar(&ktgen.VarStringAPI, "api")
82+
ktCmdFlags.StringVar(&ktgen.VarStringPKG, "pkg")
83+
84+
newCmdFlags.StringVar(&new.VarStringHome, "home")
85+
newCmdFlags.StringVar(&new.VarStringRemote, "remote")
86+
newCmdFlags.StringVar(&new.VarStringBranch, "branch")
87+
newCmdFlags.StringVarWithDefaultValue(&new.VarStringStyle, "style", config.DefaultFormat)
88+
89+
pluginCmdFlags.StringVarP(&plugin.VarStringPlugin, "plugin", "p")
90+
pluginCmdFlags.StringVar(&plugin.VarStringDir, "dir")
91+
pluginCmdFlags.StringVar(&plugin.VarStringAPI, "api")
92+
pluginCmdFlags.StringVar(&plugin.VarStringStyle, "style")
93+
94+
tsCmdFlags.StringVar(&tsgen.VarStringDir, "dir")
95+
tsCmdFlags.StringVar(&tsgen.VarStringAPI, "api")
96+
tsCmdFlags.StringVar(&tsgen.VarStringCaller, "caller")
97+
tsCmdFlags.BoolVar(&tsgen.VarBoolUnWrap, "unwrap")
98+
99+
validateCmdFlags.StringVar(&validate.VarStringAPI, "api")
166100

167101
// Add sub-commands
168-
Cmd.AddCommand(dartCmd)
169-
Cmd.AddCommand(docCmd)
170-
Cmd.AddCommand(formatCmd)
171-
Cmd.AddCommand(goCmd)
172-
Cmd.AddCommand(javaCmd)
173-
Cmd.AddCommand(ktCmd)
174-
Cmd.AddCommand(newCmd)
175-
Cmd.AddCommand(pluginCmd)
176-
Cmd.AddCommand(tsCmd)
177-
Cmd.AddCommand(validateCmd)
102+
Cmd.AddCommand(dartCmd, docCmd, formatCmd, goCmd, javaCmd, ktCmd, newCmd, pluginCmd, tsCmd, validateCmd)
178103
}

‎tools/goctl/api/new/newservice.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"github.com/spf13/cobra"
1112
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
1213
conf "github.com/zeromicro/go-zero/tools/goctl/config"
1314
"github.com/zeromicro/go-zero/tools/goctl/util"
@@ -29,7 +30,7 @@ var (
2930
)
3031

3132
// CreateServiceCommand fast create service
32-
func CreateServiceCommand(args []string) error {
33+
func CreateServiceCommand(_ *cobra.Command, args []string) error {
3334
dirName := args[0]
3435
if len(VarStringStyle) == 0 {
3536
VarStringStyle = conf.DefaultFormat

‎tools/goctl/bug/cmd.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package bug
22

3-
import "github.com/spf13/cobra"
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
6+
)
47

58
// Cmd describes a bug command.
6-
var Cmd = &cobra.Command{
7-
Use: "bug",
8-
Short: "Report a bug",
9-
Args: cobra.NoArgs,
10-
RunE: runE,
11-
}
9+
var Cmd = cobrax.NewCommand("bug", cobrax.WithRunE(cobra.NoArgs), cobrax.WithArgs(cobra.NoArgs))

‎tools/goctl/cmd/root.go‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/zeromicro/go-zero/tools/goctl/bug"
1616
"github.com/zeromicro/go-zero/tools/goctl/docker"
1717
"github.com/zeromicro/go-zero/tools/goctl/env"
18+
"github.com/zeromicro/go-zero/tools/goctl/gateway"
19+
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
1820
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
1921
"github.com/zeromicro/go-zero/tools/goctl/kube"
2022
"github.com/zeromicro/go-zero/tools/goctl/migrate"
@@ -35,14 +37,7 @@ const (
3537
var (
3638
//go:embed usage.tpl
3739
usageTpl string
38-
39-
rootCmd = &cobra.Command{
40-
Use: "goctl",
41-
Short: "A cli tool to generate go-zero code",
42-
Long: "A cli tool to generate api, zrpc, model code\n\n" +
43-
"GitHub: https://github.com/zeromicro/go-zero\n" +
44-
"Site: https://go-zero.dev",
45-
}
40+
rootCmd = cobrax.NewCommand("goctl")
4641
)
4742

4843
// Execute executes the given command
@@ -117,16 +112,8 @@ func init() {
117112
runtime.GOOS, runtime.GOARCH)
118113

119114
rootCmd.SetUsageTemplate(usageTpl)
120-
rootCmd.AddCommand(api.Cmd)
121-
rootCmd.AddCommand(bug.Cmd)
122-
rootCmd.AddCommand(docker.Cmd)
123-
rootCmd.AddCommand(kube.Cmd)
124-
rootCmd.AddCommand(env.Cmd)
125-
rootCmd.AddCommand(model.Cmd)
126-
rootCmd.AddCommand(migrate.Cmd)
127-
rootCmd.AddCommand(quickstart.Cmd)
128-
rootCmd.AddCommand(rpc.Cmd)
129-
rootCmd.AddCommand(tpl.Cmd)
130-
rootCmd.AddCommand(upgrade.Cmd)
131-
rootCmd.AddCommand(cobracompletefig.CreateCompletionSpecCommand())
115+
rootCmd.AddCommand(api.Cmd, bug.Cmd, docker.Cmd, kube.Cmd, env.Cmd, gateway.Cmd, model.Cmd)
116+
rootCmd.AddCommand(migrate.Cmd, quickstart.Cmd, rpc.Cmd, tpl.Cmd, upgrade.Cmd)
117+
rootCmd.Command.AddCommand(cobracompletefig.CreateCompletionSpecCommand())
118+
rootCmd.MustInit()
132119
}

‎tools/goctl/docker/cmd.go‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package docker
22

3-
import "github.com/spf13/cobra"
3+
import "github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
44

55
var (
66
varExeName string
@@ -14,21 +14,18 @@ var (
1414
varStringTZ string
1515

1616
// Cmd describes a docker command.
17-
Cmd = &cobra.Command{
18-
Use: "docker",
19-
Short: "Generate Dockerfile",
20-
RunE: dockerCommand,
21-
}
17+
Cmd = cobrax.NewCommand("docker", cobrax.WithRunE(dockerCommand))
2218
)
2319

2420
func init() {
25-
Cmd.Flags().StringVar(&varExeName, "exe", "", "The executable name in the built image")
26-
Cmd.Flags().StringVar(&varStringGo, "go", "", "The file that contains main function")
27-
Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch")
28-
Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none")
29-
Cmd.Flags().StringVar(&varStringHome, "home", "", "The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority")
30-
Cmd.Flags().StringVar(&varStringRemote, "remote", "", "The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority\nThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
31-
Cmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
32-
Cmd.Flags().StringVar(&varStringVersion, "version", "", "The goctl builder golang image version")
33-
Cmd.Flags().StringVar(&varStringTZ, "tz", "Asia/Shanghai", "The timezone of the container")
21+
dockerCmdFlags := Cmd.Flags()
22+
dockerCmdFlags.StringVar(&varExeName, "exe")
23+
dockerCmdFlags.StringVar(&varStringGo, "go")
24+
dockerCmdFlags.StringVarWithDefaultValue(&varStringBase, "base", "scratch")
25+
dockerCmdFlags.IntVar(&varIntPort, "port")
26+
dockerCmdFlags.StringVar(&varStringHome, "home")
27+
dockerCmdFlags.StringVar(&varStringRemote, "remote")
28+
dockerCmdFlags.StringVar(&varStringBranch, "branch")
29+
dockerCmdFlags.StringVar(&varStringVersion, "version")
30+
dockerCmdFlags.StringVarWithDefaultValue(&varStringTZ, "tz", "Asia/Shanghai")
3431
}

0 commit comments

Comments
 (0)