-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmd_completion.go
87 lines (66 loc) · 1.99 KB
/
cmd_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
76
77
78
79
80
81
82
83
84
85
86
87
package cli
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/telepresenceio/telepresence/v2/pkg/client/errcat"
)
func addCompletionCommand(rootCmd *cobra.Command) {
cmd := cobra.Command{
Use: "completion",
Short: "Generate a shell completion script",
ValidArgs: []string{
"bash",
"zsh",
"powershell",
"fish",
},
ArgAliases: []string{"ps"},
RunE: func(cmd *cobra.Command, args []string) error {
var shell string
if 0 < len(args) {
shell = args[0]
}
var err error
switch shell {
case "zsh":
err = rootCmd.GenZshCompletionNoDesc(os.Stdout)
case "bash":
err = rootCmd.GenBashCompletionV2(os.Stdout, false)
case "fish":
err = rootCmd.GenFishCompletion(os.Stdout, false)
case "ps", "powershell":
err = rootCmd.GenPowerShellCompletion(os.Stdout)
case "":
err = errcat.User.Newf("shell not specified")
}
return err
},
Long: fmt.Sprintf(`To load completions:
Bash:
$ source <(%[1]s completion bash)
# To load completions for each session, execute once:
# Linux:
$ %[1]s completion bash > /etc/bash_completion.d/%[1]s
# macOS:
$ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
# You will need to start a new shell for this setup to take effect.
fish:
$ %[1]s completion fish | source
# To load completions for each session, execute once:
$ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
PowerShell:
PS> %[1]s completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> %[1]s completion powershell > %[1]s.ps1
# and source this file from your PowerShell profile.
`, rootCmd.Name()),
}
rootCmd.AddCommand(&cmd)
}