forked from evolbioinfo/gotree
/
autocomplete.go
64 lines (55 loc) · 1.88 KB
/
autocomplete.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
package cmd
import (
"os"
"github.com/spf13/cobra"
)
// itolCmd represents the itol command
var autocompleteCmd = &cobra.Command{
Use: "completion SHELL",
Args: cobra.ExactArgs(1),
Short: "Generates auto-completion commands for bash or zsh",
Long: `Generates auto-completion commands for bash or zsh.
Examples (Largely inspired from kubectl command):
# bash completion on macOS using homebrew
## If running Bash 3.2 included with macOS
brew install bash-completion
## or, if running Bash 4.1+
brew install bash-completion@2
# Then add auto completion commands
gotree completion bash > $(brew --prefix)/etc/bash_completion.d/gotree
# bash completion on macOS using macports
## If running Bash 3.2 included with macOS
Change path to bash in Terminal preferences/path to shell:
/opt/local/bin/bash -l
## Then in stall bash-completion
sudo port install bash-completion
# Finally add auto completion commands
gotree completion bash > $(brew --prefix)/etc/bash_completion.d/gotree
# bash completion on Linux
## Load the gotree completion code for bash into the current shell
source <(gotree completion bash)
## Write bash completion code to a file and source if from .bash_profile
mkdir ~/.gotree
gotree completion bash > ~/.gotree/completion.bash.inc
printf "
# gotree shell completion
source '$HOME/.gotree/completion.bash.inc'
" >> $HOME/.bashrc
source $HOME/.bashrc
# Load the gotree completion code for zsh[1] into the current shell
source <(gotree completion zsh)
# Set the gotree completion code for zsh[1] to autoload on startup
gotree completion zsh > "${fpath[1]}/_gotree"
`,
Run: func(cmd *cobra.Command, args []string) {
shell := args[0]
if shell == "bash" {
RootCmd.GenBashCompletion(os.Stdout)
} else if shell == "zsh" {
RootCmd.GenZshCompletion(os.Stdout)
}
},
}
func init() {
RootCmd.AddCommand(autocompleteCmd)
}