feat: Replace cli to cobra (#1855)

* Replace cli

* Replace cli

* Replace cli

* Format code

* Add compare case

* Add compare case

* Add compare case

* Support go style flag

* Support go style flag

* Add test case
This commit is contained in:
anqiansong
2022-05-07 15:40:11 +08:00
committed by GitHub
parent 51472004a3
commit 5383e29ce6
61 changed files with 1858 additions and 1590 deletions

View File

@@ -9,7 +9,7 @@ import (
"strings"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -17,13 +17,20 @@ import (
//go:embed api.tpl
var apiTemplate string
// ApiCommand create api template file
func ApiCommand(c *cli.Context) error {
if c.NumFlags() == 0 {
cli.ShowAppHelpAndExit(c, 1)
}
var (
// VarStringOutput describes the output.
VarStringOutput string
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch.
VarStringBranch string
)
apiFile := c.String("o")
// CreateApiTemplate create api template file
func CreateApiTemplate(_ *cobra.Command, _ []string) error {
apiFile := VarStringOutput
if len(apiFile) == 0 {
return errors.New("missing -o")
}
@@ -34,18 +41,15 @@ func ApiCommand(c *cli.Context) error {
}
defer fp.Close()
home := c.String("home")
remote := c.String("remote")
branch := c.String("branch")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(VarStringRemote) > 0 {
repo, _ := util.CloneIntoGitHome(VarStringRemote, VarStringBranch)
if len(repo) > 0 {
home = repo
VarStringHome = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
if len(VarStringHome) > 0 {
pathx.RegisterGoctlHome(VarStringHome)
}
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)

View File

@@ -3,7 +3,6 @@ package apigen
import (
"fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -27,7 +26,7 @@ func Clean() error {
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
func GenTemplates() error {
return pathx.InitTemplates(category, templates)
}

176
tools/goctl/api/cmd.go Normal file
View File

@@ -0,0 +1,176 @@
package api
import (
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/apigen"
"github.com/zeromicro/go-zero/tools/goctl/api/dartgen"
"github.com/zeromicro/go-zero/tools/goctl/api/docgen"
"github.com/zeromicro/go-zero/tools/goctl/api/format"
"github.com/zeromicro/go-zero/tools/goctl/api/gogen"
"github.com/zeromicro/go-zero/tools/goctl/api/javagen"
"github.com/zeromicro/go-zero/tools/goctl/api/ktgen"
"github.com/zeromicro/go-zero/tools/goctl/api/new"
"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/plugin"
)
var (
// Cmd describes a 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 yaml file",
RunE: gogen.GoCommand,
}
newCmd = &cobra.Command{
Use: "new",
Short: "Fast create api service",
Example: "goctl api new [options] service-name",
Args: cobra.ExactValidArgs(1),
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",
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,
}
)
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\n\tThe git repo directory must be consistent with the"+
" https://github.com/zeromicro/go-zero-template directory structure")
Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "master", "The branch of the "+
"remote repo, it does work with --remote")
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")
docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir")
docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory")
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")
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\n\tThe git repo directory must be consistent with the "+
"https://github.com/zeromicro/go-zero-template directory structure")
goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "master", "The branch of "+
"the remote repo, it does work with --remote")
goCmd.Flags().StringVar(&gogen.VarStringStyle, "style", "gozero", "The file naming format,"+
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
javaCmd.Flags().StringVar(&javagen.VarStringDir, "dir", "", "The target dir")
javaCmd.Flags().StringVar(&javagen.VarStringAPI, "api", "", "The api file")
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")
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", "master", "The branch of "+
"the remote repo, it does work with --remote")
newCmd.Flags().StringVar(&new.VarStringStyle, "style", "gozero", "The file naming format,"+
" see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]")
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]")
tsCmd.Flags().StringVar(&tsgen.VarStringDir, "dir", "", "The target dir")
tsCmd.Flags().StringVar(&tsgen.VarStringAPI, "api", "", "The api file")
tsCmd.Flags().StringVar(&tsgen.VarStringWebAPI, "webapi", "", "The web api file path")
tsCmd.Flags().StringVar(&tsgen.VarStringCaller, "caller", "", "The web api caller")
tsCmd.Flags().BoolVar(&tsgen.VarBoolUnWrap, "unwrap", false, "Unwrap the webapi caller for import")
validateCmd.Flags().StringVar(&validate.VarStringAPI, "api", "", "Validate target api file")
// 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)
}

View File

@@ -5,17 +5,28 @@ import (
"fmt"
"strings"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
)
var (
// VarStringDir describes the directory.
VarStringDir string
// VarStringAPI defines the API.
VarStringAPI string
// VarStringLegacy describes whether legacy.
VarStringLegacy bool
// VarStringHostname defines the hostname.
VarStringHostname string
)
// DartCommand create dart network request code
func DartCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
isLegacy := c.Bool("legacy")
hostname := c.String("hostname")
func DartCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
isLegacy := VarStringLegacy
hostname := VarStringHostname
if len(apiFile) == 0 {
return errors.New("missing -api")
}

View File

@@ -7,19 +7,26 @@ import (
"path/filepath"
"strings"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
var (
// VarStringDir describes a directory.
VarStringDir string
// VarStringOutput describes an output directory.
VarStringOutput string
)
// DocCommand generate Markdown doc file
func DocCommand(c *cli.Context) error {
dir := c.String("dir")
func DocCommand(_ *cobra.Command, _ []string) error {
dir := VarStringDir
if len(dir) == 0 {
return errors.New("missing -dir")
}
outputDir := c.String("o")
outputDir := VarStringOutput
if len(outputDir) == 0 {
var err error
outputDir, err = os.Getwd()

View File

@@ -12,7 +12,7 @@ import (
"path/filepath"
"strings"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/api/util"
@@ -26,30 +26,37 @@ const (
rightBrace = "}"
)
// GoFormatApi format api file
func GoFormatApi(c *cli.Context) error {
useStdin := c.Bool("stdin")
skipCheckDeclare := c.Bool("declare")
dir := c.String("dir")
var (
// VarBoolUseStdin describes whether to use stdin or not.
VarBoolUseStdin bool
// VarBoolSkipCheckDeclare describes whether to skip.
VarBoolSkipCheckDeclare bool
// VarStringDir describes the directory.
VarStringDir string
// VarBoolIgnore describes whether to ignore.
VarBoolIgnore bool
)
// GoFormatApi format api file
func GoFormatApi(_ *cobra.Command, _ []string) error {
var be errorx.BatchError
if useStdin {
if err := apiFormatReader(os.Stdin, dir, skipCheckDeclare); err != nil {
if VarBoolUseStdin {
if err := apiFormatReader(os.Stdin, VarStringDir, VarBoolSkipCheckDeclare); err != nil {
be.Add(err)
}
} else {
if len(dir) == 0 {
if len(VarStringDir) == 0 {
return errors.New("missing -dir")
}
_, err := os.Lstat(dir)
_, err := os.Lstat(VarStringDir)
if err != nil {
return errors.New(dir + ": No such file or directory")
return errors.New(VarStringDir + ": No such file or directory")
}
err = filepath.Walk(dir, func(path string, fi os.FileInfo, errBack error) (err error) {
err = filepath.Walk(VarStringDir, func(path string, fi os.FileInfo, errBack error) (err error) {
if strings.HasSuffix(path, ".api") {
if err := ApiFormatByPath(path, skipCheckDeclare); err != nil {
if err := ApiFormatByPath(path, VarBoolSkipCheckDeclare); err != nil {
be.Add(util.WrapErr(err, fi.Name()))
}
}

View File

@@ -12,7 +12,7 @@ import (
"time"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
apiformat "github.com/zeromicro/go-zero/tools/goctl/api/format"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
@@ -24,16 +24,30 @@ import (
const tmpFile = "%s-%d"
var tmpDir = path.Join(os.TempDir(), "goctl")
var (
tmpDir = path.Join(os.TempDir(), "goctl")
// VarStringDir describes the directory.
VarStringDir string
// VarStringAPI describes the API.
VarStringAPI string
// VarStringHome describes the go home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the branch.
VarStringBranch string
// VarStringStyle describes the style of output files.
VarStringStyle string
)
// GoCommand gen go project files from command line
func GoCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
namingStyle := c.String("style")
home := c.String("home")
remote := c.String("remote")
branch := c.String("branch")
func GoCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
namingStyle := VarStringStyle
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {

View File

@@ -3,7 +3,6 @@ package gogen
import (
"fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -45,7 +44,7 @@ func Clean() error {
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
func GenTemplates() error {
return pathx.InitTemplates(category, templates)
}

View File

@@ -6,16 +6,23 @@ import (
"strings"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
var (
// VarStringDir describes a directory.
VarStringDir string
// VarStringAPI describes an API.
VarStringAPI string
)
// JavaCommand generates java code command entrance.
func JavaCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
func JavaCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
if len(apiFile) == 0 {
return errors.New("missing -api")
}

View File

@@ -3,21 +3,30 @@ package ktgen
import (
"errors"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
)
var (
// VarStringDir describes a directory.
VarStringDir string
// VarStringAPI describes an API.
VarStringAPI string
// VarStringPKG describes a package.
VarStringPKG string
)
// KtCommand generates kotlin code command entrance
func KtCommand(c *cli.Context) error {
apiFile := c.String("api")
func KtCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
if apiFile == "" {
return errors.New("missing -api")
}
dir := c.String("dir")
dir := VarStringDir
if dir == "" {
return errors.New("missing -dir")
}
pkg := c.String("pkg")
pkg := VarStringPKG
if pkg == "" {
return errors.New("missing -pkg")
}

View File

@@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"
"github.com/urfave/cli"
"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"
@@ -18,18 +17,22 @@ import (
//go:embed api.tpl
var apiTemplate string
var (
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch.
VarStringBranch string
// VarStringStyle describes the style of output files.
VarStringStyle string
)
// CreateServiceCommand fast create service
func CreateServiceCommand(c *cli.Context) error {
if c.NArg() == 0 {
cli.ShowCommandHelpAndExit(c, "new", 1)
}
args := c.Args()
dirName := args.First()
dirStyle := c.String("style")
if len(dirStyle) == 0 {
dirStyle = conf.DefaultFormat
func CreateServiceCommand(args []string) error {
dirName := args[0]
if len(VarStringStyle) == 0 {
VarStringStyle = conf.DefaultFormat
}
if strings.Contains(dirName, "-") {
return errors.New("api new command service name not support strikethrough, because this will used by function name")
@@ -55,18 +58,15 @@ func CreateServiceCommand(c *cli.Context) error {
defer fp.Close()
home := c.String("home")
remote := c.String("remote")
branch := c.String("branch")
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(VarStringRemote) > 0 {
repo, _ := util.CloneIntoGitHome(VarStringRemote, VarStringBranch)
if len(repo) > 0 {
home = repo
VarStringHome = repo
}
}
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
if len(VarStringHome) > 0 {
pathx.RegisterGoctlHome(VarStringHome)
}
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
@@ -82,6 +82,6 @@ func CreateServiceCommand(c *cli.Context) error {
return err
}
err = gogen.DoGenProject(apiFilePath, abs, dirStyle)
err = gogen.DoGenProject(apiFilePath, abs, VarStringStyle)
return err
}

View File

@@ -3,7 +3,6 @@ package new
import (
"fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -27,7 +26,7 @@ func Clean() error {
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
func GenTemplates() error {
return pathx.InitTemplates(category, templates)
}

View File

@@ -5,19 +5,32 @@ import (
"fmt"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
var (
// VarStringDir describes a directory.
VarStringDir string
// VarStringAPI describes an API file.
VarStringAPI string
// VarStringWebAPI describes a web API file.
VarStringWebAPI string
// VarStringCaller describes a caller.
VarStringCaller string
// VarBoolUnWrap describes whether wrap or not.
VarBoolUnWrap bool
)
// TsCommand provides the entry to generate typescript codes
func TsCommand(c *cli.Context) error {
apiFile := c.String("api")
dir := c.String("dir")
webAPI := c.String("webapi")
caller := c.String("caller")
unwrapAPI := c.Bool("unwrap")
func TsCommand(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
dir := VarStringDir
webAPI := VarStringWebAPI
caller := VarStringCaller
unwrapAPI := VarBoolUnWrap
if len(apiFile) == 0 {
return errors.New("missing -api")
}

View File

@@ -5,13 +5,16 @@ import (
"fmt"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
)
// VarStringAPI describes an API.
var VarStringAPI string
// GoValidateApi verifies whether the api has a syntax error
func GoValidateApi(c *cli.Context) error {
apiFile := c.String("api")
func GoValidateApi(_ *cobra.Command, _ []string) error {
apiFile := VarStringAPI
if len(apiFile) == 0 {
return errors.New("missing -api")