Skip to content

Commit fe5484d

Browse files
authored
Merge pull request #38 from jonmosco/state_machine
Clean up kubeon and off to match #37
2 parents 68bf82f + 550aa60 commit fe5484d

File tree

3 files changed

+41
-51
lines changed

3 files changed

+41
-51
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
### kube-ps1 project CHANGELOG
2+
3+
### BUG FIXES:
4+
5+
* kubeon and kubeoff: Fix state on already running shells [GH-37]

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ Inspired by several tools used to simplify usage of `kubectl`.
1919
1. Clone this repository
2020
2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`
2121

22+
This project is now available in brew ports!
23+
24+
```
25+
$ brew update
26+
$ brew install kube-ps1
27+
```
28+
2229
For Zsh:
2330
```sh
2431
source /path/to/kube-ps1.sh
@@ -138,32 +145,21 @@ Blue was used for the default symbol to match the Kubernetes color as closely
138145
as possible. Red was chosen as the cluster name to stand out, and cyan for the
139146
namespace.
140147

141-
Set the variable to a null string if you do not want color for each
148+
Set the variable to an empty string if you do not want color for each
142149
prompt section:
143150

144151
```
145-
KUBE_PS1_CTX_COLOR=""
152+
KUBE_PS1_CTX_COLOR=''
146153
```
147154

148155
Names are usable for the following colors:
149-
`black, red, green, yellow, blue, magenta, cyan, and white`.
150-
151-
The appropriate color codes and escape sequences must be used in place of the
152-
color names. The default colors assigned have the following escape ANSI
153-
escape sequences and tput capabilities depending on your terminal capabilities:
154156

155157
```
156-
tput:
157-
blue: $(tput setaf 33)
158-
red: $(tput setaf 1)
159-
cyan: $(tput setaf 37)
160-
161-
ANSI:
162-
blue: \e[34m
163-
red: \e[31m
164-
cyan: \e[36m
158+
black, red, green, yellow, blue, magenta, cyan
165159
```
166160

161+
256 colors are available by specifying the numerical value.
162+
167163
### Bug Reports and shell configuration
168164

169165
Due to the vast ways of customizing the shell, please try the prompt with a

kube-ps1.sh

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ elif [ "${BASH_VERSION-}" ]; then
4747
KUBE_PS1_SHELL="bash"
4848
fi
4949

50-
_kube_ps1_shell_settings() {
50+
_kube_ps1_init() {
51+
[[ -f "${KUBE_PS1_DISABLE_PATH}" ]] && KUBE_PS1_ENABLED=off
52+
5153
case "${KUBE_PS1_SHELL}" in
5254
"zsh")
5355
setopt PROMPT_SUBST
@@ -186,7 +188,7 @@ _kube_ps1_symbol() {
186188
# [[ "$LC_CTYPE $LC_ALL" =~ "UTF" && $TERM != "linux" ]]
187189
# Bash only supports \u \U since 4.2
188190
if [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
189-
if ((BASH_VERSINFO[0] >= 4)); then
191+
if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388 ' != "\\u2388 " ]]; then
190192
_KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT}"
191193
_KUBE_PS1_SYMBOL_IMG=$'\u2638 '
192194
else
@@ -222,37 +224,36 @@ _kube_ps1_file_newer_than() {
222224

223225
if [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
224226
mtime=$(stat +mtime "${file}")
225-
elif [ x"$KUBE_PS1_UNAME" = x"Linux" ]; then
227+
elif [[ "$KUBE_PS1_UNAME" == "Linux" ]]; then
226228
mtime=$(stat -c %Y "${file}")
227229
else
228230
mtime=$(stat -f %m "$file")
229231
fi
230232

231-
[ "${mtime}" -gt "${check_time}" ]
233+
[[ "${mtime}" -gt "${check_time}" ]]
232234
}
233235

234236
_kube_ps1_update_cache() {
235-
local conf
236-
237-
if ! _kube_ps1_enabled; then
238-
return
239-
fi
237+
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
240238

241239
if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
240+
# No ability to fetch context/namespace; display N/A.
242241
KUBE_PS1_CONTEXT="BINARY-N/A"
243242
KUBE_PS1_NAMESPACE="N/A"
244243
return
245244
fi
246245

247246
if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
247+
# User changed KUBECONFIG; unconditionally refetch.
248248
KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
249249
_kube_ps1_get_context_ns
250250
return
251251
fi
252252

253253
# kubectl will read the environment variable $KUBECONFIG
254254
# otherwise set it to ~/.kube/config
255-
for conf in $(_kube_ps1_split : "${KUBECONFIG:-$HOME/.kube/config}"); do
255+
local conf
256+
for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
256257
[[ -r "${conf}" ]] || continue
257258
if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
258259
_kube_ps1_get_context_ns
@@ -287,8 +288,8 @@ _kube_ps1_get_context_ns() {
287288
fi
288289
}
289290

290-
# Set shell options
291-
_kube_ps1_shell_settings
291+
# Set kube-ps1 shell defaults
292+
_kube_ps1_init
292293

293294
_kubeon_usage() {
294295
cat <<"EOF"
@@ -317,48 +318,37 @@ EOF
317318
}
318319

319320
kubeon() {
320-
if [[ "$#" -eq 0 ]]; then
321-
KUBE_PS1_ENABLED=on
322-
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
321+
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
323322
_kubeon_usage
324323
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
325-
rm -f "${KUBE_PS1_DISABLE_PATH}"
326-
else
324+
rm -f -- "${KUBE_PS1_DISABLE_PATH}"
325+
elif [[ "$#" -ne 0 ]]; then
327326
echo -e "error: unrecognized flag ${1}\\n"
328327
_kubeon_usage
329328
return
330329
fi
330+
331+
KUBE_PS1_ENABLED=on
331332
}
332333

333334
kubeoff() {
334-
if [[ "$#" -eq 0 ]]; then
335-
KUBE_PS1_ENABLED=off
336-
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
335+
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
337336
_kubeoff_usage
338337
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
339-
mkdir -p "$(dirname $KUBE_PS1_DISABLE_PATH)"
340-
touch "${KUBE_PS1_DISABLE_PATH}"
341-
else
342-
echo -e "error: unrecognized flag ${1}\\n"
338+
mkdir -p -- "$(dirname "${KUBE_PS1_DISABLE_PATH}")"
339+
touch -- "${KUBE_PS1_DISABLE_PATH}"
340+
elif [[ $# -ne 0 ]]; then
341+
echo "error: unrecognized flag ${1}" >&2
343342
_kubeoff_usage
344343
return
345344
fi
346-
}
347345

348-
_kube_ps1_enabled() {
349-
if [[ "${KUBE_PS1_ENABLED}" == "on" ]]; then
350-
:
351-
elif [[ "${KUBE_PS1_ENABLED}" == "off" ]] || [[ -f "${KUBE_PS1_DISABLE_PATH}" ]]; then
352-
return 1
353-
fi
354-
return 0
346+
KUBE_PS1_ENABLED=off
355347
}
356348

357349
# Build our prompt
358350
kube_ps1() {
359-
if ! _kube_ps1_enabled; then
360-
return
361-
fi
351+
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
362352

363353
local KUBE_PS1
364354
local KUBE_PS1_RESET_COLOR="$(_kube_ps1_color_fg reset_color)"

0 commit comments

Comments
 (0)