feat(goctl): Support gateway sample generation (#3049)

This commit is contained in:
anqiansong
2023-03-29 17:06:23 +08:00
committed by GitHub
parent 95b85336d6
commit 1904af2323
32 changed files with 1399 additions and 513 deletions

View File

@@ -13,166 +13,91 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/tsgen"
"github.com/zeromicro/go-zero/tools/goctl/api/validate"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/internal/cobrax"
"github.com/zeromicro/go-zero/tools/goctl/plugin"
)
var (
// Cmd describes an api command.
Cmd = &cobra.Command{
Use: "api",
Short: "Generate api related files",
RunE: apigen.CreateApiTemplate,
}
dartCmd = &cobra.Command{
Use: "dart",
Short: "Generate dart files for provided api in api file",
RunE: dartgen.DartCommand,
}
docCmd = &cobra.Command{
Use: "doc",
Short: "Generate doc files",
RunE: docgen.DocCommand,
}
formatCmd = &cobra.Command{
Use: "format",
Short: "Format api files",
RunE: format.GoFormatApi,
}
goCmd = &cobra.Command{
Use: "go",
Short: "Generate go files for provided api in api file",
RunE: gogen.GoCommand,
}
newCmd = &cobra.Command{
Use: "new",
Short: "Fast create api service",
Example: "goctl api new [options] service-name",
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
return new.CreateServiceCommand(args)
},
}
validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate api file",
RunE: validate.GoValidateApi,
}
javaCmd = &cobra.Command{
Use: "java",
Short: "Generate java files for provided api in api file",
Hidden: true,
RunE: javagen.JavaCommand,
}
ktCmd = &cobra.Command{
Use: "kt",
Short: "Generate kotlin code for provided api file",
RunE: ktgen.KtCommand,
}
pluginCmd = &cobra.Command{
Use: "plugin",
Short: "Custom file generator",
RunE: plugin.PluginCommand,
}
tsCmd = &cobra.Command{
Use: "ts",
Short: "Generate ts files for provided api in api file",
RunE: tsgen.TsCommand,
}
Cmd = cobrax.NewCommand("api", cobrax.WithRunE(apigen.CreateApiTemplate))
dartCmd = cobrax.NewCommand("dart", cobrax.WithRunE(dartgen.DartCommand))
docCmd = cobrax.NewCommand("doc", cobrax.WithRunE(docgen.DocCommand))
formatCmd = cobrax.NewCommand("format", cobrax.WithRunE(format.GoFormatApi))
goCmd = cobrax.NewCommand("go", cobrax.WithRunE(gogen.GoCommand))
newCmd = cobrax.NewCommand("new", cobrax.WithRunE(new.CreateServiceCommand),
cobrax.WithArgs(cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)))
validateCmd = cobrax.NewCommand("validate", cobrax.WithRunE(validate.GoValidateApi))
javaCmd = cobrax.NewCommand("java", cobrax.WithRunE(javagen.JavaCommand), cobrax.WithHidden())
ktCmd = cobrax.NewCommand("kt", cobrax.WithRunE(ktgen.KtCommand))
pluginCmd = cobrax.NewCommand("plugin", cobrax.WithRunE(plugin.PluginCommand))
tsCmd = cobrax.NewCommand("ts", cobrax.WithRunE(tsgen.TsCommand))
)
func init() {
Cmd.Flags().StringVar(&apigen.VarStringOutput, "o", "", "Output a sample api file")
Cmd.Flags().StringVar(&apigen.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")
Cmd.Flags().StringVar(&apigen.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")
Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+
"remote repo, it does work with --remote")
var (
apiCmdFlags = Cmd.Flags()
dartCmdFlags = dartCmd.Flags()
docCmdFlags = docCmd.Flags()
formatCmdFlags = formatCmd.Flags()
goCmdFlags = goCmd.Flags()
javaCmdFlags = javaCmd.Flags()
ktCmdFlags = ktCmd.Flags()
newCmdFlags = newCmd.Flags()
pluginCmdFlags = pluginCmd.Flags()
tsCmdFlags = tsCmd.Flags()
validateCmdFlags = validateCmd.Flags()
)
dartCmd.Flags().StringVar(&dartgen.VarStringDir, "dir", "", "The target dir")
dartCmd.Flags().StringVar(&dartgen.VarStringAPI, "api", "", "The api file")
dartCmd.Flags().BoolVar(&dartgen.VarStringLegacy, "legacy", false, "Legacy generator for flutter v1")
dartCmd.Flags().StringVar(&dartgen.VarStringHostname, "hostname", "", "hostname of the server")
dartCmd.Flags().StringVar(&dartgen.VarStringScheme, "scheme", "", "scheme of the server")
apiCmdFlags.StringVar(&apigen.VarStringOutput, "o")
apiCmdFlags.StringVar(&apigen.VarStringHome, "home")
apiCmdFlags.StringVar(&apigen.VarStringRemote, "remote")
apiCmdFlags.StringVar(&apigen.VarStringBranch, "branch")
docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir")
docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory")
dartCmdFlags.StringVar(&dartgen.VarStringDir, "dir")
dartCmdFlags.StringVar(&dartgen.VarStringAPI, "api")
dartCmdFlags.BoolVar(&dartgen.VarStringLegacy, "legacy")
dartCmdFlags.StringVar(&dartgen.VarStringHostname, "hostname")
dartCmdFlags.StringVar(&dartgen.VarStringScheme, "scheme")
formatCmd.Flags().StringVar(&format.VarStringDir, "dir", "", "The format target dir")
formatCmd.Flags().BoolVar(&format.VarBoolIgnore, "iu", false, "Ignore update")
formatCmd.Flags().BoolVar(&format.VarBoolUseStdin, "stdin", false, "Use stdin to input api"+
" doc content, press \"ctrl + d\" to send EOF")
formatCmd.Flags().BoolVar(&format.VarBoolSkipCheckDeclare, "declare", false, "Use to skip check "+
"api types already declare")
docCmdFlags.StringVar(&docgen.VarStringDir, "dir")
docCmdFlags.StringVar(&docgen.VarStringOutput, "o")
goCmd.Flags().StringVar(&gogen.VarStringDir, "dir", "", "The target dir")
goCmd.Flags().StringVar(&gogen.VarStringAPI, "api", "", "The api file")
goCmd.Flags().StringVar(&gogen.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")
goCmd.Flags().StringVar(&gogen.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")
goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
"the remote repo, it does work with --remote")
goCmd.Flags().StringVar(&gogen.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
formatCmdFlags.StringVar(&format.VarStringDir, "dir")
formatCmdFlags.BoolVar(&format.VarBoolIgnore, "iu")
formatCmdFlags.BoolVar(&format.VarBoolUseStdin, "stdin")
formatCmdFlags.BoolVar(&format.VarBoolSkipCheckDeclare, "declare")
javaCmd.Flags().StringVar(&javagen.VarStringDir, "dir", "", "The target dir")
javaCmd.Flags().StringVar(&javagen.VarStringAPI, "api", "", "The api file")
goCmdFlags.StringVar(&gogen.VarStringDir, "dir")
goCmdFlags.StringVar(&gogen.VarStringAPI, "api")
goCmdFlags.StringVar(&gogen.VarStringHome, "home")
goCmdFlags.StringVar(&gogen.VarStringRemote, "remote")
goCmdFlags.StringVar(&gogen.VarStringBranch, "branch")
goCmdFlags.StringVarWithDefaultValue(&gogen.VarStringStyle, "style", config.DefaultFormat)
ktCmd.Flags().StringVar(&ktgen.VarStringDir, "dir", "", "The target dir")
ktCmd.Flags().StringVar(&ktgen.VarStringAPI, "api", "", "The api file")
ktCmd.Flags().StringVar(&ktgen.VarStringPKG, "pkg", "", "Define package name for kotlin file")
javaCmdFlags.StringVar(&javagen.VarStringDir, "dir")
javaCmdFlags.StringVar(&javagen.VarStringAPI, "api")
newCmd.Flags().StringVar(&new.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")
newCmd.Flags().StringVar(&new.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\n\tThe git repo directory must be consistent with the "+
"https://github.com/zeromicro/go-zero-template directory structure")
newCmd.Flags().StringVar(&new.VarStringBranch, "branch", "", "The branch of "+
"the remote repo, it does work with --remote")
newCmd.Flags().StringVar(&new.VarStringStyle, "style", config.DefaultFormat, "The file naming format,"+
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
ktCmdFlags.StringVar(&ktgen.VarStringDir, "dir")
ktCmdFlags.StringVar(&ktgen.VarStringAPI, "api")
ktCmdFlags.StringVar(&ktgen.VarStringPKG, "pkg")
pluginCmd.Flags().StringVarP(&plugin.VarStringPlugin, "plugin", "p", "", "The plugin file")
pluginCmd.Flags().StringVar(&plugin.VarStringDir, "dir", "", "The target dir")
pluginCmd.Flags().StringVar(&plugin.VarStringAPI, "api", "", "The api file")
pluginCmd.Flags().StringVar(&plugin.VarStringStyle, "style", "",
"The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
newCmdFlags.StringVar(&new.VarStringHome, "home")
newCmdFlags.StringVar(&new.VarStringRemote, "remote")
newCmdFlags.StringVar(&new.VarStringBranch, "branch")
newCmdFlags.StringVarWithDefaultValue(&new.VarStringStyle, "style", config.DefaultFormat)
tsCmd.Flags().StringVar(&tsgen.VarStringDir, "dir", "", "The target dir")
tsCmd.Flags().StringVar(&tsgen.VarStringAPI, "api", "", "The api file")
tsCmd.Flags().StringVar(&tsgen.VarStringCaller, "caller", "", "The web api caller")
tsCmd.Flags().BoolVar(&tsgen.VarBoolUnWrap, "unwrap", false, "Unwrap the webapi caller for import")
pluginCmdFlags.StringVarP(&plugin.VarStringPlugin, "plugin", "p")
pluginCmdFlags.StringVar(&plugin.VarStringDir, "dir")
pluginCmdFlags.StringVar(&plugin.VarStringAPI, "api")
pluginCmdFlags.StringVar(&plugin.VarStringStyle, "style")
validateCmd.Flags().StringVar(&validate.VarStringAPI, "api", "", "Validate target api file")
tsCmdFlags.StringVar(&tsgen.VarStringDir, "dir")
tsCmdFlags.StringVar(&tsgen.VarStringAPI, "api")
tsCmdFlags.StringVar(&tsgen.VarStringCaller, "caller")
tsCmdFlags.BoolVar(&tsgen.VarBoolUnWrap, "unwrap")
validateCmdFlags.StringVar(&validate.VarStringAPI, "api")
// Add sub-commands
Cmd.AddCommand(dartCmd)
Cmd.AddCommand(docCmd)
Cmd.AddCommand(formatCmd)
Cmd.AddCommand(goCmd)
Cmd.AddCommand(javaCmd)
Cmd.AddCommand(ktCmd)
Cmd.AddCommand(newCmd)
Cmd.AddCommand(pluginCmd)
Cmd.AddCommand(tsCmd)
Cmd.AddCommand(validateCmd)
Cmd.AddCommand(dartCmd, docCmd, formatCmd, goCmd, javaCmd, ktCmd, newCmd, pluginCmd, tsCmd, validateCmd)
}

View File

@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
conf "github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util"
@@ -29,7 +30,7 @@ var (
)
// CreateServiceCommand fast create service
func CreateServiceCommand(args []string) error {
func CreateServiceCommand(_ *cobra.Command, args []string) error {
dirName := args[0]
if len(VarStringStyle) == 0 {
VarStringStyle = conf.DefaultFormat