-
Notifications
You must be signed in to change notification settings - Fork 7
/
completion.go
81 lines (59 loc) · 2.25 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
package command
import (
"fmt"
"os"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
"github.com/spf13/cobra"
)
func NewCompletion(rootCmd *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "completion <shell>",
Short: `Output shell completion code`,
Long: `Output shell completion code for the specified shell (bash or zsh).
The shell code must be evaluated to provide interactive
completion of hamctl commands. This can be done by sourcing it from
the .bash_profile.
Note for zsh users: zsh completions are only supported in versions of zsh >= 5.2
Installing bash completion on macOS using homebrew
If running Bash 3.2 included with macOS
brew install bash-completion
If running Bash 4.1+
brew install bash-completion@2
You may need add the completion to your completion directory
hamctl completion bash > $(brew --prefix)/etc/bash_completion.d/hamctl
Installing bash completion on Linux
If bash-completion is not installed on Linux, please install the 'bash-completion' package
via your distribution's package manager.
Load the hamctl completion code for bash into the current shell
source <(hamctl completion bash)
Write bash completion code to a file and source if from .bash_profile
hamctl completion bash > ~/.hamctl/completion.bash.inc
printf "
# hamctl shell completion
source '$HOME/.hamctl/completion.bash.inc'
" >> $HOME/.bash_profile
source $HOME/.bash_profile
Load the hamctl completion code for zsh[1] into the current shell
source <(hamctl completion zsh)
Set the hamctl completion code for zsh[1] to autoload on startup
hamctl completion zsh > "${fpath[1]}/_hamctl"`,
ValidArgs: []string{"bash", "zsh"},
Args: func(cmd *cobra.Command, args []string) error {
if cobra.ExactArgs(1)(cmd, args) != nil || cobra.OnlyValidArgs(cmd, args) != nil {
return fmt.Errorf("only %v arguments are allowed", cmd.ValidArgs)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "zsh":
return completion.Zsh(os.Stdout, rootCmd)
case "bash":
return rootCmd.GenBashCompletion(os.Stdout)
default:
return nil
}
},
}
return cmd
}