Skip to content

Instantly share code, notes, and snippets.

@omkensey
Created April 12, 2018 21:47
Show Gist options
  • Save omkensey/6034b871814dcb36d569fa9312ba634f to your computer and use it in GitHub Desktop.
Save omkensey/6034b871814dcb36d569fa9312ba634f to your computer and use it in GitHub Desktop.
Snippet to add k8s and git context to bash prompt when relevant
OLDPROMPT="$PS1"
prompt_set() {
if [ "$KUBECONFIG" != "" ]; then
PROMPT_KUBECONTEXT="k8s:$(kubectl config get-contexts --no-headers $(kubectl config current-context 2>/dev/null) 2>/dev/null | awk '{print substr($2,1,13)"..."substr($2,length($2)-19)}')\n"
fi
GIT_BRANCH=$(git branch 2> /dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/')
PROMPT_BRANCH=$(echo "$OLDPROMPT" | sed -e 's/\\u/\\u'${GIT_BRANCH}'/')
PS1="$PROMPT_KUBECONTEXT""$PROMPT_BRANCH"
}
PROMPT_COMMAND='prompt_set'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment