-
Notifications
You must be signed in to change notification settings - Fork 242
/
completion.go
117 lines (94 loc) 路 2.19 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
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
package completion
import (
"fmt"
"github.com/pkg/errors"
"github.com/smallstep/cli/flags"
"github.com/urfave/cli"
"go.step.sm/cli-utils/command"
"go.step.sm/cli-utils/errs"
)
func init() {
cmd := cli.Command{
Name: "completion",
Usage: "print the shell completion script",
UsageText: "**step completion** <shell>",
Description: `**step completion** command prints the shell completion script.
## POSITIONAL ARGUMENTS
<shell>
: The shell program. Supports bash, zsh, and fish.
## EXAMPLES
Add bash completion for the current user.
'''
$ step completion bash >> ~/.bash_completion
'''
Add fish completions for the current user.
'''
$ step completion fish | source
'''
`,
Flags: []cli.Flag{
flags.HiddenNoContext,
},
Action: Completion,
BashComplete: func(c *cli.Context) {
if c.NArg() > 0 {
return
}
fmt.Println("bash")
fmt.Println("zsh")
fmt.Println("fish")
},
}
command.Register(cmd)
}
var bash = `# bash completion for step
_step_cli_bash_autocomplete() {
local cur opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "$cur" == "-"* ]]; then
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
else
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
fi
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -o bashdefault -o default -F _step_cli_bash_autocomplete step
`
var zsh = `#compdef step
_step() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
return
}
compdef _step step
`
func Completion(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 1); err != nil {
return err
}
shell := ctx.Args().First()
switch shell {
case "bash":
fmt.Print(bash)
case "zsh":
fmt.Print(zsh)
case "fish":
fmt.Print(ctx.App.ToFishCompletion())
default:
return errors.Errorf("unsupported shell %s", shell)
}
return nil
}