11package commands
22
33import (
4+ "fmt"
45 "os"
56
67 "github.com/pkg/errors"
@@ -17,16 +18,36 @@ func (e *Executor) initCompletion() {
1718 bashCmd := & cobra.Command {
1819 Use : "bash" ,
1920 Short : "Output bash completion script" ,
20- RunE : e .executeCompletion ,
21+ RunE : e .executeBashCompletion ,
2122 }
2223 completionCmd .AddCommand (bashCmd )
24+
25+ zshCmd := & cobra.Command {
26+ Use : "zsh" ,
27+ Short : "Output zsh completion script" ,
28+ RunE : e .executeZshCompletion ,
29+ }
30+ completionCmd .AddCommand (zshCmd )
2331}
2432
25- func (e * Executor ) executeCompletion (cmd * cobra.Command , args []string ) error {
33+ func (e * Executor ) executeBashCompletion (cmd * cobra.Command , args []string ) error {
2634 err := cmd .Root ().GenBashCompletion (os .Stdout )
2735 if err != nil {
2836 return errors .Wrap (err , "unable to generate bash completions: %v" )
2937 }
3038
3139 return nil
3240}
41+
42+ func (e * Executor ) executeZshCompletion (cmd * cobra.Command , args []string ) error {
43+ err := cmd .Root ().GenZshCompletion (os .Stdout )
44+ if err != nil {
45+ return errors .Wrap (err , "unable to generate zsh completions: %v" )
46+ }
47+ // Add extra compdef directive to support sourcing command directly.
48+ // https://github.com/spf13/cobra/issues/881
49+ // https://github.com/spf13/cobra/pull/887
50+ fmt .Println ("compdef _golangci-lint golangci-lint" )
51+
52+ return nil
53+ }
0 commit comments