forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completionCmd.go
212 lines (181 loc) · 7.02 KB
/
completionCmd.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cobra
import (
"fmt"
)
const (
// Constants for the completion command
compCmdName = "completion"
compCmdNoDescFlagName = "no-descriptions"
compCmdNoDescFlagDesc = "disable completion descriptions"
compCmdNoDescFlagDefault = false
shortDesc = "Generate the autocompletion script for %s"
)
// CompletionOptions are the options to control shell completion
type CompletionOptions struct {
// DisableDefaultCmd prevents Cobra from creating a default 'completion' command
DisableDefaultCmd bool
// DisableNoDescFlag prevents Cobra from creating the '--no-descriptions' flag
// for shells that support completion descriptions
DisableNoDescFlag bool
// DisableDescriptions turns off all completion descriptions for shells
// that support them
DisableDescriptions bool
}
var (
CompletionCmd = &Command{
Use: compCmdName,
Short: "Generate the autocompletion script for the specified shell",
Args: NoArgs,
ValidArgsFunction: NoFileCompletions,
}
BashCompletionCmd = &Command{
Use: "bash",
Short: fmt.Sprintf(shortDesc, "bash"),
Args: NoArgs,
DisableFlagsInUseLine: true,
ValidArgsFunction: NoFileCompletions,
}
ZshCompletionCmd = &Command{
Use: "zsh",
Short: fmt.Sprintf(shortDesc, "zsh"),
Args: NoArgs,
DisableFlagsInUseLine: true,
ValidArgsFunction: NoFileCompletions,
}
FishCompletionCmd = &Command{
Use: "fish",
Short: fmt.Sprintf(shortDesc, "fish"),
Args: NoArgs,
DisableFlagsInUseLine: true,
ValidArgsFunction: NoFileCompletions,
}
PwshCompletionCmd = &Command{
Use: "powershell",
Short: fmt.Sprintf(shortDesc, "powershell"),
Args: NoArgs,
DisableFlagsInUseLine: true,
ValidArgsFunction: NoFileCompletions,
}
)
// initDefaultCompletionCmd adds a default 'completion' command to c.
// This function will do nothing if any of the following is true:
// 1- the feature has been explicitly disabled by the program,
// 2- c has no subcommands (to avoid creating one),
// 3- c already has a 'completion' command provided by the program.
func (c *Command) initDefaultCompletionCmd() {
if c.CompletionOptions.DisableDefaultCmd || !c.HasSubCommands() {
return
}
for _, cmd := range c.commands {
if cmd.Name() == compCmdName || cmd.HasAlias(compCmdName) {
// A completion command is already available
return
}
}
if CompletionCmd.Long == "" {
CompletionCmd.Long = fmt.Sprintf(
`Generate the autocompletion script for %[1]s for the specified shell.
See each sub-command's help for details on how to use the generated script.`, c.Root().Name())
}
c.RemoveCommand(CompletionCmd) // Tests can call this function multiple times in a row, so we must reset
c.AddCommand(CompletionCmd)
out := c.OutOrStdout()
noDesc := c.CompletionOptions.DisableDescriptions
haveNoDescFlag := !c.CompletionOptions.DisableNoDescFlag && !c.CompletionOptions.DisableDescriptions
bash := BashCompletionCmd
if bash.Long == "" {
bash.Long = fmt.Sprintf(
`Generate the autocompletion script for the bash shell.
This script depends on the 'bash-completion' package.
If it is not installed already, you can install it via your OS's package manager.
To load completions in your current shell session:
$ source <(%[1]s %[2]s %[3]s)
To load completions for every new session, execute once:
Linux:
$ %[1]s %[2]s %[3]s > /etc/bash_completion.d/%[1]s
MacOS:
$ %[1]s %[2]s %[3]s > /usr/local/etc/bash_completion.d/%[1]s
You will need to start a new shell for this setup to take effect.`,
c.Root().Name(), CompletionCmd.Name(), BashCompletionCmd.Name())
}
if bash.RunE == nil && bash.Run == nil {
bash.RunE = func(cmd *Command, args []string) error {
return cmd.Root().GenBashCompletionV2(out, !noDesc)
}
}
bash.ResetFlags() // Tests can call this function multiple times in a row, so we must reset
if haveNoDescFlag {
bash.Flags().BoolVar(&noDesc, compCmdNoDescFlagName, compCmdNoDescFlagDefault, compCmdNoDescFlagDesc)
}
zsh := ZshCompletionCmd
if zsh.Long == "" {
zsh.Long = fmt.Sprintf(
`Generate the autocompletion script for the zsh shell.
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 every new session, execute once:
# Linux:
$ %[1]s %[2]s %[3]s > "${fpath[1]}/_%[1]s"
# macOS:
$ %[1]s %[2]s %[3]s > /usr/local/share/zsh/site-functions/_%[1]s
You will need to start a new shell for this setup to take effect.`,
c.Root().Name(), CompletionCmd.Name(), ZshCompletionCmd.Name())
}
if zsh.RunE == nil && zsh.Run == nil {
zsh.RunE = func(cmd *Command, args []string) error {
if noDesc {
return cmd.Root().GenZshCompletionNoDesc(out)
}
return cmd.Root().GenZshCompletion(out)
}
}
zsh.ResetFlags() // Tests can call this function multiple times in a row, so we must reset
if haveNoDescFlag {
zsh.Flags().BoolVar(&noDesc, compCmdNoDescFlagName, compCmdNoDescFlagDefault, compCmdNoDescFlagDesc)
}
fish := FishCompletionCmd
if fish.Long == "" {
fish.Long = fmt.Sprintf(
`Generate the autocompletion script for the fish shell.
To load completions in your current shell session:
$ %[1]s %[2]s %[3]s | source
To load completions for every new session, execute once:
$ %[1]s %[2]s %[3]s > ~/.config/fish/completions/%[1]s.fish
You will need to start a new shell for this setup to take effect.`,
c.Root().Name(), CompletionCmd.Name(), FishCompletionCmd.Name())
}
if fish.RunE == nil && fish.Run == nil {
fish.RunE = func(cmd *Command, args []string) error {
return cmd.Root().GenFishCompletion(out, !noDesc)
}
}
fish.ResetFlags() // Tests can call this function multiple times in a row, so we must reset
if haveNoDescFlag {
fish.Flags().BoolVar(&noDesc, compCmdNoDescFlagName, compCmdNoDescFlagDefault, compCmdNoDescFlagDesc)
}
pwsh := PwshCompletionCmd
if pwsh.Long == "" {
pwsh.Long = fmt.Sprintf(
`Generate the autocompletion script for powershell.
To load completions in your current shell session:
PS C:\> %[1]s %[2]s %[3]s | Out-String | Invoke-Expression
To load completions for every new session, add the output of the above command
to your powershell profile.`,
c.Root().Name(), CompletionCmd.Name(), PwshCompletionCmd.Name())
}
if pwsh.RunE == nil && pwsh.Run == nil {
pwsh.RunE = func(cmd *Command, args []string) error {
if noDesc {
return cmd.Root().GenPowerShellCompletion(out)
}
return cmd.Root().GenPowerShellCompletionWithDesc(out)
}
}
pwsh.ResetFlags() // Tests can call this function multiple times in a row, so we must reset
if haveNoDescFlag {
pwsh.Flags().BoolVar(&noDesc, compCmdNoDescFlagName, compCmdNoDescFlagDefault, compCmdNoDescFlagDesc)
}
CompletionCmd.RemoveCommand(bash, zsh, fish, pwsh) // Tests can call this function multiple times in a row, so we must reset
CompletionCmd.AddCommand(bash, zsh, fish, pwsh)
}