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)
}