Skip to content

Commit

Permalink
optimize: consts (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoranz758 authored Oct 9, 2023
1 parent 250700d commit 1feb5d6
Show file tree
Hide file tree
Showing 26 changed files with 288 additions and 246 deletions.
11 changes: 6 additions & 5 deletions cmd/dynamic/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/client"
"github.com/cloudwego/cwgo/pkg/common/utils"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/pkg/model"
"github.com/cloudwego/cwgo/pkg/server"
"github.com/cloudwego/hertz/cmd/hz/meta"
Expand All @@ -31,10 +32,10 @@ import (

var generateType = []*survey.Question{
{
Name: "type",
Name: consts.ServiceType,
Prompt: &survey.MultiSelect{
Message: "Select generate type",
Options: []string{Server, Client, DB},
Options: []string{consts.Server, consts.Client, consts.DB},
},
Validate: survey.Required,
},
Expand All @@ -54,7 +55,7 @@ func Terminal(*cli.Context) error {

cfg := &dfConfig{}
// ask whether generate server project
if _, ok := typeMap[Server]; ok {
if _, ok := typeMap[consts.Server]; ok {
sa := config.NewServerArgument()
err = survey.Ask(commonQuestion(), sa.CommonParam)
if err != nil {
Expand Down Expand Up @@ -88,7 +89,7 @@ func Terminal(*cli.Context) error {

num := &cNum{}
// ask whether generate client project
if _, ok := typeMap[Client]; ok {
if _, ok := typeMap[consts.Client]; ok {
err = survey.Ask(clientNum, num)
if err != nil {
return err
Expand Down Expand Up @@ -130,7 +131,7 @@ func Terminal(*cli.Context) error {
}

// ask whether generate db project
if _, ok := typeMap[DB]; ok {
if _, ok := typeMap[consts.DB]; ok {
da := config.NewModelArgument()
err = survey.Ask(dbConfig, da)
if err != nil {
Expand Down
22 changes: 8 additions & 14 deletions cmd/dynamic/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@ package dynamic

import (
"github.com/AlecAivazis/survey/v2"
"github.com/cloudwego/cwgo/config"
)

const (
Server = "server"
Client = "client"
DB = "db"
"github.com/cloudwego/cwgo/pkg/consts"
)

func commonQuestion() []*survey.Question {
return []*survey.Question{
{
Name: "type",
Name: consts.ServiceType,
Prompt: &survey.Select{
Message: "Select service type",
Options: []string{"RPC", "HTTP"},
Options: []string{consts.RPC, consts.HTTP},
},
Validate: survey.Required,
},
{
Name: "service",
Name: consts.Service,
Prompt: &survey.Input{
Message: "Please input service name",
},
Expand Down Expand Up @@ -76,7 +70,7 @@ func defaultConfig() []*survey.Question {

func protoSearch() []*survey.Question {
return []*survey.Question{{
Name: "proto_search_path",
Name: consts.ProtoSearchPath,
Prompt: &survey.Input{
Message: "Please input proto search path if exists, space as separator",
},
Expand All @@ -86,7 +80,7 @@ func protoSearch() []*survey.Question {
func customConfig() []*survey.Question {
return []*survey.Question{
{
Name: "pass",
Name: consts.Pass,
Prompt: &survey.Input{
Message: "Please input custom param",
},
Expand All @@ -97,10 +91,10 @@ func customConfig() []*survey.Question {
func registryConfig() []*survey.Question {
return []*survey.Question{
{
Name: "registry",
Name: consts.Registry,
Prompt: &survey.Select{
Message: "Please select a registry",
Options: []string{config.Zk, config.Polaris, config.Etcd, config.Nacos},
Options: []string{consts.Zk, consts.Polaris, consts.Etcd, consts.Nacos},
},
Validate: survey.Required,
},
Expand Down
27 changes: 14 additions & 13 deletions cmd/dynamic/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (

"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/common/utils"
"github.com/cloudwego/cwgo/pkg/consts"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
)

var dbConfig = []*survey.Question{
{
Name: "type",
Name: consts.ServiceType,
Prompt: &survey.Select{
Message: "Select db type",
Options: []string{"MySQL", "SQLServer", "SQLite", "PostgreSQL"},
Options: []string{string(consts.MySQL), string(consts.SQLServer), string(consts.Sqlite), string(consts.Postgres)},
},
Validate: survey.Required,
Transform: func(ans interface{}) (newAns interface{}) {
Expand All @@ -54,7 +55,7 @@ type ps struct {

var pass = []*survey.Question{
{
Name: "pass",
Name: consts.Pass,
Prompt: &survey.Input{
Message: "Please input custom param",
},
Expand All @@ -63,17 +64,17 @@ var pass = []*survey.Question{

func parsePass(da *config.ModelArgument, pass string) error {
f := flag.NewFlagSet("", flag.ContinueOnError)
f.StringVar(&da.OutPath, config.OutDir, "biz/dal/query", "")
f.StringVar(&da.OutFile, config.OutFile, "gen.go", "")
f.BoolVar(&da.WithUnitTest, config.UnitTest, false, "")
f.BoolVar(&da.OnlyModel, config.OnlyModel, false, "")
f.StringVar(&da.ModelPkgName, config.ModelPkgName, "", "")
f.BoolVar(&da.FieldNullable, config.Nullable, false, "")
f.BoolVar(&da.FieldSignable, config.Signable, false, "")
f.BoolVar(&da.FieldWithTypeTag, config.TypeTag, false, "")
f.BoolVar(&da.FieldWithIndexTag, config.IndexTag, false, "")
f.StringVar(&da.OutPath, consts.OutDir, consts.DefaultDbOutDir, "")
f.StringVar(&da.OutFile, consts.OutFile, consts.DefaultDbOutFile, "")
f.BoolVar(&da.WithUnitTest, consts.UnitTest, false, "")
f.BoolVar(&da.OnlyModel, consts.OnlyModel, false, "")
f.StringVar(&da.ModelPkgName, consts.ModelPkgName, "", "")
f.BoolVar(&da.FieldNullable, consts.Nullable, false, "")
f.BoolVar(&da.FieldSignable, consts.Signable, false, "")
f.BoolVar(&da.FieldWithTypeTag, consts.TypeTag, false, "")
f.BoolVar(&da.FieldWithIndexTag, consts.IndexTag, false, "")
var tables utils.FlagStringSlice
f.Var(&tables, config.Tables, "")
f.Var(&tables, consts.Tables, "")
if err := f.Parse(utils.StringSliceSpilt([]string{pass})); err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions cmd/static/client_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ package static

import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func clientFlags() []cli.Flag {
globalArgs := config.GetGlobalArgs()
return []cli.Flag{
&cli.StringFlag{Name: config.Service, Usage: "Specify the service name.", Destination: &globalArgs.ClientArgument.Service},
&cli.StringFlag{Name: config.ServiceType, Usage: "Specify the generate type. (RPC or HTTP)", Value: config.RPC},
&cli.StringFlag{Name: config.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod.", Destination: &globalArgs.ClientArgument.GoMod},
&cli.StringFlag{Name: config.IDLPath, Usage: "Specify the IDL file path. (.thrift or .proto)", Destination: &globalArgs.ClientArgument.IdlPath},
&cli.StringFlag{Name: config.OutDir, Aliases: []string{"o"}, Usage: "Specify the output path.", Destination: &globalArgs.ClientArgument.OutDir},
&cli.StringFlag{Name: config.Template, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`", Destination: &globalArgs.ClientArgument.Template},
&cli.StringFlag{Name: config.Registry, Usage: "Specify the registry, default is None"},
&cli.StringSliceFlag{Name: config.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes. (Valid only if idl is protobuf)"},
&cli.StringSliceFlag{Name: config.Pass, Usage: "pass param to hz or kitex"},
&cli.BoolFlag{Name: config.Verbose, Usage: "Turn on verbose mode."},
&cli.StringFlag{Name: consts.Service, Usage: "Specify the service name.", Destination: &globalArgs.ClientArgument.Service},
&cli.StringFlag{Name: consts.ServiceType, Usage: "Specify the generate type. (RPC or HTTP)", Value: consts.RPC},
&cli.StringFlag{Name: consts.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod.", Destination: &globalArgs.ClientArgument.GoMod},
&cli.StringFlag{Name: consts.IDLPath, Usage: "Specify the IDL file path. (.thrift or .proto)", Destination: &globalArgs.ClientArgument.IdlPath},
&cli.StringFlag{Name: consts.OutDir, Aliases: []string{"o"}, Usage: "Specify the output path.", Destination: &globalArgs.ClientArgument.OutDir},
&cli.StringFlag{Name: consts.Template, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`", Destination: &globalArgs.ClientArgument.Template},
&cli.StringFlag{Name: consts.Registry, Usage: "Specify the registry, default is None"},
&cli.StringSliceFlag{Name: consts.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes. (Valid only if idl is protobuf)"},
&cli.StringSliceFlag{Name: consts.Pass, Usage: "pass param to hz or kitex"},
&cli.BoolFlag{Name: consts.Verbose, Usage: "Turn on verbose mode."},
}
}
16 changes: 11 additions & 5 deletions cmd/static/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/meta"
"github.com/cloudwego/cwgo/pkg/client"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/pkg/fallback"
"github.com/cloudwego/cwgo/pkg/model"
"github.com/cloudwego/cwgo/pkg/server"
Expand All @@ -32,11 +33,11 @@ func Init() *cli.App {
verboseFlag := cli.BoolFlag{Name: "verbose,vv", Usage: "turn on verbose mode"}

app := cli.NewApp()
app.Name = "cwgo"
app.Usage = "All in one tools for CloudWeGo"
app.Name = meta.Name
app.Usage = AppUsage
app.Version = meta.Version
// The default separator for multiple parameters is modified to ";"
app.SliceFlagSeparator = ";"
app.SliceFlagSeparator = consts.Comma

// global flags
app.Flags = []cli.Flag{
Expand Down Expand Up @@ -87,8 +88,8 @@ func Init() *cli.App {
},
},
{
Name: "fallback",
Usage: "fallback to hz or kitex",
Name: FallbackName,
Usage: FallbackUsage,
Action: func(c *cli.Context) error {
if err := globalArgs.FallbackArgument.ParseCli(c); err != nil {
return err
Expand All @@ -101,6 +102,8 @@ func Init() *cli.App {
}

const (
AppUsage = "All in one tools for CloudWeGo"

ServerName = "server"
ServerUsage = `generate RPC or HTTP server
Expand Down Expand Up @@ -133,4 +136,7 @@ Examples:

InitName = "init"
InitUsage = `interactive terminals provide a more user-friendly experience for generating code`

FallbackName = "fallback"
FallbackUsage = "fallback to hz or kitex"
)
27 changes: 14 additions & 13 deletions cmd/static/model_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,33 @@ import (
"strings"

"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func modelFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{Name: config.DSN, Usage: "Specify the database source name. (https://gorm.io/docs/connecting_to_the_database.html)", Value: "", DefaultText: "", Action: func(context *cli.Context, s string) error {
&cli.StringFlag{Name: consts.DSN, Usage: "Specify the database source name. (https://gorm.io/docs/connecting_to_the_database.html)", Value: "", DefaultText: "", Action: func(context *cli.Context, s string) error {
if len(s) == 0 {
return fmt.Errorf("dsn cannot be empty")
}
return nil
}},
&cli.StringFlag{Name: config.DBType, Usage: "Specify database type. (mysql or sqlserver or sqlite or postgres)", Value: "mysql", DefaultText: "mysql", Action: func(context *cli.Context, s string) error {
if _, ok := config.OpenTypeFuncMap[config.DataBaseType(strings.ToLower(s))]; !ok {
&cli.StringFlag{Name: consts.DBType, Usage: "Specify database type. (mysql or sqlserver or sqlite or postgres)", Value: string(consts.MySQL), DefaultText: string(consts.MySQL), Action: func(context *cli.Context, s string) error {
if _, ok := config.OpenTypeFuncMap[consts.DataBaseType(strings.ToLower(s))]; !ok {
return fmt.Errorf("unknow db type %s (support mysql || postgres || sqlite || sqlserver for now)", s)
}
return nil
}},
&cli.StringFlag{Name: config.OutDir, Usage: "Specify output directory", Value: "biz/dal/query", DefaultText: "biz/dao/query"},
&cli.StringFlag{Name: config.OutFile, Usage: "Specify output filename", Value: "gen.go", DefaultText: "gen.go"},
&cli.StringSliceFlag{Name: config.Tables, Usage: "Specify databases tables"},
&cli.BoolFlag{Name: config.UnitTest, Usage: "Specify generate unit test", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: config.OnlyModel, Usage: "Specify only generate model code", Value: false, DefaultText: "false"},
&cli.StringFlag{Name: config.ModelPkgName, Usage: "Specify model package name", Value: "", DefaultText: ""},
&cli.BoolFlag{Name: config.Nullable, Usage: "Specify generate with pointer when field is nullable", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: config.Signable, Usage: "Specify detect integer field's unsigned type, adjust generated data type", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: config.TypeTag, Usage: "Specify generate field with gorm column type tag", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: config.IndexTag, Usage: "Specify generate field with gorm index tag", Value: false, DefaultText: "false"},
&cli.StringFlag{Name: consts.OutDir, Usage: "Specify output directory", Value: consts.DefaultDbOutDir, DefaultText: consts.DefaultDbOutDir},
&cli.StringFlag{Name: consts.OutFile, Usage: "Specify output filename", Value: consts.DefaultDbOutFile, DefaultText: consts.DefaultDbOutFile},
&cli.StringSliceFlag{Name: consts.Tables, Usage: "Specify databases tables"},
&cli.BoolFlag{Name: consts.UnitTest, Usage: "Specify generate unit test", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: consts.OnlyModel, Usage: "Specify only generate model code", Value: false, DefaultText: "false"},
&cli.StringFlag{Name: consts.ModelPkgName, Usage: "Specify model package name", Value: "", DefaultText: ""},
&cli.BoolFlag{Name: consts.Nullable, Usage: "Specify generate with pointer when field is nullable", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: consts.Signable, Usage: "Specify detect integer field's unsigned type, adjust generated data type", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: consts.TypeTag, Usage: "Specify generate field with gorm column type tag", Value: false, DefaultText: "false"},
&cli.BoolFlag{Name: consts.IndexTag, Usage: "Specify generate field with gorm index tag", Value: false, DefaultText: "false"},
}
}
23 changes: 12 additions & 11 deletions cmd/static/server_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ package static

import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func serverFlags() []cli.Flag {
globalArgs := config.GetGlobalArgs()
return []cli.Flag{
&cli.StringFlag{Name: config.Service, Usage: "Specify the service name.", Destination: &globalArgs.ServerArgument.Service},
&cli.StringFlag{Name: config.ServiceType, Usage: "Specify the generate type. (RPC or HTTP)", Value: config.RPC},
&cli.StringFlag{Name: config.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod.", Destination: &globalArgs.ServerArgument.GoMod},
&cli.StringFlag{Name: config.IDLPath, Usage: "Specify the IDL file path. (.thrift or .proto)", Destination: &globalArgs.ServerArgument.IdlPath},
&cli.StringFlag{Name: config.OutDir, Value: ".", Aliases: []string{"o"}, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`.", Destination: &globalArgs.ServerArgument.OutDir},
&cli.StringFlag{Name: config.Template, Usage: "Specify the layout template.", Destination: &globalArgs.ServerArgument.Template},
&cli.StringFlag{Name: config.Registry, Usage: "Specify the registry, default is None."},
&cli.StringSliceFlag{Name: config.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes."},
&cli.StringSliceFlag{Name: config.Pass, Usage: "Pass param to hz or Kitex."},
&cli.BoolFlag{Name: config.Verbose, Usage: "Turn on verbose mode."},
&cli.BoolFlag{Name: config.HexTag, Usage: "Add HTTP listen for Kitex.", Destination: &globalArgs.Hex},
&cli.StringFlag{Name: consts.Service, Usage: "Specify the service name.", Destination: &globalArgs.ServerArgument.Service},
&cli.StringFlag{Name: consts.ServiceType, Usage: "Specify the generate type. (RPC or HTTP)", Value: consts.RPC},
&cli.StringFlag{Name: consts.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod.", Destination: &globalArgs.ServerArgument.GoMod},
&cli.StringFlag{Name: consts.IDLPath, Usage: "Specify the IDL file path. (.thrift or .proto)", Destination: &globalArgs.ServerArgument.IdlPath},
&cli.StringFlag{Name: consts.OutDir, Value: ".", Aliases: []string{"o"}, Usage: "Specify the output path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`.", Destination: &globalArgs.ServerArgument.OutDir},
&cli.StringFlag{Name: consts.Template, Usage: "Specify the layout template.", Destination: &globalArgs.ServerArgument.Template},
&cli.StringFlag{Name: consts.Registry, Usage: "Specify the registry, default is None."},
&cli.StringSliceFlag{Name: consts.ProtoSearchPath, Aliases: []string{"I"}, Usage: "Add an IDL search path for includes."},
&cli.StringSliceFlag{Name: consts.Pass, Usage: "Pass param to hz or Kitex."},
&cli.BoolFlag{Name: consts.Verbose, Usage: "Turn on verbose mode."},
&cli.BoolFlag{Name: consts.HexTag, Usage: "Add HTTP listen for Kitex.", Destination: &globalArgs.Hex},
}
}
Loading

0 comments on commit 1feb5d6

Please sign in to comment.