Improve 'kubectl completion` to override short flags

This commit improves `kubectl completion` to override short flags in
addition to long flags.
This commit is contained in:
Kazuki Suda 2017-10-20 09:17:44 +09:00
parent adb7aed2b0
commit d2816bc1ba

View File

@ -36,22 +36,22 @@ import (
const ( const (
bashCompletionFunc = `# call kubectl get $1, bashCompletionFunc = `# call kubectl get $1,
__kubectl_override_flag_list=(kubeconfig cluster user context namespace server) __kubectl_override_flag_list=(--kubeconfig --cluster --user --context --namespace --server -n -s)
__kubectl_override_flags() __kubectl_override_flags()
{ {
local ${__kubectl_override_flag_list[*]} two_word_of of local ${__kubectl_override_flag_list[*]##*-} two_word_of of var
for w in "${words[@]}"; do for w in "${words[@]}"; do
if [ -n "${two_word_of}" ]; then if [ -n "${two_word_of}" ]; then
eval "${two_word_of}=\"--${two_word_of}=\${w}\"" eval "${two_word_of##*-}=\"${two_word_of}=\${w}\""
two_word_of= two_word_of=
continue continue
fi fi
for of in "${__kubectl_override_flag_list[@]}"; do for of in "${__kubectl_override_flag_list[@]}"; do
case "${w}" in case "${w}" in
--${of}=*) ${of}=*)
eval "${of}=\"${w}\"" eval "${of##*-}=\"${w}\""
;; ;;
--${of}) ${of})
two_word_of="${of}" two_word_of="${of}"
;; ;;
esac esac
@ -60,9 +60,9 @@ __kubectl_override_flags()
namespace="--all-namespaces" namespace="--all-namespaces"
fi fi
done done
for of in "${__kubectl_override_flag_list[@]}"; do for var in "${__kubectl_override_flag_list[@]##*-}"; do
if eval "test -n \"\$${of}\""; then if eval "test -n \"\$${var}\""; then
eval "echo \${${of}}" eval "echo \${${var}}"
fi fi
done done
} }