diff --git a/main.go b/main.go index 64ff3925..cfbbf79d 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/spf13/cobra" "github.com/streamnative/pulsarctl/pkg/cmdutils" "github.com/streamnative/pulsarctl/pkg/ctl/cluster" + "github.com/streamnative/pulsarctl/pkg/ctl/completion" "os" ) @@ -59,6 +60,7 @@ func init() { func addCommands(flagGrouping *cmdutils.FlagGrouping) { rootCmd.AddCommand(cluster.Command(flagGrouping)) + rootCmd.AddCommand(completion.Command(rootCmd)) } func main() { diff --git a/pkg/ctl/completion/completion.go b/pkg/ctl/completion/completion.go new file mode 100644 index 00000000..f6f8dac4 --- /dev/null +++ b/pkg/ctl/completion/completion.go @@ -0,0 +1,62 @@ +package completion + +import ( + "github.com/kris-nova/logger" + "github.com/spf13/cobra" + "os" +) + +func Command(rootCmd *cobra.Command) *cobra.Command { + var bashCompletionCmd = &cobra.Command{ + Use: "bash", + Short: "Generates bash completion scripts", + Long: `To load completion run + +. <(pulsarctl completion bash) + +To configure your bash shell to load completions for each session add to your bashrc + +# ~/.bashrc or ~/.profile +. <(pulsarctl completion bash) + +If you are stuck on Bash 3 (macOS) use + +source /dev/stdin <<<"$(pulsarctl completion bash)" +`, + RunE: func(cmd *cobra.Command, args []string) error { + return rootCmd.GenBashCompletion(os.Stdout) + }, + } + + var zshCompletionCmd = &cobra.Command{ + Use: "zsh", + Short: "Generates zsh completion scripts", + Long: `To configure your zsh shell, run: + +mkdir -p ~/.zsh/completion/ +pulsarctl completion zsh > ~/.zsh/completion/_pulsarctl + +and put the following in ~/.zshrc: + +fpath=($fpath ~/.zsh/completion) +`, + RunE: func(cmd *cobra.Command, args []string) error { + return rootCmd.GenZshCompletion(os.Stdout) + }, + } + + cmd := &cobra.Command{ + Use: "completion", + Short: "Generates shell completion scripts", + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + logger.Debug("ignoring error %q", err.Error()) + } + }, + } + + cmd.AddCommand(bashCompletionCmd) + cmd.AddCommand(zshCompletionCmd) + + return cmd +}