Skip to content

Commit e52807e

Browse files
committed
add completion script. Resolves #1868
Signed-off-by: longyuxiang <longyuxiang@kylinos.cn>
1 parent ae1e89f commit e52807e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

cmd/sops/completion.go

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

cmd/sops/main.go

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

0 commit comments

Comments
 (0)