Skip to content

Commit

Permalink
Add variables for user, context and namespace (#15)
Browse files Browse the repository at this point in the history
* Add variables for user, context and namespace

* Fetching namespace only once
  • Loading branch information
jkrnak authored and superbrothers committed May 31, 2019
1 parent 414acd6 commit c97dac5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ source /path/to/zsh-kubectl-prompt/kubectl.zsh
RPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```

Or create different style depending on user, context, namespace.
The plugin creates 4 variables:
* ZSH_KUBECTL_CONTEXT
* ZSH_KUBECTL_NAMESPACE
* ZSH_KUBECTL_PROMPT
* ZSH_KUBECTL_USER

For example, make the prompt red when the username matches admin.
```sh
autoload -U colors; colors
source /path/to/zsh-kubectl-prompt/kubectl.zsh
[[ "$ZSH_KUBECTL_USER" =~ "admin" ]] && color=red || color=blue
RPROMPT='%{$fg[$color]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'
```

Also you can install with homebrew.

```console
Expand Down
9 changes: 6 additions & 3 deletions kubectl.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ function _zsh_kubectl_prompt_precmd() {
return 1
fi

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

zstyle -s ':zsh-kubectl-prompt:' namespace namespace
if [[ "$namespace" != true ]]; then
ZSH_KUBECTL_PROMPT="${context}"
return 0
fi

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

zstyle -s ':zsh-kubectl-prompt:' separator separator
ZSH_KUBECTL_PROMPT="${context}${separator}${ns}"

Expand Down

0 comments on commit c97dac5

Please sign in to comment.