Created
June 22, 2019 23:08
-
-
Save Aracki/1f97ef2f09912acd4d6fdebf2a2c27ff to your computer and use it in GitHub Desktop.
my own __kube_prompt.fish
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired from: | |
# https://github.com/jonmosco/kube-ps1 | |
# https://github.com/Ladicle/fish-kubectl-prompt | |
set -g color_ctx (set_color $KUBE_PROMPT_COLOR_CTX) | |
set -g color_ns (set_color $KUBE_PROMPT_COLOR_NS) | |
set -g color_staging (set_color blue) | |
set -g color_production (set_color red) | |
function __kube_ps_update_cache | |
function __kube_ps_cache_context | |
set -l ctx (kubectl config current-context 2>/dev/null) | |
if /bin/test $status -eq 0 | |
if [ $ctx = "production" ] | |
set -g __kube_ps_context "$color_production$ctx" | |
else if [ $ctx = "staging" ] | |
set -g __kube_ps_context "$color_staging$ctx" | |
else | |
set -g __kube_ps_context "$color_ctx$ctx" | |
end | |
else | |
set -g __kube_ps_context "n/a" | |
end | |
end | |
function __kube_ps_cache_namespace | |
set -l ns (kubectl config view --minify -o 'jsonpath={..namespace}' 2>/dev/null) | |
if /bin/test -n "$ns" | |
set -g __kube_ps_namespace "$color_ns$ns" | |
else | |
set -g __kube_ps_namespace "default" | |
end | |
end | |
set -l kubeconfig "$KUBECONFIG" | |
if /bin/test -z "$kubeconfig" | |
set kubeconfig "$HOME/.kube/config" | |
end | |
if /bin/test "$kubeconfig" != "$__kube_ps_kubeconfig" | |
__kube_ps_cache_context | |
__kube_ps_cache_namespace | |
set -g __kube_ps_kubeconfig "$kubeconfig" | |
set -g __kube_ps_timestamp (date +%s) | |
return | |
end | |
for conf in (string split ':' "$kubeconfig") | |
if /bin/test -r "$conf" | |
if /bin/test -z "$__kube_ps_timestamp"; or /bin/test (/usr/bin/stat -f '%m' "$conf") -gt "$__kube_ps_timestamp" | |
__kube_ps_cache_context | |
__kube_ps_cache_namespace | |
set -g __kube_ps_kubeconfig "$kubeconfig" | |
set -g __kube_ps_timestamp (date +%s) | |
return | |
end | |
end | |
end | |
end | |
function __kube_prompt | |
if /bin/test -z "$__kube_ps_enabled"; or /bin/test $__kube_ps_enabled -ne 1 | |
return | |
end | |
__kube_ps_update_cache | |
echo -n -s " (⎈ $__kube_ps_context|$__kube_ps_namespace)" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good for NS -> #c79e85