Skip to content

Commit

Permalink
Clean up kubeon and off to match #37
Browse files Browse the repository at this point in the history
Some small function/conditional cleanup
  • Loading branch information
jonmosco committed Feb 23, 2018
1 parent 68bf82f commit 550aa60
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
### kube-ps1 project CHANGELOG

### BUG FIXES:

* kubeon and kubeoff: Fix state on already running shells [GH-37]
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Inspired by several tools used to simplify usage of `kubectl`.
1. Clone this repository
2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`

This project is now available in brew ports!

```
$ brew update
$ brew install kube-ps1
```

For Zsh:
```sh
source /path/to/kube-ps1.sh
Expand Down Expand Up @@ -138,32 +145,21 @@ Blue was used for the default symbol to match the Kubernetes color as closely
as possible. Red was chosen as the cluster name to stand out, and cyan for the
namespace.

Set the variable to a null string if you do not want color for each
Set the variable to an empty string if you do not want color for each
prompt section:

```
KUBE_PS1_CTX_COLOR=""
KUBE_PS1_CTX_COLOR=''
```

Names are usable for the following colors:
`black, red, green, yellow, blue, magenta, cyan, and white`.

The appropriate color codes and escape sequences must be used in place of the
color names. The default colors assigned have the following escape ANSI
escape sequences and tput capabilities depending on your terminal capabilities:

```
tput:
blue: $(tput setaf 33)
red: $(tput setaf 1)
cyan: $(tput setaf 37)
ANSI:
blue: \e[34m
red: \e[31m
cyan: \e[36m
black, red, green, yellow, blue, magenta, cyan
```

256 colors are available by specifying the numerical value.

### Bug Reports and shell configuration

Due to the vast ways of customizing the shell, please try the prompt with a
Expand Down
60 changes: 25 additions & 35 deletions kube-ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ elif [ "${BASH_VERSION-}" ]; then
KUBE_PS1_SHELL="bash"
fi

_kube_ps1_shell_settings() {
_kube_ps1_init() {
[[ -f "${KUBE_PS1_DISABLE_PATH}" ]] && KUBE_PS1_ENABLED=off

case "${KUBE_PS1_SHELL}" in
"zsh")
setopt PROMPT_SUBST
Expand Down Expand Up @@ -186,7 +188,7 @@ _kube_ps1_symbol() {
# [[ "$LC_CTYPE $LC_ALL" =~ "UTF" && $TERM != "linux" ]]
# Bash only supports \u \U since 4.2
if [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then
if ((BASH_VERSINFO[0] >= 4)); then
if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388 ' != "\\u2388 " ]]; then
_KUBE_PS1_SYMBOL_DEFAULT="${KUBE_PS1_SYMBOL_DEFAULT}"
_KUBE_PS1_SYMBOL_IMG=$'\u2638 '
else
Expand Down Expand Up @@ -222,37 +224,36 @@ _kube_ps1_file_newer_than() {

if [[ "${KUBE_PS1_SHELL}" == "zsh" ]]; then
mtime=$(stat +mtime "${file}")
elif [ x"$KUBE_PS1_UNAME" = x"Linux" ]; then
elif [[ "$KUBE_PS1_UNAME" == "Linux" ]]; then
mtime=$(stat -c %Y "${file}")
else
mtime=$(stat -f %m "$file")
fi

[ "${mtime}" -gt "${check_time}" ]
[[ "${mtime}" -gt "${check_time}" ]]
}

_kube_ps1_update_cache() {
local conf

if ! _kube_ps1_enabled; then
return
fi
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return

if ! _kube_ps1_binary_check "${KUBE_PS1_BINARY}"; then
# No ability to fetch context/namespace; display N/A.
KUBE_PS1_CONTEXT="BINARY-N/A"
KUBE_PS1_NAMESPACE="N/A"
return
fi

if [[ "${KUBECONFIG}" != "${KUBE_PS1_KUBECONFIG_CACHE}" ]]; then
# User changed KUBECONFIG; unconditionally refetch.
KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
_kube_ps1_get_context_ns
return
fi

# kubectl will read the environment variable $KUBECONFIG
# otherwise set it to ~/.kube/config
for conf in $(_kube_ps1_split : "${KUBECONFIG:-$HOME/.kube/config}"); do
local conf
for conf in $(_kube_ps1_split : "${KUBECONFIG:-${HOME}/.kube/config}"); do
[[ -r "${conf}" ]] || continue
if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
_kube_ps1_get_context_ns
Expand Down Expand Up @@ -287,8 +288,8 @@ _kube_ps1_get_context_ns() {
fi
}

# Set shell options
_kube_ps1_shell_settings
# Set kube-ps1 shell defaults
_kube_ps1_init

_kubeon_usage() {
cat <<"EOF"
Expand Down Expand Up @@ -317,48 +318,37 @@ EOF
}

kubeon() {
if [[ "$#" -eq 0 ]]; then
KUBE_PS1_ENABLED=on
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_kubeon_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
rm -f "${KUBE_PS1_DISABLE_PATH}"
else
rm -f -- "${KUBE_PS1_DISABLE_PATH}"
elif [[ "$#" -ne 0 ]]; then
echo -e "error: unrecognized flag ${1}\\n"
_kubeon_usage
return
fi

KUBE_PS1_ENABLED=on
}

kubeoff() {
if [[ "$#" -eq 0 ]]; then
KUBE_PS1_ENABLED=off
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_kubeoff_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
mkdir -p "$(dirname $KUBE_PS1_DISABLE_PATH)"
touch "${KUBE_PS1_DISABLE_PATH}"
else
echo -e "error: unrecognized flag ${1}\\n"
mkdir -p -- "$(dirname "${KUBE_PS1_DISABLE_PATH}")"
touch -- "${KUBE_PS1_DISABLE_PATH}"
elif [[ $# -ne 0 ]]; then
echo "error: unrecognized flag ${1}" >&2
_kubeoff_usage
return
fi
}

_kube_ps1_enabled() {
if [[ "${KUBE_PS1_ENABLED}" == "on" ]]; then
:
elif [[ "${KUBE_PS1_ENABLED}" == "off" ]] || [[ -f "${KUBE_PS1_DISABLE_PATH}" ]]; then
return 1
fi
return 0
KUBE_PS1_ENABLED=off
}

# Build our prompt
kube_ps1() {
if ! _kube_ps1_enabled; then
return
fi
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return

local KUBE_PS1
local KUBE_PS1_RESET_COLOR="$(_kube_ps1_color_fg reset_color)"
Expand Down

0 comments on commit 550aa60

Please sign in to comment.