Skip to content
Myr

Document how to create a plugin

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

Using the new CommandDisplayNameAnnotation annotation introduced in Cobra 1.8.0.

authored by Nir Soffer · committed by Marc Khouzam · parent a73b9c3 · Browse files · 1 file+54 added−0 removed
This commit

State

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

Diff stats

1 file+54 added−0 removed

  • other:+54 added−0 removed

1 file+54 added−0 removed

modified:site/content/user_guide.md+54 added
746 unmodified lines
747748Cobra makes use of the shell-completion system to define a framework allowing you to provide Active Help to your users.749Active Help are messages (hints, warnings, etc) printed as the program is being used.750Read more about it in [Active Help](active_help.md).751752## Creating a plugin753754When creating a plugin for tools like *kubectl*, the executable is named755`kubectl-myplugin`, but it is used as `kubectl myplugin`. To fix help756messages and completions, annotate the root command with the757`cobra.CommandDisplayNameAnnotation` annotation.758759### Example kubectl plugin760761```go762package main763764import (765 "fmt"766767 "github.com/spf13/cobra"768)769770func main() {771 rootCmd := &cobra.Command{772 Use: "kubectl-myplugin",773 Annotations: map[string]string{774 cobra.CommandDisplayNameAnnotation: "kubectl myplugin",775 },776 }777 subCmd := &cobra.Command{778 Use: "subcmd",779 Run: func(cmd *cobra.Command, args []string) {780 fmt.Println("kubectl myplugin subcmd")781 },782 }783 rootCmd.AddCommand(subCmd)784 rootCmd.Execute()785}786```787788Example run as a kubectl plugin:789790```791$ kubectl myplugin792Usage:793 kubectl myplugin [command]794795Available Commands:796 completion Generate the autocompletion script for the specified shell797 help Help about any command798 subcmd799800Flags:801 -h, --help help for kubectl myplugin802803Use "kubectl myplugin [command] --help" for more information about a command.804```