-
Notifications
You must be signed in to change notification settings - Fork 21
/
usage.go
137 lines (110 loc) · 3.78 KB
/
usage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package cli
import (
"io"
"os"
"text/template"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/tanzu-plugin-runtime/component"
)
// CmdMap is the map of command groups to plugins
type CmdMap map[string][]*cobra.Command
// MainUsage create the main usage display for tanzu cli.
type MainUsage struct{}
// NewMainUsage creates an instance of Usage.
func NewMainUsage() *MainUsage {
return &MainUsage{}
}
// UsageFunc generates a usage func for cobra.
func (u *MainUsage) UsageFunc() func(*cobra.Command) error {
return func(c *cobra.Command) error {
return u.GenerateDescriptor(c, os.Stdout)
}
}
// GenerateDescriptor generates a descriptor
func (u *MainUsage) GenerateDescriptor(c *cobra.Command, w io.Writer) error {
cmdMap := CmdMap{}
for _, cmd := range c.Commands() {
if cmd.Hidden || cmd.Deprecated != "" {
continue
}
group := cmd.Annotations["group"]
if group == "" {
continue
}
g, ok := cmdMap[group]
if !ok {
g = []*cobra.Command{}
}
g = append(g, cmd)
cmdMap[group] = g
}
d := struct {
*cobra.Command
CmdMap CmdMap
}{
c,
cmdMap,
}
t := template.Must(template.New("usage").Funcs(TemplateFuncs).Parse(u.Template()))
err := t.Execute(w, d)
if err != nil {
return err
}
return nil
}
// Template returns the template for the root cmd help as well
// as for the two target commands ('kubernetes' and 'mission-control').
// Those three commands use this template because they use command groups
// for their help text.
func (u *MainUsage) Template() string {
return `{{ bold "Usage:" }}
{{.Command.CommandPath}} [command]{{if gt (len .Aliases) 0}}
{{ bold "Aliases:" }}
{{.NameAndAliases}}{{end}}{{if .HasExample}}
{{ bold "Examples:" }}
{{.Example}}{{end}}{{if gt (len .CmdMap) 0}}
{{ bold "Available command groups:" }}
{{ range $group, $cmds := .CmdMap}}
{{ bold $group }}{{ range $cmd := $cmds }}
{{rpad $cmd.Name 24}}{{$cmd.Short}} {{end}}{{end}}{{end}}
{{ bold "Flags:" }}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}
Use "{{.CommandPath}} [command] --help" for more information about a command.
`
}
// SubCmdUsageFunc is the usage func for all core sub-commands.
var SubCmdUsageFunc = func(c *cobra.Command) error {
t, err := template.New("usage").Funcs(TemplateFuncs).Parse(SubCmdTemplate)
if err != nil {
return err
}
return t.Execute(os.Stdout, c)
}
// SubCmdTemplate is the template for all core sub-commands.
const SubCmdTemplate = `{{ bold "Usage:" }}{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
{{ bold "Aliases:" }}
{{.NameAndAliases}}{{end}}{{if .HasExample}}
{{ bold "Examples:" }}
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
{{ bold "Available Commands:" }}{{range .Commands}}{{if .IsAvailableCommand }}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{ bold "Flags:" }}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{ bold "Global Flags:" }}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{ bold "Additional help topics:" }}{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
`
// TemplateFuncs are the template usage funcs.
var TemplateFuncs = template.FuncMap{
"rpad": component.Rpad,
"bold": component.Bold,
"underline": component.Underline,
"trimTrailingWhitespaces": component.TrimRightSpace,
"beginsWith": component.BeginsWith,
}