Skip to content

Commit c1673d2

Browse files
committed
Add zsh completion command.
Fixes #862
1 parent 60c5513 commit c1673d2

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/commands/completion.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
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

Comments
 (0)