Feat: refactor vela help (#5895)

Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
Somefive
2023-04-21 14:19:36 +08:00
committed by GitHub
parent 5549619ef9
commit dab1618eef
7 changed files with 40 additions and 70 deletions
+5 -5
View File
@@ -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
+1 -11
View File
@@ -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) {
+3 -1
View File
@@ -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
+28 -36
View File
@@ -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()
+1 -1
View File
@@ -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),
+1 -1
View File
@@ -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]",
+1 -15
View File
@@ -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