scwrypts/plugins/kubectl/driver/kubectl.completion.zsh

47 lines
1.2 KiB
Bash
Raw Permalink Normal View History

#####################################################################
command -v compdef >/dev/null 2>&1 || return 0
#####################################################################
2023-12-12 00:07:09 +00:00
for CLI in kubectl helm flux
do
eval "_${CLI[1]}() {
local SUBSESSION=0
echo \${words[2]} | grep -q '^[0-9]\\+$' && SUBSESSION=\${words[2]}
2023-12-12 00:07:09 +00:00
local PASSTHROUGH_WORDS=($CLI)
[[ \$CURRENT -gt 2 ]] && echo \${words[2]} | grep -qv '^[0-9]\\+$' && {
local KUBECONTEXT=\$(k \$SUBSESSION meta get context)
local NAMESPACE=\$(k \$SUBSESSION meta get namespace)
2023-12-12 00:07:09 +00:00
[ \$KUBECONTEXT ] \
&& PASSTHROUGH_WORDS+=($([[ $CLI =~ ^helm$ ]] && echo '--kube-context' || echo '--context') \$KUBECONTEXT) \
;
[ \$NAMESPACE ] \
&& PASSTHROUGH_WORDS+=(--namespace \$NAMESPACE) \
;
}
2023-12-12 00:07:09 +00:00
local DELIMIT_COUNT=0
local WORD
for WORD in \${words[@]:1}
do
case \$WORD in
[0-9]* ) continue ;;
-- )
echo \$words | grep -q 'exec' && ((DELIMIT_COUNT+=1))
[[ \$DELIMIT_COUNT -eq 0 ]] && ((DELIMIT_COUNT+=1)) && continue
;;
esac
PASSTHROUGH_WORDS+=(\"\$WORD\")
done
2023-12-12 00:07:09 +00:00
echo \"\$words\" | grep -q '\\s\\+$' && PASSTHROUGH_WORDS+=(' ')
2023-12-12 00:07:09 +00:00
words=\"\$PASSTHROUGH_WORDS\"
_$CLI
}
"
2023-12-12 00:07:09 +00:00
compdef _${CLI[1]} ${CLI[1]}
done