Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mattskinosix authored Oct 20, 2024
1 parent 67c2b9c commit c096422
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
64 changes: 64 additions & 0 deletions fish/functions/__kube_prompt.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Inspired from:
# https://github.com/jonmosco/kube-ps1
# https://github.com/Ladicle/fish-kubectl-prompt

function __kube_ps_update_cache
function __kube_ps_cache_context
set -l ctx (kubectl config current-context 2>/dev/null)
if test $status -eq 0
set -g __kube_ps_context "$ctx"
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 test -n "$ns"
set -g __kube_ps_namespace "$ns"
else
set -g __kube_ps_namespace "default"
end
end

function __stat_mtime
#-- cross-platform workaround; POSIX didn't specify stat(1) and so
#-- its interface is incompatibly different on Mac OS and Linux.
#-- see https://unix.stackexchange.com/q/561927/3097
python -c "print(__import__('os').stat(__import__('sys').argv[1]).st_mtime)" $argv
end

set -l kubeconfig "$KUBECONFIG"
if test -z "$kubeconfig"
set kubeconfig "$HOME/.kube/config"
end

if 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 test -r "$conf"
if test -z "$__kube_ps_timestamp"; or test (__stat_mtime "$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 test -z "$__kube_ps_enabled"; or test $__kube_ps_enabled -ne 1
return
end

__kube_ps_update_cache
echo -n -s " (⎈ $__kube_ps_context|$__kube_ps_namespace)"
end
19 changes: 19 additions & 0 deletions fish/functions/kube_ps.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function kube_ps -a toggle
if test "$toggle" = "on"
set -U __kube_ps_enabled 1
return
end

if test "$toggle" = "off"
set -U __kube_ps_enabled 0
return
end
end

function dcleanup
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
return
end


0 comments on commit c096422

Please sign in to comment.