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

91
tools/goctl/model/cmd.go Normal file
View File

@@ -0,0 +1,91 @@
package model
import (
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/command"
)
var (
// Cmd describes a model command.
Cmd = &cobra.Command{
Use: "model",
Short: "Generate model code",
}
mysqlCmd = &cobra.Command{
Use: "mysql",
Short: "Generate mysql model",
}
ddlCmd = &cobra.Command{
Use: "ddl",
Short: "Generate mysql model from ddl",
RunE: command.MysqlDDL,
}
datasourceCmd = &cobra.Command{
Use: "datasource",
Short: "Generate model from datasource",
RunE: command.MySqlDataSource,
}
pgCmd = &cobra.Command{
Use: "pg",
Short: "Generate postgresql model",
RunE: command.PostgreSqlDataSource,
}
mongoCmd = &cobra.Command{
Use: "mongo",
Short: "Generate mongo model",
RunE: mongo.Action,
}
)
func init() {
ddlCmd.Flags().StringVarP(&command.VarStringSrc, "src", "s", "", "The path or path globbing patterns of the ddl")
ddlCmd.Flags().StringVarP(&command.VarStringDir, "dir", "d", "", "The target dir")
ddlCmd.Flags().StringVar(&command.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
ddlCmd.Flags().BoolVarP(&command.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
ddlCmd.Flags().BoolVar(&command.VarBoolIdea, "idea", false, "For idea plugin [optional]")
ddlCmd.Flags().StringVar(&command.VarStringDatabase, "database", "", "The name of database [optional]")
ddlCmd.Flags().StringVar(&command.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")
ddlCmd.Flags().StringVar(&command.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")
ddlCmd.Flags().StringVar(&command.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
datasourceCmd.Flags().StringVar(&command.VarStringURL, "url", "", `The data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`)
datasourceCmd.Flags().StringSliceVarP(&command.VarStringSliceTable, "table", "t", nil, "The table or table globbing patterns in the database")
datasourceCmd.Flags().BoolVarP(&command.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
datasourceCmd.Flags().StringVarP(&command.VarStringDir, "dir", "d", "", "The target dir")
datasourceCmd.Flags().StringVar(&command.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
datasourceCmd.Flags().BoolVar(&command.VarBoolIdea, "idea", false, "For idea plugin [optional]")
datasourceCmd.Flags().StringVar(&command.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")
datasourceCmd.Flags().StringVar(&command.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")
datasourceCmd.Flags().StringVar(&command.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
pgCmd.Flags().StringVar(&command.VarStringURL, "url", "", `The data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`)
pgCmd.Flags().StringVarP(&command.VarStringTable, "table", "t", "", "The table or table globbing patterns in the database")
pgCmd.Flags().StringVarP(&command.VarStringSchema, "schema", "s", "public", "The table schema")
pgCmd.Flags().BoolVarP(&command.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
pgCmd.Flags().StringVarP(&command.VarStringDir, "dir", "d", "", "The target dir")
pgCmd.Flags().StringVar(&command.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
pgCmd.Flags().BoolVar(&command.VarBoolIdea, "idea", false, "For idea plugin [optional]")
pgCmd.Flags().StringVar(&command.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")
pgCmd.Flags().StringVar(&command.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")
pgCmd.Flags().StringVar(&command.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
mongoCmd.Flags().StringSliceVarP(&mongo.VarStringSliceType, "type", "t", nil, "Specified model type name")
mongoCmd.Flags().BoolVarP(&mongo.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
mongoCmd.Flags().StringVarP(&mongo.VarStringDir, "dir", "d", "", "The target dir")
mongoCmd.Flags().StringVar(&mongo.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
mongoCmd.Flags().StringVar(&mongo.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")
mongoCmd.Flags().StringVar(&mongo.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")
mongoCmd.Flags().StringVar(&mongo.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
mysqlCmd.AddCommand(datasourceCmd)
mysqlCmd.AddCommand(ddlCmd)
mysqlCmd.AddCommand(pgCmd)
Cmd.AddCommand(mysqlCmd)
Cmd.AddCommand(mongoCmd)
}

View File

@@ -3,7 +3,6 @@ package generate
import (
"fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo/template"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -30,7 +29,7 @@ func Clean() error {
}
// Templates initializes the mongo templates.
func Templates(_ *cli.Context) error {
func Templates() error {
return pathx.InitTemplates(category, templates)
}

View File

@@ -5,22 +5,39 @@ import (
"path/filepath"
"strings"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/model/mongo/generate"
file "github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
var (
// VarStringSliceType describes a golang data structure name for mongo.
VarStringSliceType []string
// VarStringDir describes an output directory.
VarStringDir string
// VarBoolCache describes whether cache is enabled.
VarBoolCache bool
// VarStringStyle describes the style.
VarStringStyle string
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch.
VarStringBranch string
)
// Action provides the entry for goctl mongo code generation.
func Action(ctx *cli.Context) error {
tp := ctx.StringSlice("type")
c := ctx.Bool("cache")
o := strings.TrimSpace(ctx.String("dir"))
s := ctx.String("style")
home := ctx.String("home")
remote := ctx.String("remote")
branch := ctx.String("branch")
func Action(_ *cobra.Command, _ []string) error {
tp := VarStringSliceType
c := VarBoolCache
o := strings.TrimSpace(VarStringDir)
s := VarStringStyle
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {

View File

@@ -6,7 +6,7 @@ import (
"strings"
"github.com/go-sql-driver/mysql"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/postgres"
"github.com/zeromicro/go-zero/core/stores/sqlx"
@@ -20,35 +20,49 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
const (
flagSrc = "src"
flagDir = "dir"
flagCache = "cache"
flagIdea = "idea"
flagURL = "url"
flagTable = "table"
flagStyle = "style"
flagDatabase = "database"
flagSchema = "schema"
flagHome = "home"
flagRemote = "remote"
flagBranch = "branch"
var (
// VarStringSrc describes the source file of sql.
VarStringSrc string
// VarStringDir describes the output directory of sql.
VarStringDir string
// VarBoolCache describes whether the cache is enabled.
VarBoolCache bool
// VarBoolIdea describes whether is idea or not.
VarBoolIdea bool
// VarStringURL describes the dsn of the sql.
VarStringURL string
// VarStringSliceTable describes tables.
VarStringSliceTable []string
// VarStringTable describes a table of sql.
VarStringTable string
// VarStringStyle describes the style.
VarStringStyle string
// VarStringDatabase describes the database.
VarStringDatabase string
// VarStringSchema describes the schema of postgresql.
VarStringSchema string
// VarStringHome describes the goctl home.
VarStringHome string
// VarStringRemote describes the remote git repository.
VarStringRemote string
// VarStringBranch describes the git branch of the repository.
VarStringBranch string
)
var errNotMatched = errors.New("sql not matched")
// MysqlDDL generates model code from ddl
func MysqlDDL(ctx *cli.Context) error {
migrationnotes.BeforeCommands(ctx)
src := ctx.String(flagSrc)
dir := ctx.String(flagDir)
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
style := ctx.String(flagStyle)
database := ctx.String(flagDatabase)
home := ctx.String(flagHome)
remote := ctx.String(flagRemote)
branch := ctx.String(flagBranch)
func MysqlDDL(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
src := VarStringSrc
dir := VarStringDir
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
database := VarStringDatabase
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@@ -67,16 +81,16 @@ func MysqlDDL(ctx *cli.Context) error {
}
// MySqlDataSource generates model code from datasource
func MySqlDataSource(ctx *cli.Context) error {
migrationnotes.BeforeCommands(ctx)
url := strings.TrimSpace(ctx.String(flagURL))
dir := strings.TrimSpace(ctx.String(flagDir))
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
style := ctx.String(flagStyle)
home := ctx.String(flagHome)
remote := ctx.String(flagRemote)
branch := ctx.String(flagBranch)
func MySqlDataSource(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
url := strings.TrimSpace(VarStringURL)
dir := strings.TrimSpace(VarStringDir)
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@@ -87,7 +101,7 @@ func MySqlDataSource(ctx *cli.Context) error {
pathx.RegisterGoctlHome(home)
}
tableValue := ctx.StringSlice(flagTable)
tableValue := VarStringSliceTable
patterns := parseTableList(tableValue)
cfg, err := config.NewConfig(style)
if err != nil {
@@ -135,17 +149,17 @@ func parseTableList(tableValue []string) pattern {
}
// PostgreSqlDataSource generates model code from datasource
func PostgreSqlDataSource(ctx *cli.Context) error {
migrationnotes.BeforeCommands(ctx)
url := strings.TrimSpace(ctx.String(flagURL))
dir := strings.TrimSpace(ctx.String(flagDir))
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
style := ctx.String(flagStyle)
schema := ctx.String(flagSchema)
home := ctx.String(flagHome)
remote := ctx.String(flagRemote)
branch := ctx.String(flagBranch)
func PostgreSqlDataSource(_ *cobra.Command, _ []string) error {
migrationnotes.BeforeCommands(VarStringDir, VarStringStyle)
url := strings.TrimSpace(VarStringURL)
dir := strings.TrimSpace(VarStringDir)
cache := VarBoolCache
idea := VarBoolIdea
style := VarStringStyle
schema := VarStringSchema
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
if len(remote) > 0 {
repo, _ := file.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@@ -160,7 +174,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
schema = "public"
}
pattern := strings.TrimSpace(ctx.String(flagTable))
pattern := strings.TrimSpace(VarStringTable)
cfg, err := config.NewConfig(style)
if err != nil {
return err

View File

@@ -1,14 +1,13 @@
package migrationnotes
import (
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)
// BeforeCommands run before comamnd run to show some migration notes
func BeforeCommands(ctx *cli.Context) error {
if err := migrateBefore1_3_4(ctx); err != nil {
func BeforeCommands(dir, style string) error {
if err := migrateBefore1_3_4(dir, style); err != nil {
return err
}
return nil

View File

@@ -4,13 +4,9 @@ import (
"fmt"
"io/ioutil"
"strings"
"github.com/urfave/cli"
)
func migrateBefore1_3_4(ctx *cli.Context) error {
dir := ctx.String("dir")
style := ctx.String("style")
func migrateBefore1_3_4(dir, style string) error {
ok, err := needShow1_3_4(dir, style)
if err != nil {
return err

View File

@@ -3,7 +3,6 @@ package gen
import (
"fmt"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/model/sql/template"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
@@ -70,7 +69,7 @@ func Clean() error {
}
// GenTemplates creates template files if not exists
func GenTemplates(_ *cli.Context) error {
func GenTemplates() error {
return pathx.InitTemplates(category, templates)
}