Skip to content
Myr

Support usage as plugin for tools like kubectl (#2018)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In this case the executable is `kubectl-plugin`, but we run it as: kubectl plugin And the help text should reflect the actual usage of the command. To create a plugin, add the cobra.CommandDisplayNameAnnotation: rootCmd := &cobra.Command{ Use: "plugin", Annotations: map[string]string{ cobra.CommandDisplayNameAnnotation: "kubectl plugin", } } Internally this change modifies CommandPath() for the root command to return the command display name instead of the command name. This is used for error messages, help text generation, and completions. CommandPath() is expected to have spaces and code using it already handle spaces (e.g replacing with _), so hopefully this does not break anything. Fixes: #2017

authored by Nir Soffer · committed by GitHub · parent 48cea5c · Browse files · 2 files+38 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Nir Soffer <nsoffer@redhat.com>

Diff stats

2 files+38 added−2 removed

  • other:+38 added−2 removed

2 files+38 added−2 removed

modified:command_test.go+30 added
364 unmodified lines
365366 EnablePrefixMatching = defaultPrefixMatching367}368369// TestPlugin checks usage as plugin for another command such as kubectl. The370// executable is `kubectl-plugin`, but we run it as `kubectl plugin`. The help371// text should reflect the way we run the command.372func TestPlugin(t *testing.T) {373 rootCmd := &Command{374 Use: "plugin",375 Args: NoArgs,376 Annotations: map[string]string{377 CommandDisplayNameAnnotation: "kubectl plugin",378 },379 }380381 subCmd := &Command{Use: "sub [flags]", Args: NoArgs, Run: emptyRun}382 rootCmd.AddCommand(subCmd)383384 rootHelp, err := executeCommand(rootCmd, "-h")385 if err != nil {386 t.Errorf("Unexpected error: %v", err)387 }388389 checkStringContains(t, rootHelp, "kubectl plugin [command]")390391 childHelp, err := executeCommand(rootCmd, "sub", "-h")392 if err != nil {393 t.Errorf("Unexpected error: %v", err)394 }395396 checkStringContains(t, childHelp, "kubectl plugin sub [flags]")397}398399// TestChildSameName checks the correct behaviour of cobra in cases,400// when an application with name "foo" and with subcommand "foo"401// is executed with args "foo foo".402func TestChildSameName(t *testing.T) {
2386 unmodified lines
modified:command.go+8 added-2 removed
28 unmodified lines
2930 flag "github.com/spf13/pflag"31)3233const (33const FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"34 FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"35 CommandDisplayNameAnnotation = "cobra_annotation_command_display_name"36)3738// FParseErrWhitelist configures Flag parse errors to be ignored39type FParseErrWhitelist flag.ParseErrorsWhitelist40
60 unmodified lines
101 // Deprecated defines, if this command is deprecated and should print this string when used.102 Deprecated string103104 // Annotations are key/value pairs that can be used by applications to identify or102 // group commands.105 // group commands or set special options.106 Annotations map[string]string107108 // Version defines the version for this command. If this value is non-empty and the command does not109 // define a "version" flag, a "version" boolean flag will be added to the command and, if specified,
1316 unmodified lines
1426func (c *Command) CommandPath() string {1427 if c.HasParent() {1428 return c.Parent().CommandPath() + " " + c.Name()1429 }1430 if displayName, ok := c.Annotations[CommandDisplayNameAnnotation]; ok {1431 return displayName1432 }1433 return c.Name()1434}14351436// UseLine puts out the full usage for a given command (including parents).
449 unmodified lines