-
Notifications
You must be signed in to change notification settings - Fork 21
/
completion.go
136 lines (106 loc) · 4.42 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2022 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package command
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/lithammer/dedent"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/tanzu-cli/pkg/cli"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
)
func init() {
completionCmd.SetUsageFunc(cli.SubCmdUsageFunc)
}
const compNoMoreArgsMsg = "This command does not take any more arguments (but may accept flags)."
var (
completionShells = []string{
"bash",
"zsh",
"fish",
"powershell",
}
completionLongDesc = `Output shell completion code for the specified shell (%v).
The shell completion code must be evaluated to provide completion. See Examples
for how to perform this for your given shell.
Note for bash users: make sure the bash-completions package has been installed.`
completionExamples = dedent.Dedent(`
# Bash instructions:
## Load only for current session:
source <(tanzu completion bash)
## Load for all new sessions:
tanzu completion bash > $HOME/.config/tanzu/completion.bash.inc
printf "\n# Tanzu shell completion\nsource '$HOME/.config/tanzu/completion.bash.inc'\n" >> $HOME/.bashrc
## NOTE: the bash-completion OS package must also be installed.
## If you invoke the 'tanzu' command using a different name or an alias such as,
## for example, 'tz', you must also include the following in your $HOME/.bashrc
complete -o default -F __start_tanzu tz
# Zsh instructions:
## Load only for current session:
autoload -U compinit; compinit
source <(tanzu completion zsh)
## Load for all new sessions:
echo "autoload -U compinit; compinit" >> $HOME/.zshrc
tanzu completion zsh > "${fpath[1]}/_tanzu"
## Aliases are handled automatically, but if you have renamed the actual 'tanzu' binary to,
## for example, 'tz', you must also include the following in your $HOME/.zshrc
compdef _tanzu tz
# Fish instructions:
## Load only for current session:
tanzu completion fish | source
## Load for all new sessions:
tanzu completion fish > $HOME/.config/fish/completions/tanzu.fish
## Aliases are handled automatically, but if you have renamed the actual 'tanzu' binary to,
## for example, 'tz', you must also include the following in your $HOME/.config/fish/config.fish
complete --command tz --wraps tanzu
# Powershell instructions:
## Load only for current session:
tanzu completion powershell | Out-String | Invoke-Expression
## Load for all new sessions:
printf "\n# Tanzu shell completion\ntanzu completion powershell | Out-String | Invoke-Expression" >> $PROFILE
## If you invoke the 'tanzu' command using a different name or an alias such as,
## for example, 'tz', you must also include the following in your powershell $PROFILE.
Register-ArgumentCompleter -CommandName 'tz' -ScriptBlock ${__tanzuCompleterBlock}`)
)
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: fmt.Sprintf("completion [%v]", strings.Join(completionShells, "|")),
Short: "Output shell completion code",
Long: fmt.Sprintf(completionLongDesc, strings.Join(completionShells, ", ")),
Example: completionExamples,
DisableFlagsInUseLine: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completionShells, cobra.ShellCompDirectiveNoFileComp
}
return activeHelpNoMoreArgs(nil), cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
return runCompletion(os.Stdout, cmd, args)
},
Annotations: map[string]string{
"group": string(plugin.SystemCmdGroup),
},
}
func runCompletion(out io.Writer, cmd *cobra.Command, args []string) error {
if length := len(args); length == 0 {
return fmt.Errorf("shell not specified, choose one of: %v", strings.Join(completionShells, ", "))
} else if length > 1 {
return errors.New("too many arguments, expected only the shell type")
}
switch strings.ToLower(args[0]) {
case "bash":
return cmd.Root().GenBashCompletionV2(out, true)
case "zsh":
return cmd.Root().GenZshCompletion(out)
case "fish":
return cmd.Root().GenFishCompletion(out, true)
case "powershell", "pwsh":
return cmd.Root().GenPowerShellCompletionWithDesc(out)
default:
return errors.New("unrecognized shell type specified")
}
}