Skip to content

Commit

Permalink
Avoid conflicts with other cobra auto completion
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Jul 10, 2019
1 parent d59a603 commit 4200d6c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/kubectl/cmd/cmd.go
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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_* |\
Expand Down

0 comments on commit 4200d6c

Please sign in to comment.