Skip to content

Commit

Permalink
Update prompt_kubecontext with config check
Browse files Browse the repository at this point in the history
Since I have many kubernetes config files and switch contexts
frequently.  Rather than always display the kubecontext segment, check
for a KUBECONFIG environment variable or a default $HOME/.kube/config to
enable segment.

Also modified the final text to skip namespace if it appears anywhere in
context name (via grep)
  • Loading branch information
porcupie committed Jun 27, 2018
1 parent 0e3c192 commit 769dbf9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions powerlevel9k.zsh-theme
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,9 @@ prompt_kubecontext() {
local kubectl_version="$(kubectl version --client 2>/dev/null)"

if [[ -n "$kubectl_version" ]]; then
# only show if in a kube context or has default config file
if [[ -z "$KUBECONFIG" && ! -r "$HOME/.kube/config" ]] && return

# Get the current Kubernetes config context's namespaece
local k8s_namespace=$(kubectl config get-contexts --no-headers | grep '*' | awk '{print $5}')
# Get the current Kuberenetes context
Expand All @@ -1429,14 +1432,14 @@ prompt_kubecontext() {

local k8s_final_text=""

if [[ "$k8s_context" == "k8s_namespace" ]]; then
# NOTE: also context name sometimes has namespace embedded ... check with grep
if [[ `echo "$k8s_context" | grep "$k8s_namespace" 2>1 >/dev/null` ]]; then
# No reason to print out the same identificator twice
k8s_final_text="$k8s_context"
else
k8s_final_text="$k8s_context/$k8s_namespace"
fi


"$1_prompt_segment" "$0" "$2" "magenta" "white" "$k8s_final_text" "KUBERNETES_ICON"
fi
}
Expand Down

0 comments on commit 769dbf9

Please sign in to comment.