Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NORMAL="\[\033[0m\]"
RED="\[\033[0;31m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
YELLOW="\[\033[0;33m\]"
# Swith K8S config
function kx() {
if [[ ! -f ~/.kube/$1 ]]; then
echo "Configuration file not found ~/.kube/$1"
else
KUBECONFIG=~/.kube/$1 kubectl config view --flatten > ~/.kube/config
kubectl cluster-info | awk 'NR==1'
export_ps1
fi
}
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
function kube_ps1() {
CONTEXT=$(cat ~/.kube/config | grep "server:" | awk -F: '{print $3}')
if [ -n "$CONTEXT" ]; then
echo "{K8S${CONTEXT}}"
fi
}
function export_ps1() {
PS_K8S=$(kube_ps1 2> /dev/null)
PS_GIT="$NORMAL\$(parse_git_branch 2> /dev/null)"
if [[ ! -z $PS_K8S ]]; then
if [[ $PS_K8S == *"cloud"* ]];then
PS_K8S="${RED}${PS_K8S}"
elif [[ $PS_K8S == *"stag"* ]];then
PS_K8S="${YELLOW}${PS_K8S}"
else
PS_K8S="${GREEN}${PS_K8S}"
fi
fi
export PS1="$GREEN\u@\H $PS_K8S\n${BLUE}:\w ${NORMAL}${PS_GIT}\\$ "
}
export_ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.