Commit ba7c533f authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #42103 from superbrothers/kubectl-context-completion

Automatic merge from submit-queue (batch tested with PRs 38676, 41765, 42103, 41833, 41702) Support --context flag completion for kubectl **What this PR does / why we need it**: With this PR, `--context` flag completion is supported for kubectl. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note ```
parents bd3ac1c2 4da41ca5
...@@ -76,6 +76,15 @@ __kubectl_get_namespaces() ...@@ -76,6 +76,15 @@ __kubectl_get_namespaces()
fi fi
} }
__kubectl_get_contexts()
{
local template kubectl_out
template="{{ range .contexts }}{{ .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
}
__kubectl_parse_get() __kubectl_parse_get()
{ {
local template local template
...@@ -326,6 +335,16 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob ...@@ -326,6 +335,16 @@ func NewKubectlCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob
) )
} }
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)) cmds.AddCommand(cmdconfig.NewCmdConfig(clientcmd.NewDefaultPathOptions(), out, err))
cmds.AddCommand(NewCmdVersion(f, out)) cmds.AddCommand(NewCmdVersion(f, out))
cmds.AddCommand(NewCmdApiVersions(f, out)) cmds.AddCommand(NewCmdApiVersions(f, out))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment