7ae4f76f6dda1521505c57880ea1e5ee2f1aa183

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(kubectl): optimize completion generation

Diff

 1diff --git a/plugins/kubectl/kubectl.plugin.zsh b/plugins/kubectl/kubectl.plugin.zsh
 2index 3630facaa5d1a58402bd36244feea0eb62bb15fe..bf602bb7b85b89ff1a6889a17b19ee6efa95453a 100644
 3--- a/plugins/kubectl/kubectl.plugin.zsh
 4+++ b/plugins/kubectl/kubectl.plugin.zsh
 5@@ -1,13 +1,22 @@
 6 if (( $+commands[kubectl] )); then
 7-    __KUBECTL_COMPLETION_FILE="${ZSH_CACHE_DIR}/kubectl_completion"
 8-
 9-    if [[ ! -f $__KUBECTL_COMPLETION_FILE || ! -s $__KUBECTL_COMPLETION_FILE ]]; then
10-        kubectl completion zsh >! $__KUBECTL_COMPLETION_FILE
11-    fi
12-
13-    [[ -f $__KUBECTL_COMPLETION_FILE ]] && source $__KUBECTL_COMPLETION_FILE
14-
15-    unset __KUBECTL_COMPLETION_FILE
16+  # TODO: 2022-01-05: remove this block
17+  # remove old generated files
18+  command rm -f "$ZSH_CACHE_DIR/kubectl_completion"
19+
20+  # TODO: 2022-01-05: remove this bit of code as it exists in oh-my-zsh.sh
21+  # Add completions folder in $ZSH_CACHE_DIR
22+  command mkdir -p "$ZSH_CACHE_DIR/completions"
23+  (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
24+
25+  # If the completion file doesn't exist yet, we need to autoload it and
26+  # bind it to `kubectl`. Otherwise, compinit will have already done that.
27+  if [[ ! -f "$ZSH_CACHE_DIR/completions/_kubectl" ]]; then
28+    typeset -g -A _comps
29+    autoload -Uz _kubectl
30+    _comps[kubectl]=_kubectl
31+  fi
32+
33+  kubectl completion zsh >! "$ZSH_CACHE_DIR/completions/_kubectl" &|
34 fi
35 
36 # This command is used a LOT both below and in daily life
37@@ -97,8 +106,9 @@ alias kdd='kubectl describe deployment'
38 alias kdeld='kubectl delete deployment'
39 alias ksd='kubectl scale deployment'
40 alias krsd='kubectl rollout status deployment'
41-kres(){
42-    kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S)
43+
44+function kres(){
45+  kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S)
46 }
47 
48 # Rollout management.
49@@ -170,9 +180,9 @@ alias kdelcj='kubectl delete cronjob'
50 
51 # Only run if the user actually has kubectl installed
52 if (( ${+_comps[kubectl]} )); then
53-  kj() { kubectl "$@" -o json | jq; }
54-  kjx() { kubectl "$@" -o json | fx; }
55-  ky() { kubectl "$@" -o yaml | yh; }
56+  function kj() { kubectl "$@" -o json | jq; }
57+  function kjx() { kubectl "$@" -o json | fx; }
58+  function ky() { kubectl "$@" -o yaml | yh; }
59 
60   compdef kj=kubectl
61   compdef kjx=kubectl