Skip to content

Commit be1218e

Browse files
committed
adjust print font color
1 parent 0a8ffad commit be1218e

24 files changed

+151
-120
lines changed

cmd/sponge/commands/generate/cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/fatih/color"
78
"github.com/spf13/cobra"
89

910
"github.com/zhufuyi/sponge/pkg/replacer"
@@ -28,7 +29,7 @@ func CacheCommand(parentName string) *cobra.Command {
2829
cmd := &cobra.Command{
2930
Use: "cache",
3031
Short: "Generate cache code",
31-
Long: fmt.Sprintf(`generate cache code.
32+
Long: color.HiBlackString(fmt.Sprintf(`generate cache code.
3233
3334
Examples:
3435
# generate kv cache code
@@ -38,7 +39,7 @@ Examples:
3839
sponge %s cache --module-name=yourModuleName --cache-name=token --prefix-key=user:token: --key-name=id --key-type=uint64 --value-name=token --value-type=string --out=./yourServerDir
3940
4041
# if you want the generated code to suited to mono-repo, you need to specify the parameter --suited-mono-repo=true --serverName=yourServerName
41-
`, parentName, parentName),
42+
`, parentName, parentName)),
4243
SilenceErrors: true,
4344
SilenceUsage: true,
4445
RunE: func(cmd *cobra.Command, args []string) error {
@@ -101,8 +102,7 @@ using help:
101102
_ = cmd.MarkFlagRequired("value-type")
102103
cmd.Flags().StringVarP(&serverName, "server-name", "s", "", "server name")
103104
cmd.Flags().BoolVarP(&suitedMonoRepo, "suited-mono-repo", "l", false, "whether the generated code is suitable for mono-repo")
104-
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./cache_<time>,"+
105-
" if you specify the directory where the web or microservice generated by sponge, the module-name flag can be ignored")
105+
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./cache_<time>, "+flagTip("module-name"))
106106

107107
return cmd
108108
}

cmd/sponge/commands/generate/common.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
const (
23-
defaultGoModVersion = "go 1.19"
23+
defaultGoModVersion = "go 1.20"
2424

2525
// TplNameSponge name of the template
2626
TplNameSponge = "sponge"
@@ -54,6 +54,12 @@ const (
5454
codeNameGRPCHTTP = "grpc-http-pb"
5555
codeNameGRPCConn = "grpc-conn"
5656
codeNameProtobuf = "protobuf"
57+
58+
wellPrefix = "## "
59+
mgoSuffix = ".mgo"
60+
pkgPathSuffix = "/pkg"
61+
expSuffix = ".exp"
62+
apiDocsSuffix = " api docs"
5763
)
5864

5965
var (
@@ -713,3 +719,14 @@ func getLocalGoVersion() string {
713719

714720
return localGoVersion
715721
}
722+
723+
func dbDriverErr(driver string) error {
724+
return errors.New("unsupported db driver: " + driver)
725+
}
726+
727+
func flagTip(name ...string) string {
728+
if len(name) == 2 {
729+
return fmt.Sprintf("if you specify the directory where the web or microservice generated by sponge, the %s and %s flag can be ignored", name[0], name[1])
730+
}
731+
return fmt.Sprintf("if you specify the directory where the web or microservice generated by sponge, the %s flag can be ignored", name[0])
732+
}

cmd/sponge/commands/generate/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/fatih/color"
1112
"github.com/spf13/cobra"
1213

1314
"github.com/zhufuyi/sponge/pkg/gofile"
@@ -29,15 +30,15 @@ func ConfigCommand() *cobra.Command {
2930
cmd := &cobra.Command{
3031
Use: "config",
3132
Short: "Generate go config code from yaml file",
32-
Long: `generate go config code from yaml file.
33+
Long: color.HiBlackString(`generate go config code from yaml file.
3334
3435
Examples:
3536
# generate config code in server directory, the yaml configuration file must be in <yourServerDir>/configs directory.
3637
sponge config --server-dir=/yourServerDir
3738
3839
# generate config code from yaml file.
3940
sponge config --yaml-file=yourConfig.yml
40-
`,
41+
`),
4142
SilenceErrors: true,
4243
SilenceUsage: true,
4344
RunE: func(cmd *cobra.Command, args []string) error {

cmd/sponge/commands/generate/configmap.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/fatih/color"
78
"github.com/huandu/xstrings"
89
"github.com/spf13/cobra"
910

@@ -22,15 +23,15 @@ func ConfigmapCommand() *cobra.Command {
2223
cmd := &cobra.Command{
2324
Use: "configmap",
2425
Short: "Generate k8s configmap",
25-
Long: `generate k8s configmap.
26+
Long: color.HiBlackString(`generate k8s configmap.
2627
2728
Examples:
2829
# generate k8s configmap
2930
sponge configmap --server-name=yourServerName --project-name=yourProjectName --config-file=yourConfigFile.yml
3031
3132
# generate grpc connection code and specify the server directory, Note: code generation will be canceled when the latest generated file already exists.
3233
sponge configmap --server-name=yourServerName --project-name=yourProjectName --config-file=yourConfigFile.yml --out=./yourServerDir
33-
`,
34+
`),
3435
SilenceErrors: true,
3536
SilenceUsage: true,
3637
RunE: func(cmd *cobra.Command, args []string) error {

cmd/sponge/commands/generate/dao.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/fatih/color"
89
"github.com/spf13/cobra"
910

1011
"github.com/zhufuyi/sponge/pkg/replacer"
@@ -33,7 +34,7 @@ func DaoCommand(parentName string) *cobra.Command {
3334
cmd := &cobra.Command{
3435
Use: "dao",
3536
Short: "Generate dao code based on sql",
36-
Long: fmt.Sprintf(`generate dao code based on sql.
37+
Long: color.HiBlackString(fmt.Sprintf(`generate dao code based on sql.
3738
3839
Examples:
3940
# generate dao code.
@@ -49,7 +50,7 @@ Examples:
4950
sponge %s dao --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --out=./yourServerDir
5051
5152
# if you want the generated code to suited to mono-repo, you need to specify the parameter --suited-mono-repo=true --serverName=yourServerName
52-
`, parentName, parentName, parentName, parentName),
53+
`, parentName, parentName, parentName, parentName)),
5354
SilenceErrors: true,
5455
SilenceUsage: true,
5556
RunE: func(cmd *cobra.Command, args []string) error {
@@ -130,8 +131,7 @@ using help:
130131
cmd.Flags().StringVarP(&serverName, "server-name", "s", "", "server name")
131132
cmd.Flags().BoolVarP(&suitedMonoRepo, "suited-mono-repo", "l", false, "whether the generated code is suitable for mono-repo")
132133
cmd.Flags().IntVarP(&sqlArgs.JSONNamedType, "json-name-type", "j", 1, "json tags name type, 0:snake case, 1:camel case")
133-
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./dao_<time>, "+
134-
"if you specify the directory where the web or microservice generated by sponge, the module-name flag can be ignored")
134+
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./dao_<time>, "+flagTip("module-name"))
135135
cmd.Flags().BoolVarP(&isIncludeInitDB, "include-init-db", "i", false, "if true, includes mysql and redis initialization code")
136136

137137
return cmd
@@ -201,7 +201,7 @@ func (g *daoGenerator) generateCode() (string, error) {
201201
}
202202

203203
default:
204-
return "", errors.New("unsupported db driver: " + g.dbDriver)
204+
return "", dbDriverErr(g.dbDriver)
205205
}
206206

207207
subFiles = append(subFiles, getSubFiles(selectFiles, replaceFiles)...)
@@ -243,7 +243,7 @@ func (g *daoGenerator) addFields(r replacer.Replacer) []replacer.Field {
243243
New: g.moduleName,
244244
},
245245
{
246-
Old: g.moduleName + "/pkg",
246+
Old: g.moduleName + pkgPathSuffix,
247247
New: "github.com/zhufuyi/sponge/pkg",
248248
},
249249
{
@@ -277,8 +277,8 @@ func daoExtendedAPI(r replacer.Replacer) (map[string][]string, []replacer.Field)
277277
}
278278
var fields []replacer.Field
279279

280-
fields = append(fields, deleteFieldsMark(r, daoFile+".exp", startMark, endMark)...)
281-
fields = append(fields, deleteFieldsMark(r, daoTestFile+".exp", startMark, endMark)...)
280+
fields = append(fields, deleteFieldsMark(r, daoFile+expSuffix, startMark, endMark)...)
281+
fields = append(fields, deleteFieldsMark(r, daoTestFile+expSuffix, startMark, endMark)...)
282282

283283
fields = append(fields, []replacer.Field{
284284
{
@@ -306,7 +306,7 @@ func daoMongoDBExtendedAPI(r replacer.Replacer) (map[string][]string, []replacer
306306

307307
var fields []replacer.Field
308308

309-
fields = append(fields, deleteFieldsMark(r, daoMgoFile+".exp", startMark, endMark)...)
309+
fields = append(fields, deleteFieldsMark(r, daoMgoFile+expSuffix, startMark, endMark)...)
310310

311311
fields = append(fields, []replacer.Field{
312312
{

cmd/sponge/commands/generate/grpc-http-pb.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/fatih/color"
78
"github.com/huandu/xstrings"
89
"github.com/spf13/cobra"
910

@@ -26,7 +27,7 @@ func GRPCAndHTTPPbCommand() *cobra.Command {
2627
cmd := &cobra.Command{
2728
Use: "grpc-http-pb",
2829
Short: "Generate grpc+http service code based on protobuf file",
29-
Long: `generate grpc+http service code based on protobuf file.
30+
Long: color.HiBlackString(`generate grpc+http service code based on protobuf file.
3031
3132
Examples:
3233
# generate grpc service code.
@@ -39,7 +40,7 @@ Examples:
3940
sponge micro grpc-http-pb --module-name=yourModuleName --server-name=yourServerName --project-name=yourProjectName --repo-addr=192.168.3.37:9443/user-name --protobuf-file=./demo.proto
4041
4142
# if you want the generated code to suited to mono-repo, you need to specify the parameter --suited-mono-repo=true
42-
`,
43+
`),
4344
SilenceErrors: true,
4445
SilenceUsage: true,
4546
RunE: func(cmd *cobra.Command, args []string) error {
@@ -196,7 +197,7 @@ func (g *httpAndGRPCPbGenerator) addFields(r replacer.Replacer) []replacer.Field
196197
fields = append(fields, deleteAllFieldsMark(r, protoShellFile, wellStartMark, wellEndMark)...)
197198
fields = append(fields, deleteAllFieldsMark(r, appConfigFile, wellStartMark, wellEndMark)...)
198199
//fields = append(fields, deleteFieldsMark(r, deploymentConfigFile, wellStartMark, wellEndMark)...)
199-
fields = append(fields, replaceFileContentMark(r, readmeFile, "## "+g.serverName)...)
200+
fields = append(fields, replaceFileContentMark(r, readmeFile, wellPrefix+g.serverName)...)
200201
fields = append(fields, []replacer.Field{
201202
{ // replace the configuration of the *.yml file
202203
Old: appConfigFileMark,
@@ -255,7 +256,7 @@ func (g *httpAndGRPCPbGenerator) addFields(r replacer.Replacer) []replacer.Field
255256
New: g.moduleName,
256257
},
257258
{
258-
Old: g.moduleName + "/pkg",
259+
Old: g.moduleName + pkgPathSuffix,
259260
New: "github.com/zhufuyi/sponge/pkg",
260261
},
261262
{ // replace the sponge version of the go.mod file
@@ -264,7 +265,7 @@ func (g *httpAndGRPCPbGenerator) addFields(r replacer.Replacer) []replacer.Field
264265
},
265266
{
266267
Old: "sponge api docs",
267-
New: g.serverName + " api docs",
268+
New: g.serverName + apiDocsSuffix,
268269
},
269270
{
270271
Old: defaultGoModVersion,

cmd/sponge/commands/generate/handler-pb.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math/rand"
77
"strings"
88

9+
"github.com/fatih/color"
910
"github.com/spf13/cobra"
1011

1112
"github.com/zhufuyi/sponge/pkg/gofile"
@@ -35,7 +36,7 @@ func HandlerPbCommand() *cobra.Command {
3536
cmd := &cobra.Command{
3637
Use: "handler-pb",
3738
Short: "Generate handler and protobuf CRUD code based on sql",
38-
Long: `generate handler and protobuf CRUD code based on sql.
39+
Long: color.HiBlackString(`generate handler and protobuf CRUD code based on sql.
3940
4041
Examples:
4142
# generate handler and protobuf code.
@@ -49,7 +50,7 @@ Examples:
4950
5051
# generate handler and protobuf code and specify the server directory, Note: code generation will be canceled when the latest generated file already exists.
5152
sponge web handler-pb --db-driver=mysql --db-dsn=root:123456@(192.168.3.37:3306)/test --db-table=user --out=./yourServerDir
52-
`,
53+
`),
5354
SilenceErrors: true,
5455
SilenceUsage: true,
5556
RunE: func(cmd *cobra.Command, args []string) error {
@@ -128,8 +129,7 @@ using help:
128129
cmd.Flags().BoolVarP(&sqlArgs.IsExtendedAPI, "extended-api", "a", false, "whether to generate extended crud api, additional includes: DeleteByIDs, GetByCondition, ListByIDs, ListByLatestID")
129130
cmd.Flags().BoolVarP(&suitedMonoRepo, "suited-mono-repo", "l", false, "whether the generated code is suitable for mono-repo")
130131
cmd.Flags().IntVarP(&sqlArgs.JSONNamedType, "json-name-type", "j", 1, "json tags name type, 0:snake case, 1:camel case")
131-
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./handler-pb_<time>,"+
132-
" if you specify the directory where the web or microservice generated by sponge, the module-name and server-name flag can be ignored")
132+
cmd.Flags().StringVarP(&outPath, "out", "o", "", "output directory, default is ./handler-pb_<time>, "+flagTip("module-name", "server-name"))
133133

134134
return cmd
135135
}
@@ -209,11 +209,11 @@ func (g *handlerPbGenerator) generateCode() (string, error) {
209209
"userExample_logic.go.mgo",
210210
},
211211
}
212-
g.fields = append(g.fields, deleteFieldsMark(r, handlerLogicFile+".mgo", startMark, endMark)...)
212+
g.fields = append(g.fields, deleteFieldsMark(r, handlerLogicFile+mgoSuffix, startMark, endMark)...)
213213
}
214214

215215
default:
216-
return "", errors.New("unsupported db driver: " + g.dbDriver)
216+
return "", dbDriverErr(g.dbDriver)
217217
}
218218

219219
subFiles = append(subFiles, getSubFiles(selectFiles, replaceFiles)...)
@@ -283,7 +283,7 @@ func (g *handlerPbGenerator) addFields(r replacer.Replacer) []replacer.Field {
283283
New: fmt.Sprintf("userExampleNO = %d", rand.Intn(99)+1),
284284
},
285285
{
286-
Old: g.moduleName + "/pkg",
286+
Old: g.moduleName + pkgPathSuffix,
287287
New: "github.com/zhufuyi/sponge/pkg",
288288
},
289289
{
@@ -341,9 +341,9 @@ func handlerPbExtendedAPI(r replacer.Replacer) (map[string][]string, []replacer.
341341

342342
var fields []replacer.Field
343343

344-
fields = append(fields, deleteFieldsMark(r, daoFile+".exp", startMark, endMark)...)
345-
fields = append(fields, deleteFieldsMark(r, daoTestFile+".exp", startMark, endMark)...)
346-
fields = append(fields, deleteFieldsMark(r, handlerLogicFile+".exp", startMark, endMark)...)
344+
fields = append(fields, deleteFieldsMark(r, daoFile+expSuffix, startMark, endMark)...)
345+
fields = append(fields, deleteFieldsMark(r, daoTestFile+expSuffix, startMark, endMark)...)
346+
fields = append(fields, deleteFieldsMark(r, handlerLogicFile+expSuffix, startMark, endMark)...)
347347

348348
fields = append(fields, []replacer.Field{
349349
{
@@ -389,7 +389,7 @@ func handlerPbMongoDBExtendedAPI(r replacer.Replacer) (map[string][]string, []re
389389

390390
var fields []replacer.Field
391391

392-
fields = append(fields, deleteFieldsMark(r, daoMgoFile+".exp", startMark, endMark)...)
392+
fields = append(fields, deleteFieldsMark(r, daoMgoFile+expSuffix, startMark, endMark)...)
393393
fields = append(fields, deleteFieldsMark(r, handlerLogicFile+".mgo.exp", startMark, endMark)...)
394394

395395
fields = append(fields, []replacer.Field{

0 commit comments

Comments
 (0)