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

@@ -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