Skip to content

Instantly share code, notes, and snippets.

@peterroth
Last active September 8, 2023 17:48
Show Gist options
  • Save peterroth/1d847d57c5748edb05061d92d6387f72 to your computer and use it in GitHub Desktop.
Save peterroth/1d847d57c5748edb05061d92d6387f72 to your computer and use it in GitHub Desktop.
what to add in .bashrc to show kubectl's current context and active git branch in shell
# Add funciton to check which context is active for kubectl
function kube_context {
CONTEXT=$(grep "current-context:" ~/.kube/config | sed "s/current-context: //")
if [ -n "$CONTEXT" ]; then
if [ "${CONTEXT}" = "\"\"" ]; then
CONTEXT=""
fi
case $CONTEXT in
*api-okd-prodcluster-com:6443*) export CONTEXT=`echo ${CONTEXT} | sed 's/\/api-okd-prodcluster-com:6443/PROD-cluster/'`;;
*api-okd-testcluster-com:6443*) export CONTEXT=`echo ${CONTEXT} | sed 's/\/api-okd-testcluster-com:6443/TEST-cluster/'`;;
esac
echo "${CONTEXT} "
fi
}
# Add funciton to parse the git branch when we enter into a repo's directory
function parse_git_branch {
git branch --show-current --no-color 2> /dev/null | sed 's/^/ /'
}
# Use yellow for kubeconfig, green for the active directory and purple for git branch
PS1='${debian_chroot:+($debian_chroot)}\[\033[93m\]$(kube_context)\[\033[01;32m\]\w\[\033[00;95m\]$(parse_git_branch)\[\033[00m\]: '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment