Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to display user #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down Expand Up @@ -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 |
Expand All @@ -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`.
Expand All @@ -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 |

Expand Down
24 changes: 24 additions & 0 deletions kube-ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-:}"
Expand All @@ -37,13 +38,15 @@ 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}"
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down