Pārlūkot izejas kodu

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
anqiansong 2 gadi atpakaļ
vecāks
revīzija
93b3f5030f

+ 2 - 2
tools/goctl/api/cmd.go

@@ -95,7 +95,7 @@ func init() {
 		"higher priority")
 	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"+
-		" 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")
 	Cmd.Flags().StringVar(&apigen.VarStringBranch, "branch", "", "The branch of the "+
 		"remote repo, it does work with --remote")
@@ -122,7 +122,7 @@ func init() {
 		"has higher priority")
 	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"+
-		" 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")
 	goCmd.Flags().StringVar(&gogen.VarStringBranch, "branch", "", "The branch of "+
 		"the remote repo, it does work with --remote")

+ 23 - 5
tools/goctl/cmd/root.go

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

+ 60 - 0
tools/goctl/cmd/usage.go

@@ -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))
+}

+ 23 - 0
tools/goctl/cmd/usage.tpl

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

+ 1 - 1
tools/goctl/docker/cmd.go

@@ -27,7 +27,7 @@ func init() {
 	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().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(&varStringVersion, "version", "", "The goctl builder golang image version")
 	Cmd.Flags().StringVar(&varStringTZ, "tz", "Asia/Shanghai", "The timezone of the container")

+ 1 - 1
tools/goctl/kube/cmd.go

@@ -58,7 +58,7 @@ func init() {
 	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")
 	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")
 	deployCmd.Flags().StringVar(&varStringBranch, "branch", "", "The branch of the remote repo, it "+
 		"does work with --remote")

+ 3 - 3
tools/goctl/model/cmd.go

@@ -57,7 +57,7 @@ func init() {
 	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.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")
 
 	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().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.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")
 
 	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().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.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")
 
 	mysqlCmd.AddCommand(datasourceCmd)

+ 4 - 4
tools/goctl/rpc/cmd.go

@@ -38,7 +38,7 @@ var (
 func init() {
 	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.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")
 
 	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, "+
 		"--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, "+
-		"--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")
 	newCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+
 		"does work with --remote")
@@ -67,7 +67,7 @@ func init() {
 	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")
 	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")
 	protocCmd.Flags().StringVar(&cli.VarStringBranch, "branch", "", "The branch of the remote repo, it "+
 		"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.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")
 
 	Cmd.AddCommand(newCmd)