├── preview.png ├── functions ├── kube_ps.fish └── __kube_prompt.fish ├── LICENSE └── README.md /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aluxian/fish-kube-prompt/HEAD/preview.png -------------------------------------------------------------------------------- /functions/kube_ps.fish: -------------------------------------------------------------------------------- 1 | function kube_ps -a toggle 2 | if test "$toggle" = "on" 3 | set -U __kube_ps_enabled 1 4 | return 5 | end 6 | 7 | if test "$toggle" = "off" 8 | set -U __kube_ps_enabled 0 9 | return 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alexandru Rosianu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fish-kube-prompt 2 | 3 | ⎈ kubectl context/namespace in your fish shell prompt 4 | 5 | ![preview](preview.png) 6 | 7 | ## Install 8 | 9 | ```fish 10 | mkdir -p ~/.config/fish/functions/ 11 | cd ~/.config/fish/ 12 | git clone https://github.com/aluxian/fish-kube-prompt 13 | ln -s ../fish-kube-prompt/functions/__kube_prompt.fish functions/ 14 | ln -s ../fish-kube-prompt/functions/kube_ps.fish functions/ 15 | ``` 16 | 17 | Then create or edit `~/.config/fish/functions/fish_prompt.fish` to include 18 | `__kube_prompt`: 19 | 20 | ```fish 21 | function fish_prompt 22 | echo -s (set_color blue) (__kube_prompt) (set_color $fish_color_cwd) " " (prompt_pwd) (set_color normal) "> " 23 | end 24 | ``` 25 | 26 | ## Speed 27 | 28 | Running the `kubectl` commands to get the context and namespace every time 29 | the prompt is shown would slow down the prompt significantly. Therefore, 30 | `fish-kube-prompt` caches the context and namespace by only calling `kubectl` 31 | when the env var `KUBECONFIG` has changed since the last cache update, or 32 | when the 'last modified' timestamp of the files in `KUBECONFIG` is newer 33 | than the timestamp of the last cache update. 34 | 35 | This was not my idea, I took it from `jonmosco/kube-ps1`. 36 | 37 | ## Config 38 | 39 | You can toggle the prompt on or off like this: 40 | 41 | ```fish 42 | kube_ps on 43 | kube_ps off 44 | ``` 45 | 46 | `kube_ps` is a simple function that just updates a universal variable 47 | `__kube_ps_enabled`. 48 | 49 | > Many choices have been hard-coded (e.g. colors, delimiters). If there's anything 50 | you'd like to customize, please add an env var and send a PR. 51 | 52 | ## Credits 53 | 54 | Inspired from the awesome work of: 55 | 56 | * https://github.com/jonmosco/kube-ps1 57 | * https://github.com/Ladicle/fish-kubectl-prompt 58 | 59 | ## Author 60 | 61 | Alexandru Rosianu (https://github.com/aluxian/dotfiles-fish) 62 | 63 | ## License 64 | 65 | MIT 66 | -------------------------------------------------------------------------------- /functions/__kube_prompt.fish: -------------------------------------------------------------------------------- 1 | # Inspired from: 2 | # https://github.com/jonmosco/kube-ps1 3 | # https://github.com/Ladicle/fish-kubectl-prompt 4 | 5 | function __kube_ps_update_cache 6 | function __kube_ps_cache_context 7 | set -l ctx (kubectl config current-context 2>/dev/null) 8 | if test $status -eq 0 9 | set -g __kube_ps_context "$ctx" 10 | else 11 | set -g __kube_ps_context "n/a" 12 | end 13 | end 14 | 15 | function __kube_ps_cache_namespace 16 | set -l ns (kubectl config view --minify -o 'jsonpath={..namespace}' 2>/dev/null) 17 | if test -n "$ns" 18 | set -g __kube_ps_namespace "$ns" 19 | else 20 | set -g __kube_ps_namespace "default" 21 | end 22 | end 23 | 24 | function __stat_mtime 25 | #-- cross-platform workaround; POSIX didn't specify stat(1) and so 26 | #-- its interface is incompatibly different on Mac OS and Linux. 27 | #-- see https://unix.stackexchange.com/q/561927/3097 28 | python -c "print(__import__('os').stat(__import__('sys').argv[1]).st_mtime)" $argv 29 | end 30 | 31 | set -l kubeconfig "$KUBECONFIG" 32 | if test -z "$kubeconfig" 33 | set kubeconfig "$HOME/.kube/config" 34 | end 35 | 36 | if test "$kubeconfig" != "$__kube_ps_kubeconfig" 37 | __kube_ps_cache_context 38 | __kube_ps_cache_namespace 39 | set -g __kube_ps_kubeconfig "$kubeconfig" 40 | set -g __kube_ps_timestamp (date +%s) 41 | return 42 | end 43 | 44 | for conf in (string split ':' "$kubeconfig") 45 | if test -r "$conf" 46 | if test -z "$__kube_ps_timestamp"; or test (__stat_mtime "$conf") -gt "$__kube_ps_timestamp" 47 | __kube_ps_cache_context 48 | __kube_ps_cache_namespace 49 | set -g __kube_ps_kubeconfig "$kubeconfig" 50 | set -g __kube_ps_timestamp (date +%s) 51 | return 52 | end 53 | end 54 | end 55 | end 56 | 57 | function __kube_prompt 58 | if test -z "$__kube_ps_enabled"; or test $__kube_ps_enabled -ne 1 59 | return 60 | end 61 | 62 | __kube_ps_update_cache 63 | echo -n -s " (⎈ $__kube_ps_context|$__kube_ps_namespace)" 64 | end 65 | --------------------------------------------------------------------------------