Skip to content

Instantly share code, notes, and snippets.

@mcfedr
Created July 23, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcfedr/30fdbe3a6f9cd3f61994adc5d1e364da to your computer and use it in GitHub Desktop.
Save mcfedr/30fdbe3a6f9cd3f61994adc5d1e364da to your computer and use it in GitHub Desktop.
Bash prompt including current AWS profile, gcloud context, k8s context+namespace
# prompt
__aws_ps1() {
local printf_format="$1"
local profile="${AWS_PROFILE:-ekreative}"
if [ "$profile" != "ekreative" ]; then
printf -- "$printf_format" "${AWS_PROFILE:-ekreative}"
fi
}
__gcloud_ps1() {
local printf_format="$1"
local context=$(cat $HOME/.config/gcloud/active_config)
if [ "$context" != "" ] && [ "$context" != "ekreative" ]; then
printf -- "$printf_format" "$context"
fi
}
__k8s_ps1() {
local printf_format="$1"
local context=$(kubectl config current-context)
if [ "$context" != "" ]; then
local namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${context}\")].context.namespace}")"
if [ "$namespace" != "" ] && [ "$namespace" != "default" ]; then
context="${context}:${namespace}"
fi
printf -- "$printf_format" "$context"
fi
}
__status_ps1() {
local printf_format="$1"
if [ "$?" -eq 0 ]; then
printf -- "$printf_format" "0"
else
printf -- "$printf_format" "1"
fi
}
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
# There is a work around for iterm bug, using zero width markers (\[ and \]) for the second part of cloud \xe2\x98\x81\[\xef\xb8\x8f\]
# https://gitlab.com/gnachman/iterm2/issues/6130
PS1='${LOGNAME/mcfedr/}\h $(__aws_ps1 "\[\e[1;31m\][\xf0\x9f\x93\x95 %s]\[\e[m\]")$(__gcloud_ps1 "\[\e[1;34m\][\xf0\x9f\xa7\xa9 %s]\[\e[m\]")$(__k8s_ps1 "\[\e[1;36m\][\xf0\x9f\x9a\xa2 %s]\[\e[m\]") \[\e[1;33m\]\w\[\e[m\] $(__git_ps1 "\[\e[1;32m\][%s] \[\e[m\]")$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment