Add zsh completion command.

Fixes #862
This commit is contained in:
Trevor Pounds 2019-12-30 01:36:09 -05:00
parent 60c55133a6
commit c1673d2277

View File

@ -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
}