mirror of https://github.com/k3s-io/k3s
Support completion for --cluster and --user
With this commit, kubectl is supported completion for `--cluster` and `--user`. ``` $ kubectl --user=<tab> --user=minikube --user=user01 $ kubectl --cluster=<tab> --cluster=cluster01 --cluster=minikube ```pull/6/head
parent
41e9b80e5f
commit
2b1078e61e
|
@ -76,10 +76,26 @@ __kubectl_get_namespaces()
|
|||
fi
|
||||
}
|
||||
|
||||
__kubectl_get_contexts()
|
||||
__kubectl_config_get_contexts()
|
||||
{
|
||||
__kubectl_parse_config "contexts"
|
||||
}
|
||||
|
||||
__kubectl_config_get_clusters()
|
||||
{
|
||||
__kubectl_parse_config "clusters"
|
||||
}
|
||||
|
||||
__kubectl_config_get_users()
|
||||
{
|
||||
__kubectl_parse_config "users"
|
||||
}
|
||||
|
||||
# $1 has to be "contexts", "clusters" or "users"
|
||||
__kubectl_config_get()
|
||||
{
|
||||
local template kubectl_out
|
||||
template="{{ range .contexts }}{{ .name }} {{ end }}"
|
||||
template="{{ range .$1 }}{{ .name }} {{ end }}"
|
||||
if kubectl_out=$(kubectl config $(__kubectl_override_flags) -o template --template="${template}" view 2>/dev/null); then
|
||||
COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) )
|
||||
fi
|
||||
|
@ -171,7 +187,7 @@ __custom_func() {
|
|||
return
|
||||
;;
|
||||
kubectl_config_use-context)
|
||||
__kubectl_get_contexts
|
||||
__kubectl_config_get_contexts
|
||||
return
|
||||
;;
|
||||
*)
|
||||
|
@ -224,6 +240,15 @@ __custom_func() {
|
|||
`
|
||||
)
|
||||
|
||||
var (
|
||||
bash_completion_flags = map[string]string{
|
||||
"namespace": "__kubectl_get_namespaces",
|
||||
"context": "__kubectl_config_get_contexts",
|
||||
"cluster": "__kubectl_config_get_clusters",
|
||||
"user": "__kubectl_config_get_users",
|
||||
}
|
||||
)
|
||||
|
||||
// NewKubectlCommand creates the `kubectl` command and its nested children.
|
||||
func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Command {
|
||||
// Parent command to which all subcommands are added.
|
||||
|
@ -330,24 +355,16 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
|
|||
}
|
||||
templates.ActsAsRootCommand(cmds, filters, groups...)
|
||||
|
||||
if cmds.Flag("namespace") != nil {
|
||||
if cmds.Flag("namespace").Annotations == nil {
|
||||
cmds.Flag("namespace").Annotations = map[string][]string{}
|
||||
for name, completion := range bash_completion_flags {
|
||||
if cmds.Flag(name) != nil {
|
||||
if cmds.Flag(name).Annotations == nil {
|
||||
cmds.Flag(name).Annotations = map[string][]string{}
|
||||
}
|
||||
cmds.Flag(name).Annotations[cobra.BashCompCustom] = append(
|
||||
cmds.Flag(name).Annotations[cobra.BashCompCustom],
|
||||
completion,
|
||||
)
|
||||
}
|
||||
cmds.Flag("namespace").Annotations[cobra.BashCompCustom] = append(
|
||||
cmds.Flag("namespace").Annotations[cobra.BashCompCustom],
|
||||
"__kubectl_get_namespaces",
|
||||
)
|
||||
}
|
||||
|
||||
if cmds.Flag("context") != nil {
|
||||
if cmds.Flag("context").Annotations == nil {
|
||||
cmds.Flag("context").Annotations = map[string][]string{}
|
||||
}
|
||||
cmds.Flag("context").Annotations[cobra.BashCompCustom] = append(
|
||||
cmds.Flag("context").Annotations[cobra.BashCompCustom],
|
||||
"__kubectl_get_contexts",
|
||||
)
|
||||
}
|
||||
|
||||
cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), out, err))
|
||||
|
|
Loading…
Reference in New Issue