Skip to content

Commit

Permalink
Set command displayname annotation when command is mapped
Browse files Browse the repository at this point in the history
When a plugin is mapped via PluginDescriptor.InvokedAs, set the command
display name annotation introduced in cobra 1.8.0. This enables the
runtime to construct more accurate usage text based on the the mapped
command name instead.

Also, updated aliases computation in the usage output to take advantage
of said annotation.

Signed-off-by: Vui Lam <[email protected]>
  • Loading branch information
vuil committed Mar 6, 2024
1 parent de3e17d commit 8c009dd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
23 changes: 22 additions & 1 deletion plugin/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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() {
Expand Down
15 changes: 11 additions & 4 deletions plugin/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c009dd

Please sign in to comment.