Skip to content

Commit fd277e0

Browse files
authored
Merge pull request #1892 from longxiucai/add-completion
add completion script. Resolves #1868
2 parents d5a890c + ae60459 commit fd277e0

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

cmd/sops/completion.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// https://github.com/urfave/cli/blob/v1-maint/autocomplete/zsh_autocomplete
6+
var Zshcompletion = `
7+
#compdef %s
8+
9+
_cli_zsh_autocomplete() {
10+
11+
local -a opts
12+
local cur
13+
cur=${words[-1]}
14+
if [[ "$cur" == "-"* ]]; then
15+
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
16+
else
17+
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
18+
fi
19+
20+
if [[ "${opts[1]}" != "" ]]; then
21+
_describe 'values' opts
22+
else
23+
_files
24+
fi
25+
26+
return
27+
}
28+
29+
compdef _cli_zsh_autocomplete %s
30+
`
31+
32+
// https://github.com/urfave/cli/blob/v1-maint/autocomplete/bash_autocomplete
33+
var Bashcompletion = `
34+
#! /bin/bash
35+
36+
_cli_bash_autocomplete() {
37+
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
38+
local cur opts base
39+
COMPREPLY=()
40+
cur="${COMP_WORDS[COMP_CWORD]}"
41+
if [[ "$cur" == "-"* ]]; then
42+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
43+
else
44+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
45+
fi
46+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
47+
return 0
48+
fi
49+
}
50+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete %s
51+
`
52+
53+
func GenBashCompletion(name string) string {
54+
return fmt.Sprintf(Bashcompletion, name)
55+
}
56+
57+
func GenZshCompletion(name string) string {
58+
return fmt.Sprintf(Zshcompletion, name, name)
59+
}

cmd/sops/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ func main() {
151151
For more information, see the README at https://github.com/getsops/sops`
152152
app.EnableBashCompletion = true
153153
app.Commands = []cli.Command{
154+
{
155+
Name: "completion",
156+
Usage: "Generate shell completion scripts",
157+
Subcommands: []cli.Command{
158+
{
159+
Name: "bash",
160+
Usage: fmt.Sprintf("Generate bash completions. To load completions: `$ source <(%s completion bash)`", app.Name),
161+
Action: func(c *cli.Context) error {
162+
fmt.Fprint(c.App.Writer, GenBashCompletion(app.Name))
163+
return nil
164+
},
165+
},
166+
{
167+
Name: "zsh",
168+
Usage: fmt.Sprintf("Generate zsh completions. To load completions: `$ source <(%s completion zsh)`", app.Name),
169+
Action: func(c *cli.Context) error {
170+
fmt.Fprint(c.App.Writer, GenZshCompletion(app.Name))
171+
return nil
172+
},
173+
}},
174+
},
154175
{
155176
Name: "exec-env",
156177
Usage: "execute a command with decrypted values inserted into the environment",

0 commit comments

Comments
 (0)