This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.go
75 lines (66 loc) · 1.58 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cmd
import (
"errors"
"os"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(completionCmd)
}
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run
. <(` + applName + ` completion bash)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(` + applName + ` completion bash)
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
var shell string
writer := os.Stdout
if len(args) == 2 {
shell = args[0]
writer, err = os.Create(args[1])
if err != nil {
return
}
} else if len(args) == 1 {
shell = args[0]
} else if len(args) == 0 {
shell = getShell()
} else {
return errInvalidArguments
}
defer func() {
err = writer.Close()
}()
switch shell {
case "bash":
return rootCmd.GenBashCompletion(writer)
case "zsh":
return rootCmd.GenZshCompletion(writer)
case "fish":
return rootCmd.GenFishCompletion(writer, true)
case "powershell":
return rootCmd.GenPowerShellCompletion(writer)
default:
return errors.New("completion for shell " + shell + " is not supported yet")
}
},
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
}
func getShell() (shell string) {
if os.Getenv("ZSH_VERSION") != "" {
shell = "zsh"
} else if os.Getenv("BASH_VERSION") != "" {
shell = "bash"
} else if os.Getenv("FISH_VERSION") != "" {
shell = "fish"
} else if os.Getenv("PSVersionTable.PSVersion") != "" {
shell = "powershell"
} else {
shell = "unknown"
}
return
}