github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/help.go (about)

     1  package commands
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/kubeshop/testkube/pkg/ui"
    12  )
    13  
    14  const (
    15  	cmdGroupAnnotation = "GroupAnnotation"
    16  	cmdMngmCmdGroup    = "1-Management commands"
    17  	cmdGroupCommands   = "2-Commands"
    18  	cmdGroupCobra      = "other"
    19  
    20  	cmdGroupDelimiter = "-"
    21  )
    22  
    23  func NewHelpCmd() *cobra.Command {
    24  	return &cobra.Command{
    25  		Use:   "help",
    26  		Short: "Help about any command",
    27  		Long:  "Display the available commands and flags",
    28  		Run: func(cmd *cobra.Command, args []string) {
    29  			ui.Print(RootCmd.Short)
    30  			ui.NL()
    31  			ui.Print(ui.LightGray("Usage"))
    32  			ui.Printf(fmt.Sprintf("%s %s", ui.White(RootCmd.Use), ui.LightGray("[flags]")))
    33  			ui.Printf("%s %s", ui.White(RootCmd.Use), ui.LightGray("[command]"))
    34  			ui.NL()
    35  			usage := helpMessageByGroups(RootCmd)
    36  			ui.Print(usage)
    37  			ui.Print(ui.LightGray("Flags"))
    38  			ui.Printf(RootCmd.Flags().FlagUsages())
    39  			ui.Print(ui.LightGray("Use \"kubectl testkube [command] --help\" for more information about a command."))
    40  			ui.NL()
    41  			ui.Printf("%s   %s", ui.LightGray("Docs & Support:"), ui.White("https://docs.testkube.io"))
    42  			ui.NL()
    43  		},
    44  	}
    45  }
    46  
    47  func helpMessageByGroups(cmd *cobra.Command) string {
    48  
    49  	groups := map[string][]string{}
    50  	for _, c := range cmd.Commands() {
    51  		var groupName string
    52  		v, ok := c.Annotations[cmdGroupAnnotation]
    53  		if !ok {
    54  			groupName = cmdGroupCobra
    55  		} else {
    56  			groupName = v
    57  		}
    58  
    59  		groupCmds := groups[groupName]
    60  		groupCmds = append(groupCmds, fmt.Sprintf("%-16s%s", c.Name(), ui.LightGray(c.Short)))
    61  		sort.Strings(groupCmds)
    62  
    63  		groups[groupName] = groupCmds
    64  	}
    65  
    66  	if len(groups[cmdGroupCobra]) != 0 {
    67  		groups[cmdMngmCmdGroup] = append(groups[cmdMngmCmdGroup], groups[cmdGroupCobra]...)
    68  	}
    69  	delete(groups, cmdGroupCobra)
    70  
    71  	groupNames := []string{}
    72  	for k := range groups {
    73  		groupNames = append(groupNames, k)
    74  	}
    75  	sort.Strings(groupNames)
    76  
    77  	buf := bytes.Buffer{}
    78  	for _, groupName := range groupNames {
    79  		commands := groups[groupName]
    80  
    81  		groupSplit := strings.Split(groupName, cmdGroupDelimiter)
    82  		group := "others"
    83  		if len(groupSplit) > 1 {
    84  			group = strings.Split(groupName, cmdGroupDelimiter)[1]
    85  		}
    86  		buf.WriteString(fmt.Sprintf("%s\n", ui.LightGray(group)))
    87  
    88  		for _, cmd := range commands {
    89  			buf.WriteString(fmt.Sprintf("%s\n", cmd))
    90  		}
    91  		buf.WriteString("\n")
    92  	}
    93  	return buf.String()
    94  }