From 7ae4f76f6dda1521505c57880ea1e5ee2f1aa183 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Wed, 5 Jan 2022 09:03:30 +0100 Subject: refactor(kubectl): optimize completion generation --- plugins/kubectl/kubectl.plugin.zsh | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'plugins/kubectl') diff --git a/plugins/kubectl/kubectl.plugin.zsh b/plugins/kubectl/kubectl.plugin.zsh index 3630facaa..bf602bb7b 100644 --- a/plugins/kubectl/kubectl.plugin.zsh +++ b/plugins/kubectl/kubectl.plugin.zsh @@ -1,13 +1,22 @@ if (( $+commands[kubectl] )); then - __KUBECTL_COMPLETION_FILE="${ZSH_CACHE_DIR}/kubectl_completion" - - if [[ ! -f $__KUBECTL_COMPLETION_FILE || ! -s $__KUBECTL_COMPLETION_FILE ]]; then - kubectl completion zsh >! $__KUBECTL_COMPLETION_FILE - fi - - [[ -f $__KUBECTL_COMPLETION_FILE ]] && source $__KUBECTL_COMPLETION_FILE - - unset __KUBECTL_COMPLETION_FILE + # TODO: 2022-01-05: remove this block + # remove old generated files + command rm -f "$ZSH_CACHE_DIR/kubectl_completion" + + # TODO: 2022-01-05: remove this bit of code as it exists in oh-my-zsh.sh + # Add completions folder in $ZSH_CACHE_DIR + command mkdir -p "$ZSH_CACHE_DIR/completions" + (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath) + + # If the completion file doesn't exist yet, we need to autoload it and + # bind it to `kubectl`. Otherwise, compinit will have already done that. + if [[ ! -f "$ZSH_CACHE_DIR/completions/_kubectl" ]]; then + typeset -g -A _comps + autoload -Uz _kubectl + _comps[kubectl]=_kubectl + fi + + kubectl completion zsh >! "$ZSH_CACHE_DIR/completions/_kubectl" &| fi # This command is used a LOT both below and in daily life @@ -97,8 +106,9 @@ alias kdd='kubectl describe deployment' alias kdeld='kubectl delete deployment' alias ksd='kubectl scale deployment' alias krsd='kubectl rollout status deployment' -kres(){ - kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S) + +function kres(){ + kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S) } # Rollout management. @@ -170,9 +180,9 @@ alias kdelcj='kubectl delete cronjob' # Only run if the user actually has kubectl installed if (( ${+_comps[kubectl]} )); then - kj() { kubectl "$@" -o json | jq; } - kjx() { kubectl "$@" -o json | fx; } - ky() { kubectl "$@" -o yaml | yh; } + function kj() { kubectl "$@" -o json | jq; } + function kjx() { kubectl "$@" -o json | fx; } + function ky() { kubectl "$@" -o yaml | yh; } compdef kj=kubectl compdef kjx=kubectl -- cgit v1.2.3-70-g09d2 From 4f2d8b4d4cbc51e609f4b568e87907883422ab41 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 17 Jan 2022 12:46:20 +0100 Subject: fix(kubectl): source completion instead of autoloading it --- plugins/kubectl/kubectl.plugin.zsh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'plugins/kubectl') diff --git a/plugins/kubectl/kubectl.plugin.zsh b/plugins/kubectl/kubectl.plugin.zsh index bf602bb7b..6edb59751 100644 --- a/plugins/kubectl/kubectl.plugin.zsh +++ b/plugins/kubectl/kubectl.plugin.zsh @@ -8,15 +8,15 @@ if (( $+commands[kubectl] )); then command mkdir -p "$ZSH_CACHE_DIR/completions" (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath) - # If the completion file doesn't exist yet, we need to autoload it and - # bind it to `kubectl`. Otherwise, compinit will have already done that. + # 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/_kubectl" ]]; then - typeset -g -A _comps - autoload -Uz _kubectl - _comps[kubectl]=_kubectl + kubectl completion zsh >| "$ZSH_CACHE_DIR/completions/_kubectl" + source "$ZSH_CACHE_DIR/completions/_kubectl" + else + source "$ZSH_CACHE_DIR/completions/_kubectl" + kubectl completion zsh >| "$ZSH_CACHE_DIR/completions/_kubectl" &| fi - - kubectl completion zsh >! "$ZSH_CACHE_DIR/completions/_kubectl" &| fi # This command is used a LOT both below and in daily life -- cgit v1.2.3-70-g09d2 From 897fa09f1517e648bfbeb8549db4848a66f19c4f Mon Sep 17 00:00:00 2001 From: Michael Favia Date: Wed, 9 Feb 2022 03:25:12 -0600 Subject: fix(kubectl): fix arguments in `keti` alias to allow completion (#10669) --- plugins/kubectl/kubectl.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/kubectl') diff --git a/plugins/kubectl/kubectl.plugin.zsh b/plugins/kubectl/kubectl.plugin.zsh index 6edb59751..eed5727d1 100644 --- a/plugins/kubectl/kubectl.plugin.zsh +++ b/plugins/kubectl/kubectl.plugin.zsh @@ -29,7 +29,7 @@ alias kca='_kca(){ kubectl "$@" --all-namespaces; unset -f _kca; }; _kca' alias kaf='kubectl apply -f' # Drop into an interactive terminal on a container -alias keti='kubectl exec -ti' +alias keti='kubectl exec -t -i' # Manage configuration quickly to switch contexts between local, dev ad staging. alias kcuc='kubectl config use-context' -- cgit v1.2.3-70-g09d2