Skip to content

Commit b2286b6

Browse files
authored
Merge pull request #194 from jonmosco/fish_port
fish port finally. Going to need lots of work. First time diving in…
2 parents 0391b23 + e766e6d commit b2286b6

File tree

1 file changed

+307
-0
lines changed

1 file changed

+307
-0
lines changed

kube-ps1.fish

+307
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
#!/usr/bin/env fish
2+
3+
# Kubernetes prompt helper for bash/zsh/fish
4+
# Displays current context and namespace
5+
6+
# Copyright 2024 Jon Mosco
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
20+
# Debug
21+
# $fish trace
22+
23+
if set -q _KUBE_PS1_BINARY
24+
set -l _KUBE_PS1_BINARY kubectl
25+
end
26+
27+
if set -q _KUBE_PS1_SYMBOL_ENABLE
28+
set _KUBE_PS1_SYMBOL_ENABLE true
29+
end
30+
31+
if set -q _KUBE_PS1_SYMBOL_PADDING
32+
set _KUBE_PS1_SYMBOL_PADDING false
33+
end
34+
35+
if set -q _KUBE_PS1_SYMBOL_USE_IMG
36+
set _KUBE_PS1_SYMBOL_USE_IMG false
37+
end
38+
39+
if set -q _KUBE_PS1_SYMBOL_OC_IMG
40+
set _KUBE_PS1_SYMBOL_OC_IMG false
41+
end
42+
43+
if set -q _KUBE_PS1_NS_ENABLE
44+
set _KUBE_PS1_NS_ENABLE true
45+
end
46+
47+
if set -q _KUBE_PS1_CONTEXT_ENABLE
48+
set _KUBE_PS1_CONTEXT_ENABLE true
49+
end
50+
51+
if test -n _KUBE_PS1_PREFIX
52+
set -g _KUBE_PS1_PREFIX "("
53+
end
54+
55+
if set -q _KUBE_PS1_SEPARATOR
56+
set -g _KUBE_PS1_SEPARATOR "|"
57+
end
58+
59+
if set -q _KUBE_PS1_DIVIDER
60+
set -g _KUBE_PS1_DIVIDER ":"
61+
end
62+
63+
if set -q _KUBE_PS1_SUFFIX
64+
set -g _KUBE_PS1_SUFFIX ")"
65+
end
66+
67+
# if set -q "$_KUBE_PS1_SYMBOL_COLOR"
68+
# set _KUBE_PS1_SYMBOL_COLOR blue
69+
# end
70+
71+
# KUBE_PS1_CTX_COLOR="${KUBE_PS1_CTX_COLOR-red}"
72+
# KUBE_PS1_NS_COLOR="${KUBE_PS1_NS_COLOR-cyan}"
73+
# KUBE_PS1_BG_COLOR="${KUBE_PS1_BG_COLOR}"
74+
75+
# set -g color_ctx (set_color $KUBE_PROMPT_COLOR_CTX)
76+
# set -g color_ns (set_color $KUBE_PROMPT_COLOR_NS)
77+
78+
# KUBE_PS1_CLUSTER_FUNCTION="${KUBE_PS1_CLUSTER_FUNCTION}"
79+
# KUBE_PS1_NAMESPACE_FUNCTION="${KUBE_PS1_NAMESPACE_FUNCTION}"
80+
81+
set -g _KUBE_PS1_DISABLE_PATH $HOME/.kube/kube-ps1/disabled
82+
set -g _KUBE_PS1_KUBECONFIG_CACHE KUBECONFIG
83+
set -g _KUBE_PS1_LAST_TIME 0
84+
85+
# function kube_ps1_color_fg
86+
# end
87+
88+
# function kube_ps1_color_bg
89+
# end
90+
91+
function kube_ps1_binary_check
92+
command -q $1
93+
end
94+
95+
## DONE ##
96+
function _kube_ps1_split_config
97+
string split ":" $argv
98+
end
99+
100+
## DONE ##
101+
function _kube_ps1_file_newer_than
102+
set -l mtime
103+
set -l file $argv[1]
104+
set -l check_time $argv[2]
105+
106+
# file modification time options from kube-ps1
107+
if stat -c "%s" /dev/null &> /dev/null
108+
# GNU stat
109+
set -l mtime (stat -L -c %Y "$file")
110+
else
111+
# BSD stat
112+
set -l mtime (stat -L -f %m "$file")
113+
end
114+
115+
[ "$mtime" -gt "$check_time" ]
116+
end
117+
118+
# TODO: Lots of work needed here
119+
function kube_ps1_prompt_update
120+
set -l return_code $status
121+
122+
[ "$KUBE_PS1_ENABLED" = "off" ] and return $return_code
123+
124+
if ! _kube_ps1_binary_check "$KUBE_PS1_BINARY"
125+
# No ability to fetch context/namespace; display N/A.
126+
set -l KUBE_PS1_CONTEXT "BINARY-N/A"
127+
set -l KUBE_PS1_NAMESPACE "N/A"
128+
return $return_code
129+
end
130+
131+
if [ "$KUBECONFIG" != "$_KUBE_PS1_KUBECONFIG_CACHE" ]
132+
# User changed KUBECONFIG; unconditionally refetch.
133+
set -l _KUBE_PS1_KUBECONFIG_CACHE $KUBECONFIG
134+
_kube_ps1_get_context_ns
135+
return $return_code
136+
end
137+
138+
local conf
139+
local config_file_cache
140+
141+
# kubectl will read the environment variable $KUBECONFIG
142+
# otherwise set it to ~/.kube/config
143+
set -l kubeconfig "$KUBECONFIG"
144+
if set -q "$kubeconfig"
145+
set kubeconfig "$HOME/.kube/config"
146+
end
147+
148+
for conf in _kube_ps1_split_config "$kubeconfig"
149+
[ -r "$conf" ] or continue
150+
set -a config_file_cache conf
151+
if _kube_ps1_file_newer_than "$conf" "$_KUBE_PS1_LAST_TIME"
152+
_kube_ps1_get_context_ns
153+
return $return_code
154+
end
155+
end
156+
157+
if [ "$config_file_cache" != "$_KUBE_PS1_CFGFILES_READ_CACHE" ]
158+
_kube_ps1_get_context_ns
159+
return $return_code
160+
end
161+
162+
return $return_code
163+
end
164+
165+
## DONE ##
166+
function _kube_ps1_get_context
167+
if [ "$KUBE_PS1_CONTEXT_ENABLE" = true ]
168+
set -g _KUBE_PS1_CONTEXT $_KUBE_PS1_BINARY config current-context 2>/dev/null
169+
170+
if not test -e "_KUBE_PS1_CONTEXT"
171+
set _KUBE_PS1_CONTEXT "N/A"
172+
end
173+
end
174+
end
175+
176+
## DONE ##
177+
function _kube_ps1_get_ns
178+
if [ "$KUBE_PS1_NS_ENABLE" = true ]
179+
set -g _KUBE_PS1_NAMESPACE $_KUBE_PS1_BINARY config view --minify --output 'jsonpath={..namespace}' 2>/dev/null
180+
181+
if test -z "_KUBE_PS1_NAMESPACE"
182+
set _KUBE_PS1_NAMESPACE "N/A"
183+
end
184+
end
185+
end
186+
187+
function kube_ps1_get_ctx_ns
188+
# Set the command time
189+
set -l _KUBE_PS1_LAST_TIME (date +%s)
190+
191+
set -q KUBE_PS1_CONTEXT; or set -l KUBE_PS1_CONTEXT "N/A"
192+
set -q KUBE_PS1_NAMESPACE; or set -l KUBE_PS1_NAMESPACE "N/A"
193+
194+
# Cache which cfgfiles we can read in case they change.
195+
set -l conf
196+
197+
set -l kubeconfig "$KUBECONFIG"
198+
if set -q "$kubeconfig"
199+
set -l kubeconfig "$HOME/.kube/config"
200+
end
201+
202+
# _KUBE_PS1_CFGFILES_READ_CACHE=
203+
for conf in _kube_ps1_split_config $kubeconfig
204+
[ -r $conf ]; and set -a conf _KUBE_PS1_CFGFILES_READ_CACHE
205+
end
206+
207+
_kube_ps1_get_context
208+
_kube_ps1_get_ns
209+
210+
set -g KUBE_PS1_CONTEXT "N/A"
211+
set -g KUBE_PS1_NAMESPACE "N/A"
212+
end
213+
214+
function _kube_ps1_symbol
215+
[ "$KUBE_PS1_SYMBOL_ENABLE" = false ]; and return
216+
217+
set -l KUBE_PS1_SYMBOL \u2638
218+
219+
if [ "$KUBE_PS1_SYMBOL_USE_IMG" = true ]
220+
set -l KUBE_PS1_SYMBOL "$KUBE_PS1_SYMBOL_IMG"
221+
end
222+
223+
# OpenShift glyph
224+
# NOTE: this requires a patched "Nerd" font to work
225+
# https://www.nerdfonts.com/
226+
if [ "$KUBE_PS1_SYMBOL_OC_IMG" = true ]
227+
set -l KUBE_PS1_SYMBOL \ue7b7
228+
end
229+
230+
if [ "$KUBE_PS1_SYMBOL_PADDING" = true ]
231+
echo "$KUBE_PS1_SYMBOL "
232+
else
233+
echo "$KUBE_PS1_SYMBOL"
234+
end
235+
end
236+
237+
## DONE ##
238+
function _kubeon_usage
239+
echo "Toggle kube-ps1 prompt on"
240+
echo
241+
echo "Usage: kubeon [-g | --global] [-h | --help]"
242+
echo
243+
echo "With no arguments, turn oon kube-ps1 status for this shell instance (default)."
244+
echo
245+
echo " -g --global turn on kube-ps1 status globally"
246+
echo " -h --help print this message"
247+
end
248+
249+
## DONE ##
250+
function _kubeoff_usage
251+
echo "Toggle kube-ps1 prompt off"
252+
echo
253+
echo "Usage: kubeoff [-g | --global] [-h | --help]"
254+
echo
255+
echo "With no arguments, turn off kube-ps1 status for this shell instance (default)."
256+
echo
257+
echo " -g --global turn off kube-ps1 status globally"
258+
echo " -h --help print this message"
259+
end
260+
261+
function kube_ps1_on
262+
argparse h/help g/global -- $argv
263+
or return
264+
265+
if set -ql _flag_help
266+
_kubeon_usage
267+
return 0
268+
end
269+
270+
if set -ql _flag_second
271+
rm -f -- "$_KUBE_PS1_DISABLE_PATH"
272+
end
273+
274+
set -g _KUBE_PS1_ENABLED on
275+
end
276+
277+
function kube_ps1_off
278+
argparse h/help g/global -- $argv
279+
or return
280+
281+
if set -ql _flag_help
282+
_kubeon_usage
283+
return 0
284+
end
285+
286+
if set -ql _flag_second
287+
mkdir -p -- "(path basename "$_KUBE_PS1_DISABLE_PATH")"
288+
touch -- "$_KUBE_PS1_DISABLE_PATH"
289+
end
290+
291+
set -g _KUBE_PS1_ENABLED off
292+
end
293+
294+
# Build our prompt
295+
function kube_ps1
296+
297+
echo "$_KUBE_PS1_PREFIX" \
298+
(_kube_ps1_symbol) \
299+
"$_KUBE_PS1_SEPARATOR" \
300+
(_kube_ps1_get_context) \
301+
"$_KUBE_PS1_DIVIDER" \
302+
(_kube_ps1_get_ns) \
303+
"$_KUBE_PS1_SUFFIX"
304+
305+
# echo (set_color blue)$KUBECTL_PROMPT_ICON" "(set_color cyan)"($context"(set_color white)"$KUBECTL_PROMPT_SEPARATOR"(set_color yellow)"$ns)"
306+
307+
end

0 commit comments

Comments
 (0)