Skip to content

Commit c30110e

Browse files
Add a new parameter "binary" (#26)
The binary parameter allows us to use the specified binary instead of `kubectl` to get the information from kubeconfig. For example, it allows OpenShift users to use `oc` command instead of `kubectl`.
1 parent 76c595c commit c30110e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Does not display the current namespace:
7070
zstyle ':zsh-kubectl-prompt:' namespace false
7171
```
7272

73+
Use another binary instead of `kubectl` to get the information (e.g. `oc`):
74+
75+
```sh
76+
zstyle ':zsh-kubectl-prompt:' binary 'oc'
77+
```
78+
7379
## With a plugin manager
7480

7581
If you use [zgen](https://github.com/tarjoilija/zgen), load this repository as follows:

kubectl.zsh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ setopt prompt_subst
22
autoload -U add-zsh-hook
33

44
function() {
5-
local namespace separator modified_time_fmt
5+
local namespace separator binary
66

77
# Specify the separator between context and namespace
88
zstyle -s ':zsh-kubectl-prompt:' separator separator
@@ -15,11 +15,23 @@ function() {
1515
if [[ -z "$namespace" ]]; then
1616
zstyle ':zsh-kubectl-prompt:' namespace true
1717
fi
18+
19+
# Specify the binary to get the information from kubeconfig (e.g. `oc`)
20+
zstyle -s ':zsh-kubectl-binary:' binary binary
21+
if [[ -z "$binary" ]]; then
22+
zstyle ':zsh-kubectl-prompt:' binary "kubectl"
23+
fi
1824
}
1925

2026
add-zsh-hook precmd _zsh_kubectl_prompt_precmd
2127
function _zsh_kubectl_prompt_precmd() {
22-
local kubeconfig config updated_at now context namespace ns separator modified_time_fmt
28+
local kubeconfig config updated_at now context namespace ns separator modified_time_fmt binary
29+
30+
zstyle -s ':zsh-kubectl-prompt:' binary binary
31+
if ! command -v "$binary" >/dev/null; then
32+
ZSH_KUBECTL_PROMPT="${binary} command not found"
33+
return 1
34+
fi
2335

2436
kubeconfig="$HOME/.kube/config"
2537
if [[ -n "$KUBECONFIG" ]]; then
@@ -53,14 +65,14 @@ function _zsh_kubectl_prompt_precmd() {
5365
zstyle ':zsh-kubectl-prompt:' updated_at "$now"
5466

5567
# Set environment variable if context is not set
56-
if ! context="$(kubectl config current-context 2>/dev/null)"; then
68+
if ! context="$("$binary" config current-context 2>/dev/null)"; then
5769
ZSH_KUBECTL_PROMPT="current-context is not set"
5870
return 1
5971
fi
6072

61-
ZSH_KUBECTL_USER="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
73+
ZSH_KUBECTL_USER="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
6274
ZSH_KUBECTL_CONTEXT="${context}"
63-
ns="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
75+
ns="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
6476
[[ -z "$ns" ]] && ns="default"
6577
ZSH_KUBECTL_NAMESPACE="${ns}"
6678

0 commit comments

Comments
 (0)