diff --git a/plugin/root.go b/plugin/root.go index 1982021e..f058f361 100644 --- a/plugin/root.go +++ b/plugin/root.go @@ -4,10 +4,30 @@ package plugin import ( + "strings" + "github.com/spf13/cobra" ) +func getPluginInvokedAs(descriptor *PluginDescriptor) string { + var invokedAsString string + name := descriptor.Name + + if len(descriptor.InvokedAs) != 0 { + invokedAsString = strings.TrimSpace(descriptor.InvokedAs[0]) + } + + if invokedAsString != "" { + cmdParts := strings.Split(invokedAsString, " ") + name = cmdParts[len(cmdParts)-1] + } + + return name +} + func newRootCmd(descriptor *PluginDescriptor) *cobra.Command { + cmdName := getPluginInvokedAs(descriptor) + cmd := &cobra.Command{ Use: descriptor.Name, Short: descriptor.Description, @@ -26,7 +46,8 @@ func newRootCmd(descriptor *PluginDescriptor) *cobra.Command { HiddenDefaultCmd: true, }, Annotations: map[string]string{ - "target": string(descriptor.Target), + "target": string(descriptor.Target), + cobra.CommandDisplayNameAnnotation: cmdName, }, } cobra.AddTemplateFuncs(TemplateFuncs) diff --git a/plugin/usage.go b/plugin/usage.go index 0b1d0948..9516d72d 100644 --- a/plugin/usage.go +++ b/plugin/usage.go @@ -132,6 +132,14 @@ func formatHelpFooter(cmd *cobra.Command, target types.Target) string { return footer.String() } +func aliasesWithMappedName(cmd *cobra.Command) string { + cmdName := cmd.Name() + if v, ok := cmd.Annotations[cobra.CommandDisplayNameAnnotation]; ok { + cmdName = v + } + return strings.Join(append([]string{cmdName}, cmd.Aliases...), ", ") +} + func printHelp(cmd *cobra.Command) string { var output strings.Builder target := types.StringToTarget(cmd.Annotations["target"]) @@ -140,7 +148,7 @@ func printHelp(cmd *cobra.Command) string { if len(cmd.Aliases) > 0 { output.WriteString("\n" + component.Bold(aliasesStr) + "\n") - output.WriteString(indentStr + cmd.NameAndAliases() + "\n") + output.WriteString(indentStr + aliasesWithMappedName(cmd) + "\n") } if cmd.HasExample() { diff --git a/plugin/usage_test.go b/plugin/usage_test.go index c76bf1f0..c9a3b84b 100644 --- a/plugin/usage_test.go +++ b/plugin/usage_test.go @@ -71,13 +71,14 @@ func SampleTestPlugin(t *testing.T, target types.Target) *Plugin { } var descriptor = PluginDescriptor{ - Name: "test", + Name: "testNotUserVisible", Target: target, Aliases: []string{"t"}, Description: "Test the CLI", Group: AdminCmdGroup, Version: "v1.1.0", BuildSHA: "1234567", + InvokedAs: []string{"test"}, } var local string @@ -140,6 +141,12 @@ func TestGlobalTestPluginCommandHelpText(t *testing.T) { got := string(<-c) + // note: reference to the unmapped name, as in + // + // '-h, --help help for testNotUserVisible' + // + // is a known bug in cobra 1.8.0 that should be fixed in the next patch or + // minor release expected := `Test the CLI Usage: @@ -157,7 +164,7 @@ Available Commands: Flags: -e, --env string env to test - -h, --help help for test + -h, --help help for testNotUserVisible Additional help topics: test plugin Plugin tests @@ -218,7 +225,7 @@ Available Commands: Flags: -e, --env string env to test - -h, --help help for test + -h, --help help for testNotUserVisible Additional help topics: test plugin Plugin tests @@ -279,7 +286,7 @@ Available Commands: Flags: -e, --env string env to test - -h, --help help for test + -h, --help help for testNotUserVisible Additional help topics: test plugin Plugin tests