Last active
August 31, 2018 00:58
-
-
Save nathanph/452aaed78b7e8f4165037cffe96de7aa to your computer and use it in GitHub Desktop.
Ultra fast kubectl context parsing for your PS1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim:ft=zsh ts=2 sw=2 sts=2 | |
rvm_current() { | |
rvm current 2>/dev/null | |
} | |
rbenv_version() { | |
rbenv version 2>/dev/null | awk '{print $1}' | |
} | |
# Prints information about the current context. | |
# | |
# Returns: | |
# name <context_name> | |
# cluster <context_cluster> | |
# namespace <context_namespace> | |
function kubectl_context () { | |
# Exit if KUBECONFIG isn't set. | |
[[ -z "$KUBECONFIG" ]] && return 1 | |
# Get our kubeconfigs. | |
kubeconfigs=(${(ps_:_)KUBECONFIG}) | |
# Get our primary kubeconfig. | |
kubeconfig=$kubeconfigs[1] | |
# Grab our current context. | |
context=$(grep current-context $kubeconfig | cut -f 2 -d ':' | tr -d '[:space:]' 2> /dev/null) | |
# Exit if current-context isn't set. | |
[[ -z "$context" ]] && return 1 | |
# kubeconfig contents. | |
kubeconfig_contents=$(cat $kubeconfigs) | |
# Grab content around `contexts:`. | |
kubeconfig_contexts=$(echo $kubeconfig_contents | sed -n -e '/^contexts:/,/^[a-z]*:/p') | |
# Remove non-context contents entries. | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | sed -e '/^[a-z-]*:/d') | |
# Remove newlines. | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | tr '\n' ' ') | |
# Split contexts onto separate lines (unique character hack for newline on OSX). | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | sed -e 's/- /,/g' | tr ',' '\n') | |
# Eliminate extra whitespace. | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | sed -E -e 's/[[:space:]]+/ /g') | |
# Grab the current context information. | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | grep -E "name: $context") | |
# Expand the current context information onto multiple lines (OSX hack again). | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | sed -E 's/([a-z-]*):/,\1/g' | tr ',' '\n' ) | |
# Remove quotes and empty strings. | |
kubeconfig_contexts=$(echo $kubeconfig_contexts | sed -E 's/"//g') | |
# Store the results. | |
context_info=$kubeconfig_contexts | |
# Current namespace. | |
namespace=$(echo $context_info | grep -E 'namespace ' | awk '{print $2}') | |
# Current cluster. | |
cluster=$(echo $context_info | grep -E 'cluster ' | awk '{print $2}') | |
# Return values. | |
echo "name $context" | |
echo "namespace $namespace" | |
echo "cluster $cluster" | |
} | |
function kubectl_prompt () { | |
# Grab our kubectl data. | |
kubectl_context=$(kubectl_context) | |
# Grab our context name. | |
context=$(echo "$kubectl_context" | grep -e 'name' | awk '{print $2}') | |
# Grab our cluster name. | |
cluster=$(echo "$kubectl_context" | grep -e 'cluster' | awk '{print $2}') | |
# Grab our namespace. | |
namespace=$(echo "$kubectl_context" | grep -e 'namespace' | awk '{print $2}') | |
# Style our data. | |
prefix="⛵" | |
color_delimiter="%{$fg_bold[yellow]%} + %{$reset_color%}" | |
[[ ! -z "$context" ]] && color_context="%{$fg_bold[blue]%}$context%{$reset_color%}" | |
[[ ! -z "$cluster" ]] && color_cluster="%{$fg_bold[blue]%}$cluster%{$reset_color%}" | |
[[ ! -z "$namespace" ]] && color_namespace="%{$fg_bold[blue]%}$namespace%{$reset_color%}" || color_namespace="%{$fg_bold[blue]%}default%{$reset_color%}" | |
# Print the data we have available. | |
[[ ! -z "$cluster$namespace" ]] && echo -n " $prefix" | |
# [[ ! -z "$context" ]] && echo -n " $color_context" | |
# [[ ! -z "$cluster" ]] && echo -n " $color_delimiter" | |
[[ ! -z "$cluster" ]] && echo -n " $color_cluster" | |
[[ ! -z "$cluster" ]] && echo -n "$color_delimiter" | |
[[ ! -z "$cluster" ]] && echo -n "$color_namespace" | |
[[ ! -z "$cluster$namespace" ]] && echo -n "" | |
} | |
PROMPT=' | |
%{$fg_bold[green]%}${PWD/#$HOME/~}%{$reset_color%}$(git_prompt_info)$(kubectl_prompt) $(emoji-clock) %{$fg_bold[magenta]%}%*%{$reset_color%} | |
%{$fg_bold[white]%}λ%{$reset_color%} ' | |
# Must use Powerline font, for \uE0A0 to render. | |
ZSH_THEME_GIT_PROMPT_PREFIX=" on %{$fg[magenta]%}\uE0A0 " | |
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}" | |
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}!" | |
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?" | |
ZSH_THEME_GIT_PROMPT_CLEAN="" | |
if [ -e ~/.rvm/bin/rvm-prompt ]; then | |
RPROMPT='%{$fg_bold[red]%}‹$(rvm_current)›%{$reset_color%}' | |
else | |
if which rbenv &> /dev/null; then | |
RPROMPT='%{$fg_bold[red]%}$(rbenv_version)%{$reset_color%}' | |
fi | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment