Skip to content

Commit

Permalink
Add a new parameter "binary" (#26)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
superbrothers authored Feb 11, 2021
1 parent 76c595c commit c30110e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Does not display the current namespace:
zstyle ':zsh-kubectl-prompt:' namespace false
```

Use another binary instead of `kubectl` to get the information (e.g. `oc`):

```sh
zstyle ':zsh-kubectl-prompt:' binary 'oc'
```

## With a plugin manager

If you use [zgen](https://github.com/tarjoilija/zgen), load this repository as follows:
Expand Down
22 changes: 17 additions & 5 deletions kubectl.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ setopt prompt_subst
autoload -U add-zsh-hook

function() {
local namespace separator modified_time_fmt
local namespace separator binary

# Specify the separator between context and namespace
zstyle -s ':zsh-kubectl-prompt:' separator separator
Expand All @@ -15,11 +15,23 @@ function() {
if [[ -z "$namespace" ]]; then
zstyle ':zsh-kubectl-prompt:' namespace true
fi

# Specify the binary to get the information from kubeconfig (e.g. `oc`)
zstyle -s ':zsh-kubectl-binary:' binary binary
if [[ -z "$binary" ]]; then
zstyle ':zsh-kubectl-prompt:' binary "kubectl"
fi
}

add-zsh-hook precmd _zsh_kubectl_prompt_precmd
function _zsh_kubectl_prompt_precmd() {
local kubeconfig config updated_at now context namespace ns separator modified_time_fmt
local kubeconfig config updated_at now context namespace ns separator modified_time_fmt binary

zstyle -s ':zsh-kubectl-prompt:' binary binary
if ! command -v "$binary" >/dev/null; then
ZSH_KUBECTL_PROMPT="${binary} command not found"
return 1
fi

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

# Set environment variable if context is not set
if ! context="$(kubectl config current-context 2>/dev/null)"; then
if ! context="$("$binary" config current-context 2>/dev/null)"; then
ZSH_KUBECTL_PROMPT="current-context is not set"
return 1
fi

ZSH_KUBECTL_USER="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
ZSH_KUBECTL_USER="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.user}")"
ZSH_KUBECTL_CONTEXT="${context}"
ns="$(kubectl config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
ns="$("$binary" config view -o "jsonpath={.contexts[?(@.name==\"$context\")].context.namespace}")"
[[ -z "$ns" ]] && ns="default"
ZSH_KUBECTL_NAMESPACE="${ns}"

Expand Down

0 comments on commit c30110e

Please sign in to comment.