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
This commit is contained in:
@@ -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
tools/goctl/cmd/usage.go
Normal file
60
tools/goctl/cmd/usage.go
Normal file
@@ -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
tools/goctl/cmd/usage.tpl
Normal file
23
tools/goctl/cmd/usage.tpl
Normal file
@@ -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}}
|
||||
Reference in New Issue
Block a user