From c1673d22774ce2691f99182264a043b20f415e20 Mon Sep 17 00:00:00 2001 From: Trevor Pounds <trevor.pounds@gmail.com> Date: Mon, 30 Dec 2019 01:36:09 -0500 Subject: [PATCH] Add zsh completion command. Fixes #862 --- pkg/commands/completion.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/commands/completion.go b/pkg/commands/completion.go index 1ff4b44f..7d919d19 100644 --- a/pkg/commands/completion.go +++ b/pkg/commands/completion.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "os" "github.com/pkg/errors" @@ -17,12 +18,19 @@ func (e *Executor) initCompletion() { bashCmd := &cobra.Command{ Use: "bash", Short: "Output bash completion script", - RunE: e.executeCompletion, + RunE: e.executeBashCompletion, } completionCmd.AddCommand(bashCmd) + + zshCmd := &cobra.Command{ + Use: "zsh", + Short: "Output zsh completion script", + RunE: e.executeZshCompletion, + } + completionCmd.AddCommand(zshCmd) } -func (e *Executor) executeCompletion(cmd *cobra.Command, args []string) error { +func (e *Executor) executeBashCompletion(cmd *cobra.Command, args []string) error { err := cmd.Root().GenBashCompletion(os.Stdout) if err != nil { return errors.Wrap(err, "unable to generate bash completions: %v") @@ -30,3 +38,16 @@ func (e *Executor) executeCompletion(cmd *cobra.Command, args []string) error { return nil } + +func (e *Executor) executeZshCompletion(cmd *cobra.Command, args []string) error { + err := cmd.Root().GenZshCompletion(os.Stdout) + if err != nil { + return errors.Wrap(err, "unable to generate zsh completions: %v") + } + // Add extra compdef directive to support sourcing command directly. + // https://github.com/spf13/cobra/issues/881 + // https://github.com/spf13/cobra/pull/887 + fmt.Println("compdef _golangci-lint golangci-lint") + + return nil +}