github.com/oam-dev/kubevela@v1.9.11/references/cli/help.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cli
    18  
    19  import (
    20  	"fmt"
    21  	"math"
    22  	"strconv"
    23  
    24  	"github.com/kubevela/pkg/util/slices"
    25  	"github.com/spf13/cobra"
    26  	"k8s.io/kubectl/pkg/util/i18n"
    27  
    28  	"github.com/oam-dev/kubevela/apis/types"
    29  )
    30  
    31  // NewHelpCommand get any command help
    32  func NewHelpCommand(order string) *cobra.Command {
    33  	cmd := &cobra.Command{
    34  		Use:                   "help",
    35  		DisableFlagsInUseLine: true,
    36  		Short:                 i18n.T("Help about any command."),
    37  		Example:               "help [command] | STRING_TO_SEARCH",
    38  		Run:                   RunHelp,
    39  		Annotations: map[string]string{
    40  			types.TagCommandType:  types.TypeAuxiliary,
    41  			types.TagCommandOrder: order,
    42  		},
    43  	}
    44  	return cmd
    45  }
    46  
    47  // RunHelp exec help [command]
    48  func RunHelp(cmd *cobra.Command, args []string) {
    49  	runHelp(cmd, cmd.Root().Commands(), args)
    50  }
    51  
    52  func runHelp(cmd *cobra.Command, allCommands []*cobra.Command, args []string) {
    53  	if len(args) == 0 {
    54  		cmd.Printf("A Highly Extensible Platform Engine based on Kubernetes and Open Application Model.\n\n")
    55  		for _, t := range []string{types.TypeStart, types.TypeApp, types.TypeCD, types.TypePlatform, types.TypeExtension, types.TypeSystem, types.TypeAuxiliary} {
    56  			PrintHelpByTag(cmd, allCommands, t)
    57  		}
    58  		cmd.Println("Flags:")
    59  		cmd.Println("  -h, --help   help for vela")
    60  		cmd.Println()
    61  		cmd.Println(`Use "vela [command] --help" for more information about a command.`)
    62  	} else {
    63  		foundCmd, _, err := cmd.Root().Find(args)
    64  		if foundCmd != nil && err == nil {
    65  			foundCmd.HelpFunc()(foundCmd, args)
    66  		}
    67  	}
    68  }
    69  
    70  // Printable is a struct for print help
    71  type Printable struct {
    72  	Order int64
    73  	Use   string
    74  	Desc  string
    75  }
    76  
    77  // NewPrintable create printable object
    78  func NewPrintable(c *cobra.Command, desc string) Printable {
    79  	order, err := strconv.ParseInt(c.Annotations[types.TagCommandOrder], 10, 64)
    80  	if err != nil {
    81  		order = math.MaxInt
    82  	}
    83  	return Printable{Order: order, Use: c.Use, Desc: desc}
    84  }
    85  
    86  // PrintHelpByTag print custom defined help message
    87  func PrintHelpByTag(cmd *cobra.Command, all []*cobra.Command, tag string) {
    88  	table := newUITable()
    89  	table.MaxColWidth = 80
    90  	var pl []Printable
    91  	for _, c := range all {
    92  		if c.Hidden || c.IsAdditionalHelpTopicCommand() {
    93  			continue
    94  		}
    95  		if val, ok := c.Annotations[types.TagCommandType]; ok && val == tag {
    96  			pl = append(pl, NewPrintable(c, c.Short))
    97  		}
    98  	}
    99  	if len(all) == 0 {
   100  		return
   101  	}
   102  	slices.Sort(pl, func(i, j Printable) bool { return i.Order < j.Order })
   103  	cmd.Println(tag + ":")
   104  	for _, v := range pl {
   105  		table.AddRow(fmt.Sprintf("  %-15s", v.Use), v.Desc)
   106  	}
   107  	cmd.Println(table.String())
   108  	cmd.Println()
   109  }