93a2ba6b5f562385f29188cd363329dc2d835959

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

Message

kube-ps1: update to latest upstream version (c685ac8)

Closes #8009

Co-authored-by: Jon Mosco <jonny.mosco@gmail.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/kube-ps1/README.md b/plugins/kube-ps1/README.md
  2index fdc1a94e868aaad7c56e1960b6d7a7a765cdffb1..a1433727869adf6b655acb092ece118212bc2201 100644
  3--- a/plugins/kube-ps1/README.md
  4+++ b/plugins/kube-ps1/README.md
  5@@ -1,106 +1,246 @@
  6-# Kubernetes prompt for zsh
  7+kube-ps1: Kubernetes prompt for bash and zsh
  8+============================================
  9 
 10-A Kubernetes zsh prompt that displays the current cluster cluster
 11-and the namespace.
 12+A script that lets you add the current Kubernetes context and namespace
 13+configured on `kubectl` to your Bash/Zsh prompt strings (i.e. the `$PS1`).
 14 
 15-Inspired by several tools used to simplify usage of kubectl
 16+Inspired by several tools used to simplify usage of `kubectl`.
 17 
 18-NOTE: If you are not using zsh, check out [kube-ps1](https://github.com/jonmosco/kube-ps1)
 19-designed for bash as well as zsh.
 20+![prompt](img/screenshot2.png)
 21 
 22-## Requirements
 23-
 24-The default prompt assumes you have the kubectl command line utility installed.  It
 25-can be obtained here:
 26+![prompt_sol_light](img/screenshot-sol-light.png)
 27 
 28-[Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
 29+![prompt_img](img/screenshot-img.png)
 30 
 31-If using this with OpenShift, the oc tool needs installed.  It can be obtained from here:
 32+![prompt demo](img/kube-ps1.gif)
 33 
 34-[OC Client Tools](https://www.openshift.org/download.html)
 35+## Installing
 36 
 37-## Helper utilities
 38+### MacOS
 39 
 40-There are several great tools that make using kubectl very enjoyable.
 41+Homebrew package manager:
 42 
 43-[kubectx and kubenx](https://github.com/ahmetb/kubectx) are great for
 44-fast switching between clusters and namespaces.
 45+```
 46+$ brew update
 47+$ brew install kube-ps1
 48+```
 49+### From Source
 50 
 51-## Prompt Structure
 52+1. Clone this repository
 53+2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`
 54 
 55-The prompt layout is:
 56+### Arch Linux
 57+AUR Package available at [https://aur.archlinux.org/packages/kube-ps1/](https://aur.archlinux.org/packages/kube-ps1/).
 58 
 59+#### Zsh
 60+```sh
 61+source /path/to/kube-ps1.sh
 62+PROMPT='$(kube_ps1)'$PROMPT
 63 ```
 64-(<symbol>|<cluster>:<namespace>)
 65+#### Bash
 66+```sh
 67+source /path/to/kube-ps1.sh
 68+PS1='[\u@\h \W $(kube_ps1)]\$ '
 69 ```
 70 
 71-## Enabling
 72+### Zsh Plugin Managers
 73 
 74-In order to use kube-ps1 with Oh My Zsh, you'll need to enable them in the
 75-.zshrc file. You'll find the zshrc file in your $HOME directory. Open it with
 76-your favorite text editor and you'll see a spot to list all the plugins you
 77-want to load.
 78+#### Using [zplugin](https://github.com/zdharma/zplugin)
 79 
 80-```shell
 81-vim $HOME/.zshrc
 82+Update `.zshrc` with:
 83+```sh
 84+zplugin light jonmosco/kube-ps1
 85+PROMPT='$(kube_ps1)'$PROMPT
 86 ```
 87 
 88-Add kube-ps1 to the list of enabled plugins and enable it on the prompt:
 89+## Requirements
 90 
 91-```shell
 92-plugins=(
 93-  git
 94-  kube-ps1
 95-)
 96+The default prompt assumes you have the `kubectl` command line utility installed.
 97+Official installation instructions and binaries are available:
 98 
 99-# After the "source Oh My Zsh" line
100-PROMPT=$PROMPT'$(kube_ps1) '
101-# Or in double quotes, don't forget to escape
102-PROMPT=$PROMPT"\$(kube_ps1) "
103+[Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
104+
105diff --git a/plugins/kube-ps1/kube-ps1.plugin.zsh b/plugins/kube-ps1/kube-ps1.plugin.zsh
106index 3cb21284abc522a30b40cec888b6fd0611d69974..0f68ca93843bab9769e257cc992384b65c5efaba 100644
107--- a/plugins/kube-ps1/kube-ps1.plugin.zsh
108+++ b/plugins/kube-ps1/kube-ps1.plugin.zsh
109@@ -1,16 +1,15 @@
110-#!/bin/zsh
111+#!/bin/bash
112 
113 # Kubernetes prompt helper for bash/zsh
114-# ported to oh-my-zsh
115 # Displays current context and namespace
116 
117-# Copyright 2018 Jon Mosco
118+# Copyright 2019 Jon Mosco
119 #
120 #  Licensed under the Apache License, Version 2.0 (the "License");
121 # you may not use this file except in compliance with the License.
122 # You may obtain a copy of the License at
123 #
124-#     https://www.apache.org/licenses/LICENSE-2.0
125+#     http://www.apache.org/licenses/LICENSE-2.0
126 #
127 # Unless required by applicable law or agreed to in writing, software
128 # distributed under the License is distributed on an "AS IS" BASIS,
129@@ -21,39 +20,150 @@
130 # Debug
131 [[ -n $DEBUG ]] && set -x
132 
133-setopt PROMPT_SUBST
134-autoload -U add-zsh-hook
135-add-zsh-hook precmd _kube_ps1_update_cache
136-zmodload zsh/stat
137-zmodload zsh/datetime
138-
139 # Default values for the prompt
140-# Override these values in ~/.zshrc
141+# Override these values in ~/.zshrc or ~/.bashrc
142 KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
143 KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
144-KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
145+KUBE_PS1_SYMBOL_DEFAULT=${KUBE_PS1_SYMBOL_DEFAULT:-$'\u2388 '}
146 KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
147 KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
148+KUBE_PS1_CONTEXT_ENABLE="${KUBE_PS1_CONTEXT_ENABLE:-true}"
149+KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
150 KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
151 KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
152-KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
153 KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
154+KUBE_PS1_SYMBOL_COLOR="${KUBE_PS1_SYMBOL_COLOR-blue}"
155+KUBE_PS1_CTX_COLOR="${KUBE_PS1_CTX_COLOR-red}"
156+KUBE_PS1_NS_COLOR="${KUBE_PS1_NS_COLOR-cyan}"
157+KUBE_PS1_BG_COLOR="${KUBE_PS1_BG_COLOR}"
158+KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
159+KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
160 KUBE_PS1_LAST_TIME=0
161-KUBE_PS1_ENABLED=true
162+KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}"
163+KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"
164+
165+# Determine our shell
166+if [ "${ZSH_VERSION-}" ]; then
167+  KUBE_PS1_SHELL="zsh"
168+elif [ "${BASH_VERSION-}" ]; then
169+  KUBE_PS1_SHELL="bash"
170+fi
171+
172+_kube_ps1_init() {
173+  [[ -f "${KUBE_PS1_DISABLE_PATH}" ]] && KUBE_PS1_ENABLED=off
174 
175-KUBE_PS1_COLOR_SYMBOL="%{$fg[blue]%}"
176-KUBE_PS1_COLOR_CONTEXT="%{$fg[red]%}"
177-KUBE_PS1_COLOR_NS="%{$fg[cyan]%}"
178+  case "${KUBE_PS1_SHELL}" in
179+    "zsh")
180+      _KUBE_PS1_OPEN_ESC="%{"
181+      _KUBE_PS1_CLOSE_ESC="%}"
182+      _KUBE_PS1_DEFAULT_BG="%k"
183+      _KUBE_PS1_DEFAULT_FG="%f"
184+      setopt PROMPT_SUBST
185+      autoload -U add-zsh-hook
186+      add-zsh-hook precmd _kube_ps1_update_cache
187+      zmodload -F zsh/stat b:zstat
188+      zmodload zsh/datetime
189+      ;;
190+    "bash")
191+      _KUBE_PS1_OPEN_ESC=$'\001'
192+      _KUBE_PS1_CLOSE_ESC=$'\002'
193+      _KUBE_PS1_DEFAULT_BG=$'\033[49m'
194+      _KUBE_PS1_DEFAULT_FG=$'\033[39m'
195+      [[ $PROMPT_COMMAND =~ _kube_ps1_update_cache ]] || PROMPT_COMMAND="_kube_ps1_update_cache;${PROMPT_COMMAND:-:}"
196+      ;;
197+  esac
198+}
199+
200+_kube_ps1_color_fg() {
201+  local KUBE_PS1_FG_CODE
202+  case "${1}" in
203+    black) KUBE_PS1_FG_CODE=0;;
204+    red) KUBE_PS1_FG_CODE=1;;
205+    green) KUBE_PS1_FG_CODE=2;;
206+    yellow) KUBE_PS1_FG_CODE=3;;
207+    blue) KUBE_PS1_FG_CODE=4;;
208+    magenta) KUBE_PS1_FG_CODE=5;;