Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build prompt when necessary #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 44 additions & 35 deletions kube-ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
_KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
_KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
_KUBE_PS1_LAST_TIME=0
_KUBE_PS1=""

# Determine our shell
_kube_ps1_shell_type() {
Expand Down Expand Up @@ -153,7 +154,7 @@ _kube_ps1_symbol() {

local symbol=""
local symbol_default=$'\u2388'
local symbol_img="☸️"
local symbol_img="☸️"
local k8s_glyph=$'\Uf10fe'
local k8s_symbol_color=blue
local oc_glyph=$'\ue7b7'
Expand Down Expand Up @@ -233,13 +234,15 @@ _kube_ps1_prompt_update() {
# No ability to fetch context/namespace; display N/A.
KUBE_PS1_CONTEXT="BINARY-N/A"
KUBE_PS1_NAMESPACE="N/A"
_build_ps1_prompt
return $return_code
fi

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

Expand All @@ -253,12 +256,14 @@ _kube_ps1_prompt_update() {
config_file_cache+=":${conf}"
if _kube_ps1_file_newer_than "${conf}" "${_KUBE_PS1_LAST_TIME}"; then
_kube_ps1_get_context_ns
_build_ps1_prompt
return $return_code
fi
done

if [[ "${config_file_cache}" != "${_KUBE_PS1_CFGFILES_READ_CACHE}" ]]; then
_kube_ps1_get_context_ns
_build_ps1_prompt
return $return_code
fi

Expand Down Expand Up @@ -343,40 +348,8 @@ With no arguments, turn off kube-ps1 status for this shell instance (default).
EOF
}

kubeon() {
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_kubeon_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
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 [[ "${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}"
elif [[ $# -ne 0 ]]; then
echo "error: unrecognized flag ${1}" >&2
_kubeoff_usage
return
fi

KUBE_PS1_ENABLED=off
}

# Build our prompt
kube_ps1() {
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
[[ -z "${KUBE_PS1_CONTEXT}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]] && return

_build_ps1_prompt() {
local KUBE_PS1
local KUBE_PS1_RESET_COLOR="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_FG}${_KUBE_PS1_CLOSE_ESC}"

Expand Down Expand Up @@ -420,5 +393,41 @@ kube_ps1() {
# Close Background color if defined
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="${_KUBE_PS1_OPEN_ESC}${_KUBE_PS1_DEFAULT_BG}${_KUBE_PS1_CLOSE_ESC}"

echo "${KUBE_PS1}"
_KUBE_PS1="${KUBE_PS1}"
}

kubeon() {
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
_kubeon_usage
elif [[ "${1}" == '-g' || "${1}" == '--global' ]]; then
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 [[ "${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}"
elif [[ $# -ne 0 ]]; then
echo "error: unrecognized flag ${1}" >&2
_kubeoff_usage
return
fi

KUBE_PS1_ENABLED=off
}

kube_ps1() {
[[ "${KUBE_PS1_ENABLED}" == "off" ]] && return
[[ -z "${KUBE_PS1_CONTEXT}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]] && return

echo "${_KUBE_PS1}"
}