Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Add kubecontext section to show currently active kubectl context (#76)
Browse files Browse the repository at this point in the history
Closes #73
  • Loading branch information
owais authored and Matan Kushner committed Oct 2, 2018
1 parent 89faf27 commit 8108b6b
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/Options.md
Expand Up @@ -212,6 +212,19 @@ Go section is shown only in directories that contain `Godeps`, `glide.yaml`, any
| `SPACEFISH_GOLANG_SYMBOL` | `馃惞路` | Character to be shown before Go version |
| `SPACEFISH_GOLANG_COLOR` | `cyan` | Color of Go section |


### Kubectl context \(`kubecontext`\)

Kubernetes context is shown everywhere if `kubectl` binary is found.

| Variable | Default | Meaning |
| :------- | :-----: | ------- |
| `SPACEFISH_KUBECONTEXT_SHOW` | `true` | Show current kubectl context |
| `SPACEFISH_KUBECONTEXT_PREFIX` | `at ` | Prefix before the kubectl section |
| `SPACEFISH_KUBECONTEXT_SUFFIX` | `$SPACEFISH_PROMPT_DEFAULT_SUFFIX` | Suffix after the kubectl section |
| `SPACEFISH_KUBECONTEXT_SYMBOL` | `鈽革笍 ` | Character to be shown before kubectl context |
| `SPACEFISH_KUBECONTEXT_COLOR` | `cyan` | Color of kubectl section |

### Exec Time \(`exec_time`\)

Execution time of the last command. Will be displayed if it exceeds the set threshold of time.
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Expand Up @@ -17,6 +17,7 @@
* [Haskell (haskell)](./docs/Options.md#haskell-haskell)
* [Pyenv (pyenv)](./docs/Options.md#pyenv-pyenv)
* [Go (golang)](./docs/Options.md#go-golang)
* [Kubectl context (kubecontext)](./docs/Options.md#kubectl-context-kubecontext)
* [Execution time (exec_time)](./docs/Options.md#execution-time-exec_time)
* [Line Separator (line_sep)](./docs/Options.md#line_sep-node)
* [Battery (battery)](./docs/Options.md#battery-battery)
Expand Down
2 changes: 1 addition & 1 deletion fish_prompt.fish
Expand Up @@ -13,7 +13,7 @@ function fish_prompt
__sf_util_set_default SPACEFISH_PROMPT_SUFFIXES_SHOW true
__sf_util_set_default SPACEFISH_PROMPT_DEFAULT_PREFIX "via "
__sf_util_set_default SPACEFISH_PROMPT_DEFAULT_SUFFIX " "
__sf_util_set_default SPACEFISH_PROMPT_ORDER time user dir host git package node ruby golang haskell pyenv exec_time line_sep battery jobs exit_code char
__sf_util_set_default SPACEFISH_PROMPT_ORDER time user dir host git package node ruby golang haskell pyenv kubecontext exec_time line_sep battery jobs exit_code char

# ------------------------------------------------------------------------------
# Sections
Expand Down
32 changes: 32 additions & 0 deletions functions/__sf_section_kubecontext.fish
@@ -0,0 +1,32 @@
#
# Kubernetes (kubectl)
#
# Kubernetes is an open-source system for deployment, scaling,
# and management of containerized applications.
# Link: https://kubernetes.io/

# ------------------------------------------------------------------------------
# Configuration
# ------------------------------------------------------------------------------
function __sf_section_kubecontext -d "Display the kubernetes context"
__sf_util_set_default SPACEFISH_KUBECONTEXT_SHOW true
__sf_util_set_default SPACEFISH_KUBECONTEXT_PREFIX "at "
__sf_util_set_default SPACEFISH_KUBECONTEXT_SUFFIX $SPACEFISH_PROMPT_DEFAULT_SUFFIX
__sf_util_set_default SPACEFISH_KUBECONTEXT_SYMBOL "鈽革笍 "
__sf_util_set_default SPACEFISH_KUBECONTEXT_COLOR cyan

[ $SPACEFISH_KUBECONTEXT_SHOW = false ]; and return

if not type -q kubectl
return
end

set -l sf_kube_context (kubectl config current-context ^/dev/null)

__sf_lib_section \
$SPACEFISH_KUBECONTEXT_COLOR \
$SPACEFISH_KUBECONTEXT_PREFIX \
"$SPACEFISH_KUBECONTEXT_SYMBOL$sf_kube_context" \
$SPACEFISH_KUBECONTEXT_SUFFIX

end
78 changes: 78 additions & 0 deletions tests/__sf_section_kubecontext.test.fish
@@ -0,0 +1,78 @@
source $DIRNAME/spacefish_test_setup.fish
source $DIRNAME/mock.fish

function setup
spacefish_test_setup
mock kubectl 0 "echo \"testkube\""
end

test "Prints section "
(
set_color --bold fff
echo -n "at "
set_color normal
set_color --bold cyan
echo -n "鈽革笍 testkube"
set_color normal
set_color --bold fff
echo -n " "
set_color normal
) = (__sf_section_kubecontext)
end

test "Changing SPACEFISH_KUBECONTEXT_SYMBOL changes the displayed character"
(
set SPACEFISH_KUBECONTEXT_SYMBOL ""

set_color --bold fff
echo -n "at "
set_color normal
set_color --bold cyan
echo -n "路 testkube"
set_color normal
set_color --bold fff
echo -n " "
set_color normal
) = (__sf_section_kubecontext)
end

test "Changing SPACEFISH_KUBECONTEXT_PREFIX changes the character prefix"
(
set sf_exit_code 0
set SPACEFISH_KUBECONTEXT_PREFIX 路

set_color --bold fff
echo -n ""
set_color normal
set_color --bold cyan
echo -n "鈽革笍 testkube"
set_color normal
set_color --bold fff
echo -n " "
set_color normal
) = (__sf_section_kubecontext)
end

test "Changing SPACEFISH_KUBECONTEXT_SUFFIX changes the character prefix"
(
set sf_exit_code 0
set SPACEFISH_KUBECONTEXT_SUFFIX 路

set_color --bold fff
echo -n "at "
set_color normal
set_color --bold cyan
echo -n "鈽革笍 testkube"
set_color normal
set_color --bold fff
echo -n ""
set_color normal
) = (__sf_section_kubecontext)
end

test "Doesn't display node when SPACEFISH_KUBECONTEXT_SHOW is set to 'false'"
(
set SPACEFISH_KUBECONTEXT_SHOW false
) = (__sf_section_kubecontext)
end

0 comments on commit 8108b6b

Please sign in to comment.