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

Improve startup time drasticaly by caching to disk #109

Open
wants to merge 1 commit 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
18 changes: 18 additions & 0 deletions kube-ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
KUBE_PS1_LAST_TIME=0
KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}"
KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"
KUBE_PS1_ENV_CACHEFILE="${HOME}/.kube-ps1.envs"

# Determine our shell
if [ "${ZSH_VERSION-}" ]; then
Expand All @@ -63,6 +64,7 @@ _kube_ps1_init() {
add-zsh-hook precmd _kube_ps1_update_cache
zmodload -F zsh/stat b:zstat
zmodload zsh/datetime
_kube_load_vars_from_file
;;
"bash")
_KUBE_PS1_OPEN_ESC=$'\001'
Expand All @@ -74,6 +76,20 @@ _kube_ps1_init() {
esac
}

_kube_load_vars_from_file() {
if test -f "$KUBE_PS1_ENV_CACHEFILE"; then
source "$KUBE_PS1_ENV_CACHEFILE"
fi
}

_kube_ps1_update_cachefile() {
# Cache has been updated, write to file.
echo "KUBE_PS1_CONTEXT=$KUBE_PS1_CONTEXT" > $KUBE_PS1_ENV_CACHEFILE
echo "KUBE_PS1_KUBECONFIG_CACHE=$KUBE_PS1_KUBECONFIG_CACHE" >> $KUBE_PS1_ENV_CACHEFILE
echo "KUBE_PS1_LAST_TIME=$KUBE_PS1_LAST_TIME" >> $KUBE_PS1_ENV_CACHEFILE
echo "KUBE_PS1_NAMESPACE=$KUBE_PS1_NAMESPACE" >> $KUBE_PS1_ENV_CACHEFILE
}

_kube_ps1_color_fg() {
local KUBE_PS1_FG_CODE
case "${1}" in
Expand Down Expand Up @@ -212,6 +228,7 @@ _kube_ps1_update_cache() {
# User changed KUBECONFIG; unconditionally refetch.
KUBE_PS1_KUBECONFIG_CACHE=${KUBECONFIG}
_kube_ps1_get_context_ns
_kube_ps1_update_cachefile
return
fi

Expand All @@ -222,6 +239,7 @@ _kube_ps1_update_cache() {
[[ -r "${conf}" ]] || continue
if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
_kube_ps1_get_context_ns
_kube_ps1_update_cachefile
return
fi
done
Expand Down