Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace cli to cobra #1855

Merged
merged 10 commits into from May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 19 additions & 15 deletions tools/goctl/api/apigen/gen.go
Expand Up @@ -9,21 +9,28 @@ 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"
)

//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")
}
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions tools/goctl/api/apigen/template.go
Expand Up @@ -3,7 +3,6 @@ package apigen
import (
"fmt"

"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)

Expand All @@ -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)
}

Expand Down
176 changes: 176 additions & 0 deletions tools/goctl/api/cmd.go
@@ -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)
}
23 changes: 17 additions & 6 deletions tools/goctl/api/dartgen/gen.go
Expand Up @@ -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")
}
Expand Down
15 changes: 11 additions & 4 deletions tools/goctl/api/docgen/gen.go
Expand Up @@ -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()
Expand Down
33 changes: 20 additions & 13 deletions tools/goctl/api/format/format.go
Expand Up @@ -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"
Expand All @@ -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()))
}
}
Expand Down