Skip to content

Commit 3280fdf

Browse files
Updated the cobra Usage Template to closely match the ibmcloud one. This will really only effect usage from the cli directly (outside of use with ibmcloud as a plugin).
1 parent 1e18d0d commit 3280fdf

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

plugin/plugin.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"reflect"
77
"regexp"
88
"strings"
9+
"bytes"
10+
"text/template"
911

1012
trace "github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/trace"
1113

@@ -60,8 +62,31 @@ var USEAGE_TEMPLATE = `${COMMAND_NAME} {{if .HasParent}}{{.Parent.CommandPath}}
6062
6163
{{.Long}}`
6264

63-
// Cobra Default Template is: https://github.com/spf13/cobra/blob/v1.8.1/command.go#L546
65+
// https://github.ibm.com/ibmcloud-cli/bluemix-cli/blob/master/bluemix/cli/help.go#L68
66+
// Copied/pasted because I don't want to import the whole bluemix/cli lib just for this
67+
var BX_TEMPLATE = `{{"NAME:" | T | HeaderColor}}
68+
{{.Name}}{{with .Aliases}}{{range .}}, {{.}}{{end}}{{end}} - {{.Short}}
69+
70+
{{"USAGE:" | T | HeaderColor}}
71+
{{UsageCommandString . }}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
72+
{{"Available Commands:" | HeaderColor}}{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
73+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}
74+
{{.Title}}{{range $cmds}}{{if (and (eq .GroupID $group.ID) (or .IsAvailableCommand (eq .Name "help")))}}
75+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if not .AllChildCommandsHaveGroup}}
76+
Additional Commands:{{range $cmds}}{{if (and (eq .GroupID "") (or .IsAvailableCommand (eq .Name "help")))}}
77+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
78+
{{"OPTIONS:" | T | HeaderColor}}
79+
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{if .HasAvailableInheritedFlags}}
80+
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{end}}
81+
`
82+
83+
// Adds HeaderColor to string in a template
84+
func HeaderColor(text string) string {
85+
return terminal.HeaderColor(text)
86+
}
6487

88+
// Cobra Default Template is: https://github.com/spf13/cobra/blob/v1.8.1/command.go#L546
89+
// Used to mark flags that are required in the Usage String
6590
func RequiredFlags(flags *pflag.FlagSet) string {
6691
requiredFlags := ""
6792
flags.VisitAll(func(pflag *pflag.Flag) {
@@ -80,6 +105,20 @@ func RequiredFlags(flags *pflag.FlagSet) string {
80105
})
81106
return requiredFlags
82107
}
108+
109+
// Since we overwrite the cobra Usage template, we need to build it manually.
110+
func UsageCommandString(cmd *cobra.Command) string {
111+
var buf bytes.Buffer
112+
var templateFuncs = template.FuncMap{
113+
"RequiredFlags": RequiredFlags,
114+
}
115+
usage := template.New("usage")
116+
usage.Funcs(templateFuncs)
117+
template.Must(usage.Parse(USEAGE_TEMPLATE))
118+
usage.Execute(&buf, cmd)
119+
return buf.String()
120+
}
121+
83122
func (sl *SoftlayerPlugin) GetMetadata() plugin.PluginMetadata {
84123
return plugin.PluginMetadata{
85124
Name: metadata.NS_SL_NAME,
@@ -276,8 +315,7 @@ func defaultIsZeroValue(f *pflag.Flag) bool {
276315
func cobraToCLIMeta(topCommand *cobra.Command, namespace string) []plugin.Command {
277316
var pluginCommands []plugin.Command
278317
// Custom Usage to ibmcloud CLI prints out a nice messages for us
279-
cobra.AddTemplateFunc("RequiredFlags", RequiredFlags)
280-
topCommand.SetUsageTemplate(USEAGE_TEMPLATE)
318+
281319
for _, cliCmd := range topCommand.Commands() {
282320
if len(cliCmd.Commands()) > 0 {
283321
pluginCommands = append(pluginCommands, cobraToCLIMeta(cliCmd, namespace+" "+cliCmd.Use)...)
@@ -286,7 +324,7 @@ func cobraToCLIMeta(topCommand *cobra.Command, namespace string) []plugin.Comman
286324
Namespace: namespace,
287325
Name: cliCmd.Name(),
288326
Description: cliCmd.Short,
289-
Usage: cliCmd.UsageString(),
327+
Usage: UsageCommandString(cliCmd),
290328
Flags: cobraFlagToPlugin(cliCmd.Flags()),
291329
}
292330
pluginCommands = append(pluginCommands, thisCmd)
@@ -308,7 +346,12 @@ func GetTopCobraCommand(ui terminal.UI, session *session.Session) *cobra.Command
308346
SilenceUsage: true, // Surpresses help text on errors
309347
SilenceErrors: true,
310348
}
311-
349+
// This is to mock the `ibmcloud` usage string. Not perfect, but its close to what you can expect
350+
cobra.AddTemplateFunc("UsageCommandString", UsageCommandString)
351+
cobra.AddTemplateFunc("HeaderColor", HeaderColor)
352+
cobra.AddTemplateFunc("T", T)
353+
cobraCmd.SetUsageTemplate(BX_TEMPLATE)
354+
cobraCmd.SetHelpTemplate(`{{.UsageString}}`)
312355
versionCommand := &cobra.Command{
313356
Use: "version",
314357
Short: T("Print the version of the sl plugin"),

0 commit comments

Comments
 (0)