diff --git a/README.md b/README.md index 629483d..42c44ee 100644 --- a/README.md +++ b/README.md @@ -193,9 +193,15 @@ NOTE: You do not need to specify *end* segment - it will be added automatically. |`BULLETTRAIN_KCTX_BG`|`yellow`|Background color |`BULLETTRAIN_KCTX_FG`|`white`|Foreground color |`BULLETTRAIN_KCTX_PREFIX`|`⎈`|[Kubernetes](https://unicode-table.com/de/2388/) prefix of the segment -|`BULLETTRAIN_KCTX_KCONFIG`|``|Location of kube config file (e.g. /Users/Hugo/.kube/config) +|`BULLETTRAIN_KCTX_KUBECTL`|`true`|If `false` disable `kubectl` usage +|`BULLETTRAIN_KCTX_NAMESPACE`|`true`|If `false` will not show the default namespace. Namespace is only visible if `kubectl` is installed +|`BULLETTRAIN_KCTX_KCONFIG`|`${HOME}/.kube/config`|Location of kube config file (e.g. /Users/Hugo/.kube/config) + +The prompt will first check if `BULLETTRAIN_KCTX_KUBECTL=true` and `kubectl` is installed than it will use `kubectl config view --minify` to determine the context and default namespace in use. +If `BULLETTRAIN_KCTX_KUBECTL=false` or `kubectl` is not installed, `BULLETTRAIN_KCTX_KCONFIG` will be parsed to get the current context. + +The usage of `kubectl` allow the prompt to get the default namespace even if you are using multiple kube config files (e.g. KUBECONFIG=~/.kube/config:path-to-config1:path-to-config2) -`BULLETTRAIN_KCTX_KCONFIG` must be set, e.g. in .zshrc. There can be no default value and `~/` can not be reliably interpreted. The prompt will also do a sanity check whether `kubectl` is installed. If either condition fails, the prompt segment will not be drawn at all. ### AWS Profile diff --git a/bullet-train.zsh-theme b/bullet-train.zsh-theme index c31cc96..77f34c2 100644 --- a/bullet-train.zsh-theme +++ b/bullet-train.zsh-theme @@ -161,6 +161,15 @@ fi if [ ! -n "${BULLETTRAIN_KCTX_PREFIX+1}" ]; then BULLETTRAIN_KCTX_PREFIX="⎈" fi +if [ ! -n "${BULLETTRAIN_KCTX_KCONFIG+1}" ]; then + BULLETTRAIN_KCTX_KCONFIG="${HOME}/.kube/config" +fi +if [ ! -n "${BULLETTRAIN_KCTX_KUBECTL+1}" ]; then + BULLETTRAIN_KCTX_KUBECTL="true" +fi +if [ ! -n "${BULLETTRAIN_KCTX_NAMESPACE+1}" ]; then + BULLETTRAIN_KCTX_NAMESPACE="true" +fi # ELIXIR if [ ! -n "${BULLETTRAIN_ELIXIR_BG+1}" ]; then @@ -552,13 +561,14 @@ prompt_rust() { # Kubernetes Context prompt_kctx() { - if [[ ! -n $BULLETTRAIN_KCTX_KCONFIG ]]; then - return - fi - if command -v kubectl > /dev/null 2>&1; then - if [[ -f $BULLETTRAIN_KCTX_KCONFIG ]]; then - prompt_segment $BULLETTRAIN_KCTX_BG $BULLETTRAIN_KCTX_FG $BULLETTRAIN_KCTX_PREFIX" $(cat $BULLETTRAIN_KCTX_KCONFIG|grep current-context| awk '{print $2}')" - fi + if [[ "$BULLETTRAIN_KCTX_KUBECTL" == "true" ]] && command -v kubectl > /dev/null 2>&1; then + local jsonpath='{.current-context}' + if [[ "$BULLETTRAIN_KCTX_NAMESPACE" == "true" ]]; then + jsonpath="${jsonpath}{':'}{..namespace}" + fi + prompt_segment $BULLETTRAIN_KCTX_BG $BULLETTRAIN_KCTX_FG $BULLETTRAIN_KCTX_PREFIX" $(kubectl config view --minify --output "jsonpath=${jsonpath}" 2>/dev/null)" + elif [[ -f $BULLETTRAIN_KCTX_KCONFIG ]]; then + prompt_segment $BULLETTRAIN_KCTX_BG $BULLETTRAIN_KCTX_FG $BULLETTRAIN_KCTX_PREFIX" $(cat $BULLETTRAIN_KCTX_KCONFIG | grep current-context | awk '{print $2}')" fi }