From 51671ecc2e258d50c7b1315b7b68c4e239f7e829 Mon Sep 17 00:00:00 2001 From: Philipp Stehle Date: Fri, 22 Jan 2021 10:04:56 +0100 Subject: [PATCH] bash-completion for comma separated list on get `kubectl get` supports specifying a comma separated list of resource types. E.g.: ```bash kubectl get pods,secrets ``` Will list all pods and secrets in the current namespace. This commit adds support to the kubectl bash completion for this feature. Which means if you type `kubectl get pods,sec` it will be auto-completed to `kubectl get pods,secrets` (assuming the cluster does not have a CRD starting with `sec`). --- staging/src/k8s.io/kubectl/pkg/cmd/cmd.go | 48 +++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go b/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go index ba1ac37385c..94bf8239692 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go @@ -152,19 +152,49 @@ __kubectl_parse_get() fi } +# Same as __kubectl_get_resources (with s) but allows completion for only one resource name. __kubectl_get_resource() { if [[ ${#nouns[@]} -eq 0 ]]; then - local kubectl_out - if kubectl_out=$(__kubectl_debug_out "kubectl api-resources $(__kubectl_override_flags) -o name --cached --request-timeout=5s --verbs=get"); then - COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) ) - return 0 - fi - return 1 + __kubectl_get_resource_helper "" "$cur" + return # the return status is that of the last command executed in the function body fi __kubectl_parse_get "${nouns[${#nouns[@]} -1]}" } +# Same as __kubectl_get_resource (without s) but allows completion for multiple, comma-separated resource names. +__kubectl_get_resources() +{ + local SEPARATOR=',' + if [[ ${#nouns[@]} -eq 0 ]]; then + local kubectl_out HEAD TAIL + HEAD="" + TAIL="$cur" + # if SEPARATOR is contained in $cur, e.g. "pod,sec" + if [[ "$cur" = *${SEPARATOR}* ]] ; then + # set HEAD to "pod," + HEAD="${cur%${SEPARATOR}*}${SEPARATOR}" + # set TAIL to "sec" + TAIL="${cur##*${SEPARATOR}}" + fi + __kubectl_get_resource_helper "$HEAD" "$TAIL" + return # the return status is that of the last command executed in the function body + fi + __kubectl_parse_get "${nouns[${#nouns[@]} -1]}" +} + +__kubectl_get_resource_helper() +{ + local kubectl_out HEAD TAIL + HEAD="$1" + TAIL="$2" + if kubectl_out=$(__kubectl_debug_out "kubectl api-resources $(__kubectl_override_flags) -o name --cached --request-timeout=5s --verbs=get"); then + COMPREPLY=( $( compgen -P "$HEAD" -W "${kubectl_out[*]}" -- "$TAIL" ) ) + return 0 + fi + return 1 +} + __kubectl_get_resource_namespace() { __kubectl_parse_get "namespace" @@ -251,7 +281,11 @@ __kubectl_cp() __kubectl_custom_func() { case ${last_command} in - kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_edit | kubectl_patch |\ + kubectl_get) + __kubectl_get_resources + return + ;; + kubectl_describe | kubectl_delete | kubectl_label | kubectl_edit | kubectl_patch |\ kubectl_annotate | kubectl_expose | kubectl_scale | kubectl_autoscale | kubectl_taint | kubectl_rollout_* |\ kubectl_apply_edit-last-applied | kubectl_apply_view-last-applied) __kubectl_get_resource