@@ -47,7 +47,9 @@ elif [ "${BASH_VERSION-}" ]; then
4747 KUBE_PS1_SHELL=" bash"
4848fi
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
319320kubeon () {
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
333334kubeoff () {
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
358350kube_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