Skip to content

Commit 93def50

Browse files
kyoungerjedahan
authored andcommitted
Refactor kube
Use better approach for retreiving current namespace that doesn't rely on getting context first. Use the geometry_kube_{namespace, symbol, context, version} convention to make this function similar to the git function. Detect a valid current context in addition to a non-zero KUBECONFIG. Add GEOMETRY_KUBE_VERSION_COLOR. Remove GEOMETRY_KUBE_VERSION check, as you can remove this element now with by intentionally excluding geometry_kube_version from your prompt. Added some different logic to pinning. If you have have GEOMETRY_KUBE_PIN unset, then it defaults to false and displays prompt extras only if valid KUBECONFIG or kubectl get current-context is valid. If explictly set to false, then it will not display at all. If set to true, then it displays all the time regardless of a valid context.
1 parent c83e445 commit 93def50

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

functions/geometry_kube.zsh

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
# geometry_kube - show kubectl client version and current context/namespace.
2+
geometry_kube_symbol() {
3+
ansi ${GEOMETRY_KUBE_COLOR:=blue} ${GEOMETRY_KUBE_SYMBOL:=""}
4+
}
25

3-
geometry_kube() {
4-
(( $+commands[kubectl] )) || return
5-
( ${GEOMETRY_KUBE_PIN:=false} ) || [[ -n "$KUBECONFIG" ]] || return
6-
: ${GEOMETRY_KUBE_SEPARATOR:="|"}
6+
geometry_kube_namespace() {
7+
local kube_namespace=$(kubectl config view --minify --output "jsonpath={..namespace}" 2> /dev/null)
8+
ansi ${GEOMETRY_KUBE_NAMESPACE_COLOR:=default} ${kube_namespace:=default}
9+
}
710

8-
# Variable declaration
9-
local -a geometry_kube
10-
local kube_symbol
11-
local kube_context
12-
local kube_namespace
13-
local kube_version
11+
geometry_kube_context() {
12+
local kube_context="$(kubectl config current-context 2> /dev/null)"
13+
ansi ${GEOMETRY_KUBE_CONTEXT_COLOR:=default} ${kube_context}
14+
}
1415

15-
kube_symbol=$(ansi ${GEOMETRY_KUBE_COLOR:=blue} ${GEOMETRY_KUBE_SYMBOL:=""})
16+
geometry_kube_version() {
17+
[[ $(kubectl version --client --short) =~ 'Client Version: ([0-9a-zA-Z.]+)' ]]
18+
local kube_version=($match[1])
19+
ansi ${GEOMETRY_KUBE_VERSION_COLOR:=default} ${kube_version:=default}
20+
}
1621

17-
kube_context="$(kubectl config current-context 2> /dev/null)"
18-
kube_context=$(ansi ${GEOMETRY_KUBE_CONTEXT_COLOR:=default} ${kube_context})
22+
geometry_kube() {
23+
(( $+commands[kubectl] )) || return
1924

20-
kube_namespace=$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"${kube_context}\")].context.namespace}" 2> /dev/null)
21-
kube_namespace=$(ansi ${GEOMETRY_KUBE_NAMESPACE_COLOR:=default} ${kube_namespace:=default})
25+
( ${GEOMETRY_KUBE_PIN:=true} ) || return
2226

23-
if ( ${GEOMETRY_KUBE_VERSION:=true} ); then
24-
[[ $(kubectl version --client --short) =~ 'Client Version: ([0-9a-zA-Z.]+)' ]]
25-
kube_version=($match[1])
26-
fi
27+
( ${GEOMETRY_KUBE_PIN:=false} ) || [[ -n "$KUBECONFIG" ]] || [[ -n "$(kubectl config current-context 2> /dev/null)" ]] || return
2728

28-
geometry_kube=($kube_symbol $kube_context $kube_namespace $kube_version)
29+
local geometry_kube_details && geometry_kube_details=(
30+
$(geometry_kube_symbol)
31+
$(geometry_kube_context)
32+
$(geometry_kube_namespace)
33+
$(geometry_kube_version)
34+
)
2935

30-
echo -n ${(pj.$GEOMETRY_KUBE_SEPARATOR.)geometry_kube}
36+
local separator=${GEOMETRY_KUBE_SEPARATOR:-"|"}
37+
echo -n ${(pj.$separator.)geometry_kube_details}
3138
}

0 commit comments

Comments
 (0)