#!/usr/bin/env bash

# kubernetes_segment

# -----------------------------------------------------------------------------
# append to prompt: default kubernetes context/namespace from $KUBECONFIG
# arg: $1 background color
# arg: $2 foreground color
function kubernetes_segment {
    # check if the 'kubectl' command is available
    if kubectl_loc="$(type -p "kubectl")" || [[ -n $kubectl_loc ]]; then
        local context=$(kubectl config current-context 2>/dev/null)
        if [ -n "$context" ]; then
            local ns=$( kubectl config view -o jsonpath="{.contexts[?(@.name == \"${context}\")].context.namespace}" 2>/dev/null )
            if [ -n "$ns" ]; then
                local bg_color=$1                  # Set the background color
                local fg_color=$2                  # Set the foregropund color
                local kubesymbol=$'\xE2\x8E\x88'
                local content="${kubesymbol} ${context}:${ns}"
                PS1+=$(segment_end "$bg_color")
                PS1+=$(segment_content "$fg_color" "$bg_color" " $content ")
                __last_color=$bg_color
            fi
        fi
    fi
}

