Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Commit bbb2c4b

Browse files
ulidtkojkatagi
andauthored
Crossplatform fix for the stat_mtime issue (#11)
* fix functions path * use test * fix #5 * Add cross-platform stat_mtime via python Fixes #1 properly. Fixes #5 too. Makes both MacOS and Linux users happy. Co-authored-by: jkatagi <kemisknkn37@gmail.com>
1 parent b2acc07 commit bbb2c4b

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
mkdir -p ~/.config/fish/functions/
1111
cd ~/.config/fish/
1212
git clone https://github.com/aluxian/fish-kube-prompt
13-
ln -s ../fish-kube-prompt/__kube_prompt.fish functions/
14-
ln -s ../fish-kube-prompt/kube_ps.fish functions/
13+
ln -s ../fish-kube-prompt/functions/__kube_prompt.fish functions/
14+
ln -s ../fish-kube-prompt/functions/kube_ps.fish functions/
1515
```
1616

1717
Then create or edit `~/.config/fish/functions/fish_prompt.fish` to include

functions/__kube_prompt.fish

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function __kube_ps_update_cache
66
function __kube_ps_cache_context
77
set -l ctx (kubectl config current-context 2>/dev/null)
8-
if /bin/test $status -eq 0
8+
if test $status -eq 0
99
set -g __kube_ps_context "$ctx"
1010
else
1111
set -g __kube_ps_context "n/a"
@@ -14,19 +14,26 @@ function __kube_ps_update_cache
1414

1515
function __kube_ps_cache_namespace
1616
set -l ns (kubectl config view --minify -o 'jsonpath={..namespace}' 2>/dev/null)
17-
if /bin/test -n "$ns"
17+
if test -n "$ns"
1818
set -g __kube_ps_namespace "$ns"
1919
else
2020
set -g __kube_ps_namespace "default"
2121
end
2222
end
2323

24+
function __stat_mtime
25+
#-- cross-platform workaround; POSIX didn't specify stat(1) and so
26+
#-- its interface is incompatibly different on Mac OS and Linux.
27+
#-- see https://unix.stackexchange.com/q/561927/3097
28+
python -c "print(__import__('os').stat(__import__('sys').argv[1]).st_mtime)" $argv
29+
end
30+
2431
set -l kubeconfig "$KUBECONFIG"
25-
if /bin/test -z "$kubeconfig"
32+
if test -z "$kubeconfig"
2633
set kubeconfig "$HOME/.kube/config"
2734
end
2835

29-
if /bin/test "$kubeconfig" != "$__kube_ps_kubeconfig"
36+
if test "$kubeconfig" != "$__kube_ps_kubeconfig"
3037
__kube_ps_cache_context
3138
__kube_ps_cache_namespace
3239
set -g __kube_ps_kubeconfig "$kubeconfig"
@@ -35,8 +42,8 @@ function __kube_ps_update_cache
3542
end
3643

3744
for conf in (string split ':' "$kubeconfig")
38-
if /bin/test -r "$conf"
39-
if /bin/test -z "$__kube_ps_timestamp"; or /bin/test (/usr/bin/stat -f '%m' "$conf") -gt "$__kube_ps_timestamp"
45+
if test -r "$conf"
46+
if test -z "$__kube_ps_timestamp"; or test (__stat_mtime "$conf") -gt "$__kube_ps_timestamp"
4047
__kube_ps_cache_context
4148
__kube_ps_cache_namespace
4249
set -g __kube_ps_kubeconfig "$kubeconfig"
@@ -48,7 +55,7 @@ function __kube_ps_update_cache
4855
end
4956

5057
function __kube_prompt
51-
if /bin/test -z "$__kube_ps_enabled"; or /bin/test $__kube_ps_enabled -ne 1
58+
if test -z "$__kube_ps_enabled"; or test $__kube_ps_enabled -ne 1
5259
return
5360
end
5461

0 commit comments

Comments
 (0)