22bda7799b1e1500a701e1a55a189c77119e9e5f

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

Message

fix(kubectx): fix display of k8s contexts without double quotes (#12534)

BREAKING CHANGE: the `kubectx` plugin now properly displays contexts from the
kubectx_mapping array, without using double quotes. Please refer to the plugin
README to see how to use it properly.

Closes #12534

Co-authored-by: Ken Kelly <ken@kenhkelly.us>

Diff

 1diff --git a/plugins/kubectx/README.md b/plugins/kubectx/README.md
 2index d924e745c1d0a3d8798d33631026dbc48b4a7f9e..e3987b42f4737bcc229c2bdfcc97ea427cfb9f21 100644
 3--- a/plugins/kubectx/README.md
 4+++ b/plugins/kubectx/README.md
 5@@ -1,25 +1,55 @@
 6 # kubectx - show active kubectl context
 7 
 8-This plugins adds ```kubectx_prompt_info()``` function. It shows name of the
 9-active kubectl context (```kubectl config current-context```).
10+This plugins adds `kubectx_prompt_info()` function. It shows name of the active
11+kubectl context (`kubectl config current-context`).
12 
13 You can use it to customize prompt and know if You are on prod cluster ;)
14 
15-_Example_. Add to **.zshrc**:
16+To use this plugin, add `kubectx` to the plugins array in your zshrc file:
17 
18+```zsh
19+plugins=(... kubectx)
20 ```
21+
22+### Usage
23+
24+Add to **.zshrc**:
25+
26+```zsh
27+# right prompt
28 RPS1='$(kubectx_prompt_info)'
29+# left prompt
30+PROMPT="$PROMPT"'$(kubectx_prompt_info)'
31 ```
32 
33-### custom ctx names
34+### Custom context names
35 
36-One can rename default context name for better readability.
37+You can rename the default context name for better readability or additional formatting.
38+These values accept [prompt expansion sequences](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html)
39+such as `%F{color}`, `%f`, `%K{color}`, `%k`, `%B`, `%b`, `%U`, `%u`, `%S`, `%s`, `%{...%}`.
40 
41-_Example_. Add to **.zshrc**:
42+**Example**: add this to your .zshrc file:
43+
44+```zsh
45+kubectx_mapping[minikube]="mini"
46+kubectx_mapping[context_name_from_kubeconfig]="$emoji[wolf_face]"
47+kubectx_mapping[production_cluster]="%{$fg[yellow]%}prod!%{$reset_color%}"
48+# contexts with spaces
49+kubectx_mapping[context\ with\ spaces]="%F{red}spaces%f"
50+# don't use quotes as it will break the prompt
51+kubectx_mapping["context with spaces"]="%F{red}spaces%f" # ti
52 ```
53-kubectx_mapping["minikube"]="mini"
54-kubectx_mapping["context_name_from_kubeconfig"]="$emoji[wolf_face]"
55-kubectx_mapping["production_cluster"]="%{$fg[yellow]%}prod!%{$reset_color%}"
56+
57+You can also define the whole mapping array at once:
58+
59+```zsh
60+typeset -A kubectx_mapping
61+kubectx_mapping=(
62+  minikube                      "mini"
63+  context_name_from_kubeconfig  "$emoji[wolf_face]"
64+  production_cluster            "%{$fg[yellow]%}prod!%{$reset_color%}"
65+  "context with spaces"         "%F{red}spaces%f"
66+)
67 ```
68 
69 ![staging](stage.png)
70diff --git a/plugins/kubectx/kubectx.plugin.zsh b/plugins/kubectx/kubectx.plugin.zsh
71index a3210faccd2ce11928b0b689174d60db6593e017..f1ca990ad923851576ca1a9a63d96260af8e62b1 100644
72--- a/plugins/kubectx/kubectx.plugin.zsh
73+++ b/plugins/kubectx/kubectx.plugin.zsh
74@@ -7,7 +7,9 @@ function kubectx_prompt_info() {
75 
76   [[ -n "$current_ctx" ]] || return
77 
78-  # use value in associative array if it exists
79-  # otherwise fall back to the context name
80-  echo "${kubectx_mapping[\"$current_ctx\"]:-${current_ctx:gs/%/%%}}"
81+  # Use value in associative array if it exists, otherwise fall back to the context name
82+  #
83+  # Note: we need to escape the % character in the prompt string when coming directly from
84+  # the context name, as it could contain a % character.
85+  echo "${kubectx_mapping[$current_ctx]:-${current_ctx:gs/%/%%}}"
86 }