-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
completion.go
45 lines (38 loc) · 1.05 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package commands
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func newCompletionCmd() (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: completionLong,
Args: cobra.ExactValidArgs(1),
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
DisableFlagsInUseLine: true,
Run: cmdCompletionRun,
}
return cmd
}
func cmdCompletionRun(cmd *cobra.Command, args []string) {
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
err = cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
fmt.Printf("Invalid shell provided for completion command: %s\n", args[0])
os.Exit(1)
}
if err != nil {
fmt.Printf("Error generating completion: %v\n", err)
os.Exit(1)
}
}