From 4200d6c2cc8d83ea98968235299f7eab76381c94 Mon Sep 17 00:00:00 2001 From: Daisuke Taniwaki Date: Sun, 13 Jan 2019 01:53:47 +0900 Subject: [PATCH] Avoid conflicts with other cobra auto completion --- pkg/kubectl/cmd/cmd.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/kubectl/cmd/cmd.go b/pkg/kubectl/cmd/cmd.go index 217b91ef265b..bf0ed3a69816 100644 --- a/pkg/kubectl/cmd/cmd.go +++ b/pkg/kubectl/cmd/cmd.go @@ -79,6 +79,13 @@ import ( const ( bashCompletionFunc = `# call kubectl get $1, +__kubectl_debug_out() +{ + local cmd="$1" + __kubectl_debug "${FUNCNAME[1]}: get completion by ${cmd}" + eval "${cmd} 2>/dev/null" +} + __kubectl_override_flag_list=(--kubeconfig --cluster --user --context --namespace --server -n -s) __kubectl_override_flags() { @@ -127,7 +134,7 @@ __kubectl_parse_config() { local template kubectl_out template="{{ range .$1 }}{{ .name }} {{ end }}" - if kubectl_out=$(kubectl config $(__kubectl_override_flags) -o template --template="${template}" view 2>/dev/null); then + if kubectl_out=$(__kubectl_debug_out "kubectl config $(__kubectl_override_flags) -o template --template=\"${template}\" view"); then COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) ) fi } @@ -139,7 +146,7 @@ __kubectl_parse_get() local template template="${2:-"{{ range .items }}{{ .metadata.name }} {{ end }}"}" local kubectl_out - if kubectl_out=$(kubectl get $(__kubectl_override_flags) -o template --template="${template}" "$1" 2>/dev/null); then + if kubectl_out=$(__kubectl_debug_out "kubectl get $(__kubectl_override_flags) -o template --template=\"${template}\" \"$1\""); then COMPREPLY+=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) ) fi } @@ -148,7 +155,7 @@ __kubectl_get_resource() { if [[ ${#nouns[@]} -eq 0 ]]; then local kubectl_out - if kubectl_out=$(kubectl api-resources $(__kubectl_override_flags) -o name --cached --request-timeout=5s --verbs=get 2>/dev/null); then + 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 @@ -195,7 +202,7 @@ __kubectl_get_containers() fi local last=${nouns[${len} -1]} local kubectl_out - if kubectl_out=$(kubectl get $(__kubectl_override_flags) -o template --template="${template}" pods "${last}" 2>/dev/null); then + if kubectl_out=$(__kubectl_debug_out "kubectl get $(__kubectl_override_flags) -o template --template=\"${template}\" pods \"${last}\""); then COMPREPLY=( $( compgen -W "${kubectl_out[*]}" -- "$cur" ) ) fi } @@ -228,7 +235,7 @@ __kubectl_cp() local template namespace kubectl_out template="{{ range .items }}{{ .metadata.namespace }}/{{ .metadata.name }}: {{ end }}" namespace="${cur%%/*}" - if kubectl_out=( $(kubectl get $(__kubectl_override_flags) --namespace "${namespace}" -o template --template="${template}" pods 2>/dev/null) ); then + if kubectl_out=$(__kubectl_debug_out "kubectl get $(__kubectl_override_flags) --namespace \"${namespace}\" -o template --template=\"${template}\" pods"); then COMPREPLY=( $(compgen -W "${kubectl_out[*]}" -- "${cur}") ) fi return @@ -241,7 +248,7 @@ __kubectl_cp() esac } -__custom_func() { +__kubectl_custom_func() { case ${last_command} in kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_edit | kubectl_patch |\ kubectl_annotate | kubectl_expose | kubectl_scale | kubectl_autoscale | kubectl_taint | kubectl_rollout_* |\