refactor(plugins): use cached background completion generation (#13662)

Move invoke, istioctl, kn, kompose, kops, oc, operator-sdk, please and
svcat off sourcing `<($cmd completion ...)` on every startup. The
completion script is now cached under $ZSH_CACHE_DIR/completions and
regenerated in the background, following the pattern used by the helm
plugin.

Writes go through a temp file (prefixed with a dot to keep it out of
$fpath) that is moved into place atomically with `zf_mv`, so a
half-written file is never sourced.
This commit is contained in:
Alexander Schaber authored and GitHub committed 2026-11-27 13:23:29 -08:00
1 parent 53f0790157
commit 924db8c81b
9 files changed
+151 -27

No files matched your search

+17 -4
View File
@@ -1,5 +1,18 @@
# Autocompletion for invoke.
#
if [ $commands[invoke] ]; then
source <(invoke --print-completion-script=zsh)
# Autocompletion for the Invoke CLI (invoke).
if (( ! $+commands[invoke] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_invoke" ]]; then
invoke --print-completion-script=zsh | tee "$ZSH_CACHE_DIR/completions/_invoke" >/dev/null
source "$ZSH_CACHE_DIR/completions/_invoke"
else
source "$ZSH_CACHE_DIR/completions/_invoke"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._invoke"
zf_mv -f -- =( invoke --print-completion-script=zsh ) "$ZSH_CACHE_DIR/completions/_invoke"
} &|
fi
+17 -3
View File
@@ -1,4 +1,18 @@
if [ $commands[istioctl] ]; then
source <(istioctl completion zsh)
compdef _istioctl istioctl
# Autocompletion for the Istio CLI (istioctl).
if (( ! $+commands[istioctl] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_istioctl" ]]; then
istioctl completion zsh | tee "$ZSH_CACHE_DIR/completions/_istioctl" >/dev/null
source "$ZSH_CACHE_DIR/completions/_istioctl"
else
source "$ZSH_CACHE_DIR/completions/_istioctl"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._istioctl"
zf_mv -f -- =( istioctl completion zsh ) "$ZSH_CACHE_DIR/completions/_istioctl"
} &|
fi
+17 -4
View File
@@ -1,8 +1,21 @@
# Autocompletion for kn, the command line interface for knative
# Autocompletion for kn, the command line interface for Knative.
#
# Author: https://github.com/btannous
if [ $commands[kn] ]; then
source <(kn completion zsh)
compdef _kn kn
if (( ! $+commands[kn] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_kn" ]]; then
kn completion zsh | tee "$ZSH_CACHE_DIR/completions/_kn" >/dev/null
source "$ZSH_CACHE_DIR/completions/_kn"
else
source "$ZSH_CACHE_DIR/completions/_kn"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._kn"
zf_mv -f -- =( kn completion zsh ) "$ZSH_CACHE_DIR/completions/_kn"
} &|
fi
+17 -2
View File
@@ -1,3 +1,18 @@
if [ $commands[kompose] ]; then
source <(kompose completion zsh)
# Autocompletion for the Kompose CLI (kompose).
if (( ! $+commands[kompose] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_kompose" ]]; then
kompose completion zsh | tee "$ZSH_CACHE_DIR/completions/_kompose" >/dev/null
source "$ZSH_CACHE_DIR/completions/_kompose"
else
source "$ZSH_CACHE_DIR/completions/_kompose"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._kompose"
zf_mv -f -- =( kompose completion zsh ) "$ZSH_CACHE_DIR/completions/_kompose"
} &|
fi
+17 -2
View File
@@ -1,3 +1,18 @@
if [ $commands[kops] ]; then
source <(kops completion zsh)
# Autocompletion for the Kubernetes Operations CLI (kops).
if (( ! $+commands[kops] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_kops" ]]; then
kops completion zsh | tee "$ZSH_CACHE_DIR/completions/_kops" >/dev/null
source "$ZSH_CACHE_DIR/completions/_kops"
else
source "$ZSH_CACHE_DIR/completions/_kops"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._kops"
zf_mv -f -- =( kops completion zsh ) "$ZSH_CACHE_DIR/completions/_kops"
} &|
fi
+16 -3
View File
@@ -2,7 +2,20 @@
#
# Author: https://github.com/kevinkirkup
if [ $commands[oc] ]; then
source <(oc completion zsh)
compdef _oc oc
if (( ! $+commands[oc] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_oc" ]]; then
oc completion zsh | tee "$ZSH_CACHE_DIR/completions/_oc" >/dev/null
source "$ZSH_CACHE_DIR/completions/_oc"
else
source "$ZSH_CACHE_DIR/completions/_oc"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._oc"
zf_mv -f -- =( oc completion zsh ) "$ZSH_CACHE_DIR/completions/_oc"
} &|
fi
+17 -3
View File
@@ -1,4 +1,18 @@
if [ $commands[operator-sdk] ]; then
source <(operator-sdk completion zsh)
compdef _operator-sdk operator-sdk
# Autocompletion for the Operator SDK CLI (operator-sdk).
if (( ! $+commands[operator-sdk] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_operator-sdk" ]]; then
operator-sdk completion zsh | tee "$ZSH_CACHE_DIR/completions/_operator-sdk" >/dev/null
source "$ZSH_CACHE_DIR/completions/_operator-sdk"
else
source "$ZSH_CACHE_DIR/completions/_operator-sdk"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._operator-sdk"
zf_mv -f -- =( operator-sdk completion zsh ) "$ZSH_CACHE_DIR/completions/_operator-sdk"
} &|
fi
+17 -2
View File
@@ -1,5 +1,20 @@
if (( $+commands[plz] )); then
source <(plz --completion_script)
# Autocompletion for the Please build system CLI (plz).
if (( ! $+commands[plz] )); then
return
fi
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_plz" ]]; then
plz --completion_script | tee "$ZSH_CACHE_DIR/completions/_plz" >/dev/null
source "$ZSH_CACHE_DIR/completions/_plz"
else
source "$ZSH_CACHE_DIR/completions/_plz"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._plz"
zf_mv -f -- =( plz --completion_script ) "$ZSH_CACHE_DIR/completions/_plz"
} &|
fi
alias pb='plz build'
+16 -4
View File
@@ -1,6 +1,18 @@
# Autocompletion for svcat.
#
# Autocompletion for the Service Catalog CLI (svcat).
if (( ! $+commands[svcat] )); then
return
fi
if [ $commands[svcat] ]; then
source <(svcat completion zsh)
# If the completion file does not exist, generate it and then source it
# Otherwise, source it and regenerate in the background
if [[ ! -f "$ZSH_CACHE_DIR/completions/_svcat" ]]; then
svcat completion zsh | tee "$ZSH_CACHE_DIR/completions/_svcat" >/dev/null
source "$ZSH_CACHE_DIR/completions/_svcat"
else
source "$ZSH_CACHE_DIR/completions/_svcat"
zmodload -F zsh/files b:zf_mv
() {
local TMPPREFIX="$ZSH_CACHE_DIR/completions/._svcat"
zf_mv -f -- =( svcat completion zsh ) "$ZSH_CACHE_DIR/completions/_svcat"
} &|
fi