a3ab45db8b10a44329d7b715e2457fdd666c696e

Author
Jon Mosco <1970496+jonmosco@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

updating kube-ps1 to align with upstream changes (#6995)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/kube-ps1/README.md b/plugins/kube-ps1/README.md
  2index a572773a3f891a1859b671763f5f0c86d97a80df..fcb73cd2d5afc67f42e1494c233363c74ee2967e 100644
  3--- a/plugins/kube-ps1/README.md
  4+++ b/plugins/kube-ps1/README.md
  5@@ -1,13 +1,12 @@
  6-Kubernetes prompt for zsh
  7-=========================
  8+# Kubernetes prompt for zsh
  9 
 10-A Kubernetes (k8s) zsh prompt that displays the current cluster cluster
 11+A Kubernetes zsh prompt that displays the current cluster cluster
 12 and the namespace.
 13 
 14 Inspired by several tools used to simplify usage of kubectl
 15 
 16-NOTE: If you are not using zsh, check out [kube-ps1](https://github.com/jonmosco/kube-ps1) designed for bash
 17-as well as zsh.
 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 
 21 ## Requirements
 22 
 23@@ -32,28 +31,33 @@ fast switching between clusters and namespaces.
 24 The prompt layout is:
 25 
 26 ```
 27-(<logo>|<cluster>:<namespace>)
 28+(<symbol>|<cluster>:<namespace>)
 29 ```
 30 
 31-Supported platforms:
 32-* k8s - Kubernetes
 33-* ocp - OpenShift
 34+## Enabling
 35 
 36-## Install
 37+In order to use kube-ps1 with Oh My Zsh, you'll need to enable them in the
 38+.zshrc file. You'll find the zshrc file in your $HOME directory. Open it with
 39+your favorite text editor and you'll see a spot to list all the plugins you
 40+want to load.
 41 
 42-1. Clone this repository
 43-2. Source the kube-ps1.zsh in your ~./.zshrc
 44-
 45-ZSH:
 46+```shell
 47+vim $HOME/.zshrc
 48 ```
 49-source path/kube-ps1.sh
 50-PROMPT='$(kube_ps1) '
 51+
 52+Add kube-ps1 to the list of enabled plugins:
 53+
 54+```shell
 55+plugins=(
 56+  git
 57+  kube-ps1
 58+)
 59 ```
 60 
 61 ## Colors
 62 
 63-The colors are of my opinion. Blue was used as the prefix to match the Kubernetes
 64-color as closely as possible. Red was chosen as the cluster name to stand out, and cyan
 65+Blue was used as the prefix to match the Kubernetes color as closely as
 66+possible. Red was chosen as the cluster name to stand out, and cyan
 67 for the namespace.  These can of course be changed.
 68 
 69 ## Customization
 70@@ -62,14 +66,15 @@ The default settings can be overridden in ~/.zshrc
 71 
 72 | Variable | Default | Meaning |
 73 | :------- | :-----: | ------- |
 74-| `KUBE_PS1_DEFAULT` | `true` | Default settings for the prompt |
 75+| `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
 76 | `KUBE_PS1_PREFIX` | `(` | Prompt opening character  |
 77-| `KUBE_PS1_DEFAULT_LABEL` | `⎈ ` | Default prompt symbol |
 78+| `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` |
 79+| `KUBE_PS1_SYMBOL_DEFAULT` | `⎈ ` | Default prompt symbol. Unicode `\u2388` |
 80+| `KUBE_PS1_SYMBOL_USE_IMG` | `false` | ☸️  ,  Unicode `\u2638` as the prompt symbol |
 81+| `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` |
 82 | `KUBE_PS1_SEPERATOR` | `\|` | Separator between symbol and cluster name |
 83-| `KUBE_PS1_PLATFORM` | `kubectl` | Cluster type and binary to use |
 84 | `KUBE_PS1_DIVIDER` | `:` | Separator between cluster and namespace |
 85 | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
 86-| `KUBE_PS1_DEFAULT_LABEL_IMG` | `false` | Use Kubernetes img as the label: ☸️  |
 87 
 88 ## Contributors
 89 
 90diff --git a/plugins/kube-ps1/kube-ps1.plugin.zsh b/plugins/kube-ps1/kube-ps1.plugin.zsh
 91index e1cb4339de6dbf5f1d0e3e80486991253b9c108f..fadef80d7d32a0d3c2e788529c95876c3cb006c7 100644
 92--- a/plugins/kube-ps1/kube-ps1.plugin.zsh
 93+++ b/plugins/kube-ps1/kube-ps1.plugin.zsh
 94@@ -1,9 +1,10 @@
 95 #!/bin/zsh
 96 
 97 # Kubernetes prompt helper for bash/zsh
 98+# ported to oh-my-zsh
 99 # Displays current context and namespace
100 
101-# Copyright 2017 Jon Mosco
102+# Copyright 2018 Jon Mosco
103 #
104 #  Licensed under the Apache License, Version 2.0 (the "License");
105 # you may not use this file except in compliance with the License.
106@@ -21,32 +22,39 @@
107 [[ -n $DEBUG ]] && set -x
108 
109 setopt PROMPT_SUBST
110-add-zsh-hook precmd _kube_ps1_load
111+autoload -U add-zsh-hook
112+add-zsh-hook precmd _kube_ps1_update_cache
113 zmodload zsh/stat
114+zmodload zsh/datetime
115 
116 # Default values for the prompt
117-# Override these values in ~/.zshrc or ~/.bashrc
118-KUBE_PS1_DEFAULT="${KUBE_PS1_DEFAULT:=true}"
119-KUBE_PS1_PREFIX="("
120-KUBE_PS1_DEFAULT_LABEL="${KUBE_PS1_DEFAULT_LABEL:="⎈ "}"
121-KUBE_PS1_DEFAULT_LABEL_IMG="${KUBE_PS1_DEFAULT_LABEL_IMG:=false}"
122-KUBE_PS1_SEPERATOR="|"
123-KUBE_PS1_PLATFORM="${KUBE_PS1_PLATFORM:="kubectl"}"
124-KUBE_PS1_DIVIDER=":"
125-KUBE_PS1_SUFFIX=")"
126-KUBE_PS1_UNAME=$(uname)
127+# Override these values in ~/.zshrc
128+KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}"
129+KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}"
130+KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT:-\u2388 }"
131+KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}"
132+KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}"
133+KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}"
134+KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}"
135+KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}"
136+KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
137 KUBE_PS1_LAST_TIME=0
138 
139-kube_ps1_label () {
140+_kube_ps1_binary_check() {
141+  command -v "$1" >/dev/null
142+}
143 
144-  [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == false ]] && return
145+_kube_ps1_symbol() {
146+  [[ "${KUBE_PS1_SYMBOL_ENABLE}" == false ]] && return
147 
148-  if [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == true ]]; then
149-    local KUBE_LABEL="☸️ "
150-  fi
151+  KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_DEFAULT}"
152+  KUBE_PS1_SYMBOL_IMG="\u2638 "
153 
154-  KUBE_PS1_DEFAULT_LABEL="${KUBE_LABEL}"
155+  if [[ "${KUBE_PS1_SYMBOL_USE_IMG}" == true ]]; then
156+    KUBE_PS1_SYMBOL="${KUBE_PS1_SYMBOL_IMG}"
157+  fi
158 
159+  echo "${KUBE_PS1_SYMBOL}"
160 }
161 
162 _kube_ps1_split() {
163@@ -56,23 +64,45 @@ _kube_ps1_split() {
164 }
165 
166 _kube_ps1_file_newer_than() {
167-
168   local mtime
169   local file=$1
170   local check_time=$2
171-  mtime=$(stat +mtime "${file}")
172 
173-  [ "${mtime}" -gt "${check_time}" ]
174+  zmodload -e "zsh/stat"
175+  if [[ "$?" -eq 0 ]]; then
176+    mtime=$(stat +mtime "${file}")
177+  elif stat -c "%s" /dev/null &> /dev/null; then
178+    # GNU stat
179+    mtime=$(stat -c %Y "${file}")
180+  else
181+    # BSD stat
182+    mtime=$(stat -f %m "$file")
183+  fi
184 
185+  [[ "${mtime}" -gt "${check_time}" ]]
186 }
187 
188-_kube_ps1_load() {
189-  # kubectl will read the environment variable $KUBECONFIG
190-  # otherwise set it to ~/.kube/config
191+_kube_ps1_update_cache() {
192   KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
193+  if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then