Skip to content

Instantly share code, notes, and snippets.

@etoews
Created March 30, 2017 16:02
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 etoews/e5a51e54ec7355a2d6716bfbf6c08fe2 to your computer and use it in GitHub Desktop.
Save etoews/e5a51e54ec7355a2d6716bfbf6c08fe2 to your computer and use it in GitHub Desktop.
A PS1 with Docker context and Kubernetes context (including namespace)
PS1='(k:$(kubernetes-context) | d:$(docker-context "%s") | g:$(__git_ps1 "%s"))\n\W $ '
function docker-context()
{
if [ -z "$DOCKER_HOST" ]; then
printf "localhost"
else
local HOSTNAME_REGEX=".*//(.*):.*"
[[ $DOCKER_HOST =~ $HOSTNAME_REGEX ]]
printf ${BASH_REMATCH[1]}
fi
}
function kubernetes-context()
{
local KUBERNETES_CONTEXT=$(kubectl config current-context)
local HAS_NAMESPACE=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"$KUBERNETES_CONTEXT"'")].context.namespace}')
local NAMESPACE=${HAS_NAMESPACE:-default}
printf $KUBERNETES_CONTEXT[$NAMESPACE]
}
# __git_ps1 is just the standard __git_ps1 from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment