chore: Add command desc & color commands (#2013)

* Add link & Color sub-commands

* Color sub-commands for unix-like OS

* Remove useless code

* Remove redundant dependency
master
anqiansong 2 years ago committed by GitHub
parent b44e8f5c75
commit 93b3f5030f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -95,7 +95,7 @@ func init() {
"higher priority") "higher priority")
Cmd.Flags().StringVar(&apigen.VarStringRemote, "remote", "", "The remote git repo of the"+ Cmd.Flags().StringVar(&apigen.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"+ " 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"+ " priority\nThe git repo directory must be consistent with the"+
" https://github.com/zeromicro/go-zero-template directory structure") " https://github.com/zeromicro/go-zero-template directory structure")
Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+ Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+
"remote repo, it does work with --remote") "remote repo, it does work with --remote")
@ -122,7 +122,7 @@ func init() {
"has higher priority") "has higher priority")
goCmd.Flags().StringVar(&gogen.VarStringRemote, "remote", "", "The remote git repo "+ goCmd.Flags().StringVar(&gogen.VarStringRemote, "remote", "", "The remote git repo "+
"of the template, --home and --remote cannot be set at the same time, if they are, --remote"+ "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 "+ " has higher priority\nThe git repo directory must be consistent with the "+
"https://github.com/zeromicro/go-zero-template directory structure") "https://github.com/zeromicro/go-zero-template directory structure")
goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+ goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
"the remote repo, it does work with --remote") "the remote repo, it does work with --remote")

@ -1,10 +1,12 @@
package cmd package cmd
import ( import (
_ "embed"
"fmt" "fmt"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"text/template"
"github.com/logrusorgru/aurora" "github.com/logrusorgru/aurora"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -30,11 +32,18 @@ const (
assign = "=" assign = "="
) )
var rootCmd = &cobra.Command{ var (
Use: "goctl", //go:embed usage.tpl
Short: "A cli tool to generate go-zero code", usageTpl string
Long: "A cli tool to generate api, zrpc, model code",
} rootCmd = &cobra.Command{
Use: "goctl",
Short: "A cli tool to generate go-zero code",
Long: "A cli tool to generate api, zrpc, model code\n\n" +
"GitHub: https://github.com/zeromicro/go-zero\n" +
"Site: https://go-zero.dev",
}
)
// Execute executes the given command // Execute executes the given command
func Execute() { func Execute() {
@ -96,9 +105,18 @@ func isBuiltin(name string) bool {
} }
func init() { func init() {
cobra.AddTemplateFuncs(template.FuncMap{
"blue": blue,
"green": green,
"rpadx": rpadx,
"rainbow": rainbow,
})
rootCmd.Version = fmt.Sprintf( rootCmd.Version = fmt.Sprintf(
"%s %s/%s", version.BuildVersion, "%s %s/%s", version.BuildVersion,
runtime.GOOS, runtime.GOARCH) runtime.GOOS, runtime.GOARCH)
rootCmd.SetUsageTemplate(usageTpl)
rootCmd.AddCommand(api.Cmd) rootCmd.AddCommand(api.Cmd)
rootCmd.AddCommand(bug.Cmd) rootCmd.AddCommand(bug.Cmd)
rootCmd.AddCommand(docker.Cmd) rootCmd.AddCommand(docker.Cmd)

@ -0,0 +1,60 @@
package cmd
import (
"fmt"
"runtime"
"github.com/logrusorgru/aurora"
"github.com/zeromicro/go-zero/tools/goctl/vars"
)
var colorRender = []func(v interface{}) string{
func(v interface{}) string {
return aurora.BrightRed(v).String()
},
func(v interface{}) string {
return aurora.BrightGreen(v).String()
},
func(v interface{}) string {
return aurora.BrightYellow(v).String()
},
func(v interface{}) string {
return aurora.BrightBlue(v).String()
},
func(v interface{}) string {
return aurora.BrightMagenta(v).String()
},
func(v interface{}) string {
return aurora.BrightCyan(v).String()
},
}
func blue(s string) string {
if runtime.GOOS == vars.OsWindows {
return s
}
return aurora.BrightBlue(s).String()
}
func green(s string) string {
if runtime.GOOS == vars.OsWindows {
return s
}
return aurora.BrightGreen(s).String()
}
func rainbow(s string) string {
if runtime.GOOS == vars.OsWindows {
return s
}
s0 := s[0]
return colorRender[int(s0)%(len(colorRender)-1)](s)
}
// rpadx adds padding to the right of a string.
func rpadx(s string, padding int) string {
template := fmt.Sprintf("%%-%ds", padding)
return rainbow(fmt.Sprintf(template, s))
}

@ -0,0 +1,23 @@
{{blue "Usage:"}}{{if .Runnable}}
{{green .UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{green .CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
{{blue "Aliases:"}}
{{green .NameAndAliases}}{{end}}{{if .HasExample}}
{{blue "Examples:"}}
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
{{blue "Available Commands:"}}{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpadx .Name .NamePadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{blue "Flags:"}}
{{green .LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{blue "Global Flags:"}}
{{green .InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{blue "Additional help topics:"}}{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{green .CommandPath}} [command] --help" for more information about a command.{{end}}

@ -27,7 +27,7 @@ func init() {
Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch") Cmd.Flags().StringVar(&varStringBase, "base", "scratch", "The base image to build the docker image, default scratch")
Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none") Cmd.Flags().IntVar(&varIntPort, "port", 0, "The port to expose, default none")
Cmd.Flags().StringVar(&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") Cmd.Flags().StringVar(&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")
Cmd.Flags().StringVar(&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") Cmd.Flags().StringVar(&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\nThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
Cmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote") Cmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
Cmd.Flags().StringVar(&varStringVersion, "version", "", "The goctl builder golang image version") Cmd.Flags().StringVar(&varStringVersion, "version", "", "The goctl builder golang image version")
Cmd.Flags().StringVar(&varStringTZ, "tz", "Asia/Shanghai", "The timezone of the container") Cmd.Flags().StringVar(&varStringTZ, "tz", "Asia/Shanghai", "The timezone of the container")

@ -58,7 +58,7 @@ func init() {
deployCmd.Flags().StringVar(&varStringHome, "home", "", "The goctl home path of the template, "+ deployCmd.Flags().StringVar(&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") "--home and --remote cannot be set at the same time, if they are, --remote has higher priority")
deployCmd.Flags().StringVar(&varStringRemote, "remote", "", "The remote git repo of the template, "+ deployCmd.Flags().StringVar(&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 "+ "--home and --remote cannot be set at the same time, if they are, --remote has higher priority\nThe git repo "+
"directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure") "directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
deployCmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it "+ deployCmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it "+
"does work with --remote") "does work with --remote")

@ -57,7 +57,7 @@ func init() {
ddlCmd.Flags().BoolVar(&command.VarBoolIdea, "idea", false, "For idea plugin [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.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.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.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\nThe 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") 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().StringVar(&command.VarStringURL, "url", "", `The data source of database,like "root:password@tcp(127.0.0.1:3306)/database"`)
@ -67,7 +67,7 @@ func init() {
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().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().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.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.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\nThe 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") datasourceCmd.Flags().StringVar(&command.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
pgDatasourceCmd.Flags().StringVar(&command.VarStringURL, "url", "", `The data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`) pgDatasourceCmd.Flags().StringVar(&command.VarStringURL, "url", "", `The data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`)
@ -86,7 +86,7 @@ func init() {
mongoCmd.Flags().StringVarP(&mongo.VarStringDir, "dir", "d", "", "The target dir") 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.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.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.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\nThe 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") mongoCmd.Flags().StringVar(&mongo.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
mysqlCmd.AddCommand(datasourceCmd) mysqlCmd.AddCommand(datasourceCmd)

@ -38,7 +38,7 @@ var (
func init() { func init() {
Cmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file") Cmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file")
Cmd.Flags().StringVar(&cli.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") Cmd.Flags().StringVar(&cli.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")
Cmd.Flags().StringVar(&cli.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") Cmd.Flags().StringVar(&cli.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\nThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
Cmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote") Cmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
newCmd.Flags().StringSliceVar(&cli.VarStringSliceGoOpt, "go_opt", nil, "") newCmd.Flags().StringSliceVar(&cli.VarStringSliceGoOpt, "go_opt", nil, "")
@ -48,7 +48,7 @@ func init() {
newCmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home path of the template, "+ newCmd.Flags().StringVar(&cli.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") "--home and --remote cannot be set at the same time, if they are, --remote has higher priority")
newCmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote git repo of the template, "+ newCmd.Flags().StringVar(&cli.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 "+ "--home and --remote cannot be set at the same time, if they are, --remote has higher priority\nThe git repo "+
"directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure") "directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
newCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+ newCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+
"does work with --remote") "does work with --remote")
@ -67,7 +67,7 @@ func init() {
protocCmd.Flags().StringVar(&cli.VarStringHome, "home", "", "The goctl home path of the template, "+ protocCmd.Flags().StringVar(&cli.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") "--home and --remote cannot be set at the same time, if they are, --remote has higher priority")
protocCmd.Flags().StringVar(&cli.VarStringRemote, "remote", "", "The remote git repo of the template, "+ protocCmd.Flags().StringVar(&cli.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 "+ "--home and --remote cannot be set at the same time, if they are, --remote has higher priority\nThe git repo "+
"directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure") "directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
protocCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+ protocCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+
"does work with --remote") "does work with --remote")
@ -81,7 +81,7 @@ func init() {
templateCmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file") templateCmd.Flags().StringVar(&cli.VarStringOutput, "o", "", "Output a sample proto file")
templateCmd.Flags().StringVar(&cli.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") templateCmd.Flags().StringVar(&cli.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")
templateCmd.Flags().StringVar(&cli.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") templateCmd.Flags().StringVar(&cli.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\nThe git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure")
templateCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote") templateCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it does work with --remote")
Cmd.AddCommand(newCmd) Cmd.AddCommand(newCmd)

Loading…
Cancel
Save