From dab1618eef7da0304684ffd44b560524e88a8bb8 Mon Sep 17 00:00:00 2001 From: Somefive Date: Fri, 21 Apr 2023 14:19:36 +0800 Subject: [PATCH] Feat: refactor vela help (#5895) Signed-off-by: Somefive --- hack/docgen/cli/gen.go | 10 +++--- references/cli/cli.go | 12 +------ references/cli/exec.go | 4 ++- references/cli/help.go | 64 +++++++++++++++-------------------- references/cli/logs.go | 2 +- references/cli/portforward.go | 2 +- references/cli/status.go | 16 +-------- 7 files changed, 40 insertions(+), 70 deletions(-) diff --git a/hack/docgen/cli/gen.go b/hack/docgen/cli/gen.go index 99ce25a28..9c159b1a5 100644 --- a/hack/docgen/cli/gen.go +++ b/hack/docgen/cli/gen.go @@ -23,9 +23,9 @@ import ( "log" "os" "path/filepath" - "sort" "strings" + "github.com/kubevela/pkg/util/slices" "github.com/spf13/cobra" "github.com/spf13/cobra/doc" @@ -36,7 +36,7 @@ import ( // PrintCLIByTag print custom defined index func PrintCLIByTag(cmd *cobra.Command, all []*cobra.Command, tag string) string { var result string - pl := cli.PrintList{} + var pl []cli.Printable for _, c := range all { if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { continue @@ -47,14 +47,14 @@ func PrintCLIByTag(cmd *cobra.Command, all []*cobra.Command, tag string) string cname := cmd.Name() + " " + c.Name() link := cname link = strings.Replace(link, " ", "_", -1) - pl = append(pl, cli.Printable{Order: c.Annotations[types.TagCommandOrder], Long: fmt.Sprintf("* [%s](%s)\t - %s\n", cname, link, c.Long)}) + pl = append(pl, cli.Printable{Order: c.Annotations[types.TagCommandOrder], Short: fmt.Sprintf("* [%s](%s)\t - %s\n", cname, link, c.Long)}) } - sort.Sort(pl) + slices.Sort(pl, func(i, j cli.Printable) bool { return i.Order < j.Order }) for _, v := range pl { - result += v.Long + result += v.Short } result += "\n" return result diff --git a/references/cli/cli.go b/references/cli/cli.go index 04861552c..fb8bef0f5 100644 --- a/references/cli/cli.go +++ b/references/cli/cli.go @@ -52,17 +52,7 @@ func NewCommandWithIOStreams(ioStream util.IOStreams) *cobra.Command { Use: "vela", DisableFlagParsing: true, Run: func(cmd *cobra.Command, args []string) { - allCommands := cmd.Commands() - cmd.Printf("A Highly Extensible Platform Engine based on Kubernetes and Open Application Model.\n\nUsage:\n vela [flags]\n vela [command]\n\nAvailable Commands:\n\n") - PrintHelpByTag(cmd, allCommands, types.TypeStart) - PrintHelpByTag(cmd, allCommands, types.TypeApp) - PrintHelpByTag(cmd, allCommands, types.TypeCD) - PrintHelpByTag(cmd, allCommands, types.TypeExtension) - PrintHelpByTag(cmd, allCommands, types.TypeSystem) - cmd.Println("Flags:") - cmd.Println(" -h, --help help for vela") - cmd.Println() - cmd.Println(`Use "vela [command] --help" for more information about a command.`) + runHelp(cmd, cmd.Commands(), nil) }, SilenceUsage: true, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/references/cli/exec.go b/references/cli/exec.go index d8a4dbcfb..47f78a5f0 100644 --- a/references/cli/exec.go +++ b/references/cli/exec.go @@ -86,7 +86,7 @@ func NewExecCommand(c common.Args, order string, ioStreams util.IOStreams) *cobr }, } cmd := &cobra.Command{ - Use: "exec [flags] APP_NAME -- COMMAND [args...]", + Use: "exec", Short: "Execute command in a container", Long: "Execute command inside container based vela application.", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { @@ -128,6 +128,8 @@ func NewExecCommand(c common.Args, order string, ioStreams util.IOStreams) *cobr types.TagCommandType: types.TypeApp, }, Example: ` + exec [flags] APP_NAME -- COMMAND [args...] + # Get output from running 'date' command from app pod, using the first container by default vela exec my-app -- date diff --git a/references/cli/help.go b/references/cli/help.go index f914104f2..00195165c 100644 --- a/references/cli/help.go +++ b/references/cli/help.go @@ -17,19 +17,21 @@ limitations under the License. package cli import ( - "sort" + "fmt" + + "github.com/kubevela/pkg/util/slices" + "github.com/spf13/cobra" + "k8s.io/kubectl/pkg/util/i18n" "github.com/oam-dev/kubevela/apis/types" - - "github.com/spf13/cobra" ) // NewHelpCommand get any command help func NewHelpCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "help [command] ", + Use: "help [command] | STRING_TO_SEARCH", DisableFlagsInUseLine: true, - Short: "Help about any command", + Short: i18n.T("Help about any command"), Run: RunHelp, } return cmd @@ -37,18 +39,23 @@ func NewHelpCommand() *cobra.Command { // RunHelp exec help [command] func RunHelp(cmd *cobra.Command, args []string) { + runHelp(cmd, cmd.Root().Commands(), args) +} + +func runHelp(cmd *cobra.Command, allCommands []*cobra.Command, args []string) { if len(args) == 0 { - allCommands := cmd.Root().Commands() - // print error message at first, since it can contain suggestions - cmd.Printf("A Highly Extensible Platform Engine based on Kubernetes and Open Application Model.\n\nUsage:\n vela [flags]\n vela [command]\n\nAvailable Commands:\n\n") - PrintHelpByTag(cmd, allCommands, types.TypeStart) - PrintHelpByTag(cmd, allCommands, types.TypeApp) - PrintHelpByTag(cmd, allCommands, types.TypeExtension) - PrintHelpByTag(cmd, allCommands, types.TypeSystem) + cmd.Printf("A Highly Extensible Platform Engine based on Kubernetes and Open Application Model.\n\n") + for _, t := range []string{types.TypeStart, types.TypeApp, types.TypeCD, types.TypeExtension, types.TypeSystem} { + PrintHelpByTag(cmd, allCommands, t) + } + cmd.Println("Flags:") + cmd.Println(" -h, --help help for vela") + cmd.Println() + cmd.Println(`Use "vela [command] --help" for more information about a command.`) } else { foundCmd, _, err := cmd.Root().Find(args) if foundCmd != nil && err == nil { - foundCmd.HelpFunc()(cmd, args) + foundCmd.HelpFunc()(foundCmd, args) } } } @@ -56,45 +63,30 @@ func RunHelp(cmd *cobra.Command, args []string) { // Printable is a struct for print help type Printable struct { Order string - use string - Long string -} - -// PrintList is a list of Printable -type PrintList []Printable - -func (p PrintList) Len() int { - return len(p) -} -func (p PrintList) Swap(i, j int) { - p[i], p[j] = p[j], p[i] -} -func (p PrintList) Less(i, j int) bool { - return p[i].Order > p[j].Order + Use string + Short string } // PrintHelpByTag print custom defined help message func PrintHelpByTag(cmd *cobra.Command, all []*cobra.Command, tag string) { table := newUITable() - var pl PrintList + table.MaxColWidth = 80 + var pl []Printable for _, c := range all { if c.Hidden || c.IsAdditionalHelpTopicCommand() { continue } if val, ok := c.Annotations[types.TagCommandType]; ok && val == tag { - pl = append(pl, Printable{Order: c.Annotations[types.TagCommandOrder], use: c.Use, Long: c.Long}) + pl = append(pl, Printable{Order: c.Annotations[types.TagCommandOrder], Use: c.Use, Short: c.Short}) } } if len(all) == 0 { return } - cmd.Println(" " + tag + ":") - cmd.Println() - - sort.Sort(pl) - + slices.Sort(pl, func(i, j Printable) bool { return i.Order < j.Order }) + cmd.Println(tag + ":") for _, v := range pl { - table.AddRow(" "+v.use, v.Long) + table.AddRow(fmt.Sprintf(" %-15s", v.Use), v.Short) } cmd.Println(table.String()) cmd.Println() diff --git a/references/cli/logs.go b/references/cli/logs.go index 0ec6363db..13d2114f1 100644 --- a/references/cli/logs.go +++ b/references/cli/logs.go @@ -41,7 +41,7 @@ var re = regexp.MustCompile(`"((?:[^"\\]|\\.)*)"`) func NewLogsCommand(c common.Args, order string, ioStreams util.IOStreams) *cobra.Command { largs := &Args{Args: c} cmd := &cobra.Command{ - Use: "logs APP_NAME", + Use: "logs", Short: "Tail logs for application.", Long: "Tail logs for vela application.", Args: cobra.ExactArgs(1), diff --git a/references/cli/portforward.go b/references/cli/portforward.go index 5f8c3000c..1b3382e8d 100644 --- a/references/cli/portforward.go +++ b/references/cli/portforward.go @@ -85,7 +85,7 @@ func NewPortForwardCommand(c common.Args, order string, ioStreams util.IOStreams }, } cmd := &cobra.Command{ - Use: "port-forward APP_NAME", + Use: "port-forward", Short: "Forward local ports to container/service port of vela application.", Long: "Forward local ports to container/service port of vela application.", Example: "port-forward APP_NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]", diff --git a/references/cli/status.go b/references/cli/status.go index 40f47e2e9..4b6e5c761 100644 --- a/references/cli/status.go +++ b/references/cli/status.go @@ -61,20 +61,6 @@ import ( // HealthStatus represents health status strings. type HealthStatus = v1alpha2.HealthStatus -const ( - // HealthStatusNotDiagnosed means there's no health scope referred or unknown health status returned - HealthStatusNotDiagnosed HealthStatus = "NOT DIAGNOSED" -) - -const ( - // HealthStatusHealthy represents healthy status. - HealthStatusHealthy = v1alpha2.StatusHealthy - // HealthStatusUnhealthy represents unhealthy status. - HealthStatusUnhealthy = v1alpha2.StatusUnhealthy - // HealthStatusUnknown represents unknown status. - HealthStatusUnknown = v1alpha2.StatusUnknown -) - // WorkloadHealthCondition holds health status of any resource type WorkloadHealthCondition = v1alpha2.WorkloadHealthCondition @@ -101,7 +87,7 @@ func NewAppStatusCommand(c common.Args, order string, ioStreams cmdutil.IOStream var outputFormat string var detail bool cmd := &cobra.Command{ - Use: "status APP_NAME", + Use: "status", Short: "Show status of an application.", Long: "Show status of vela application.", Example: ` # Get basic app info