Created
April 12, 2018 21:47
-
-
Save omkensey/6034b871814dcb36d569fa9312ba634f to your computer and use it in GitHub Desktop.
Snippet to add k8s and git context to bash prompt when relevant
This file contains hidden or 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
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