From 2b1078e61e7235399a57706f68c294b9d1cb7a68 Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Sun, 9 Apr 2017 15:11:21 +0900 Subject: [PATCH] Support completion for --cluster and --user With this commit, kubectl is supported completion for `--cluster` and `--user`. ``` $ kubectl --user= --user=minikube --user=user01 $ kubectl --cluster= --cluster=cluster01 --cluster=minikube ``` --- pkg/kubectl/cmd/cmd.go | 57 +++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 0deb6d639af..5f73b765114 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -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))