diff --git a/README.md b/README.md index 4d905e3..edec6d3 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ kube-ps1: Kubernetes prompt for bash and zsh ============================================ -A script that lets you add the current Kubernetes context and namespace +A script that lets you add the current Kubernetes context, user and namespace configured on `kubectl` to your Bash/Zsh prompt strings (i.e. the `$PS1`). Inspired by several tools used to simplify usage of `kubectl`. @@ -134,6 +134,7 @@ the following environment variables: | :------- | :-----: | ------- | | `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary | | `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` | +| `KUBE_PS1_USER_ENABLE` | `true` | Display current user | | `KUBE_PS1_PREFIX` | `(` | Prompt opening character | | `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` | | `KUBE_PS1_SYMBOL_PADDING` | `true` | Adds a space (padding) after the symbol to prevent clobbering prompt characters | @@ -144,6 +145,7 @@ the following environment variables: | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character | | `KUBE_PS1_CLUSTER_FUNCTION` | No default, must be user supplied | Function to customize how cluster is displayed | | `KUBE_PS1_NAMESPACE_FUNCTION` | No default, must be user supplied | Function to customize how namespace is displayed | +| `KUBE_PS1_USER_FUNCTION` | No default, must be user supplied | Function to customize how user is displayed | For terminals that do not support UTF-8, the symbol will be replaced with the string `k8s`. @@ -164,6 +166,7 @@ The default colors are set with the following environment variables: | `KUBE_PS1_SYMBOL_COLOR` | `blue` | Set default color of the Kubernetes symbol | | `KUBE_PS1_CTX_COLOR` | `red` | Set default color of the context | | `KUBE_PS1_SUFFIX_COLOR` | `null` | Set default color of the prompt suffix | +| `KUBE_PS1_USR_COLOR` | `cyan` | Set default color of the user | | `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace | | `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background | diff --git a/kube-ps1.sh b/kube-ps1.sh index 69c2c4c..b39cad6 100644 --- a/kube-ps1.sh +++ b/kube-ps1.sh @@ -29,6 +29,7 @@ KUBE_PS1_SYMBOL_PADDING="${KUBE_PS1_SYMBOL_PADDING:-true}" KUBE_PS1_SYMBOL_USE_IMG="${KUBE_PS1_SYMBOL_USE_IMG:-false}" KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}" KUBE_PS1_CONTEXT_ENABLE="${KUBE_PS1_CONTEXT_ENABLE:-true}" +KUBE_PS1_USER_ENABLE="${KUBE_PS1_USER_ENABLE:-true}" KUBE_PS1_PREFIX="${KUBE_PS1_PREFIX-(}" KUBE_PS1_SEPARATOR="${KUBE_PS1_SEPARATOR-|}" KUBE_PS1_DIVIDER="${KUBE_PS1_DIVIDER-:}" @@ -37,6 +38,7 @@ KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}" KUBE_PS1_SYMBOL_COLOR="${KUBE_PS1_SYMBOL_COLOR-blue}" KUBE_PS1_CTX_COLOR="${KUBE_PS1_CTX_COLOR-red}" KUBE_PS1_NS_COLOR="${KUBE_PS1_NS_COLOR-cyan}" +KUBE_PS1_USR_COLOR="${KUBE_PS1_USR_COLOR-cyan}" KUBE_PS1_BG_COLOR="${KUBE_PS1_BG_COLOR}" KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}" @@ -44,6 +46,7 @@ KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled" KUBE_PS1_LAST_TIME=0 KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}" KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}" +KUBE_PS1_USER_FUNCTION="${KUBE_PS1_USER_FUNCTION}" # Determine our shell if [ "${ZSH_VERSION-}" ]; then @@ -260,6 +263,18 @@ _kube_ps1_get_ns() { fi } +_kube_ps1_get_user() { + if [[ "${KUBE_PS1_USER_ENABLE}" == true ]]; then + KUBE_PS1_USER="$(${KUBE_PS1_BINARY} config view -o=jsonpath="{.contexts[?(@.name==\"$(${KUBE_PS1_BINARY} config current-context)\")].context.user}" 2>/dev/null)" + + KUBE_PS1_USER="${KUBE_PS1_USER:-N/A}" + + if [[ ! -z "${KUBE_PS1_USER_FUNCTION}" ]]; then + KUBE_PS1_USER=$($KUBE_PS1_USER_FUNCTION $KUBE_PS1_USER) + fi + fi +} + _kube_ps1_get_context_ns() { # Set the command time if [[ "${KUBE_PS1_SHELL}" == "bash" ]]; then @@ -274,6 +289,7 @@ _kube_ps1_get_context_ns() { _kube_ps1_get_context _kube_ps1_get_ns + _kube_ps1_get_user } # Set kube-ps1 shell defaults @@ -364,6 +380,14 @@ kube_ps1() { KUBE_PS1+="$(_kube_ps1_color_fg $KUBE_PS1_CTX_COLOR)${KUBE_PS1_CONTEXT}${KUBE_PS1_RESET_COLOR}" fi + # User + if [[ "${KUBE_PS1_USER_ENABLE}" == true ]]; then + if [[ -n "${KUBE_PS1_DIVIDER}" ]] && [[ "${KUBE_PS1_USER_ENABLE}" == true ]]; then + KUBE_PS1+="${KUBE_PS1_DIVIDER}" + fi + KUBE_PS1+="$(_kube_ps1_color_fg ${KUBE_PS1_USR_COLOR})${KUBE_PS1_USER}${KUBE_PS1_RESET_COLOR}" + fi + # Namespace if [[ "${KUBE_PS1_NS_ENABLE}" == true ]]; then if [[ -n "${KUBE_PS1_DIVIDER}" ]] && [[ "${KUBE_PS1_CONTEXT_ENABLE}" == true ]]; then