mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-07-04 11:06:12 +00:00
add support for interactive selection with fzf (#74)
Present a fuzzy search choice in "kubectx" and "kubens" commands without arguments.  Fixes #71.
This commit is contained in:
parent
7b23263fc2
commit
8df92316d6
14
README.md
14
README.md
@ -127,7 +127,17 @@ sudo apt install kubectx
|
|||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
### Customizing current context colors
|
### Interactive mode
|
||||||
|
|
||||||
|
If you want `kubectx` and `kubens` commands to present you an interactive menu
|
||||||
|
with fuzzy searching, you just need to [install
|
||||||
|
`fzf`](https://github.com/junegunn/fzf) in your PATH.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
### Customizing colors
|
||||||
|
|
||||||
If you like to customize the colors indicating the current namespace or context, set the environment variables `KUBECTX_CURRENT_FGCOLOR` and `KUBECTX_CURRENT_BGCOLOR` (refer color codes [here](https://linux.101hacks.com/ps1-examples/prompt-color-using-tput/)):
|
If you like to customize the colors indicating the current namespace or context, set the environment variables `KUBECTX_CURRENT_FGCOLOR` and `KUBECTX_CURRENT_BGCOLOR` (refer color codes [here](https://linux.101hacks.com/ps1-examples/prompt-color-using-tput/)):
|
||||||
|
|
||||||
@ -136,7 +146,7 @@ export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6) # blue text
|
|||||||
export KUBECTX_CURRENT_BGCOLOR=$(tput setaf 7) # white background
|
export KUBECTX_CURRENT_BGCOLOR=$(tput setaf 7) # white background
|
||||||
```
|
```
|
||||||
|
|
||||||
Colors in the output can be disabled by setting the
|
Colors in the output can be disabled by setting the
|
||||||
[`NO_COLOR`](http://no-color.org/) environment variable.
|
[`NO_COLOR`](http://no-color.org/) environment variable.
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
BIN
img/kubectx-interactive.gif
Normal file
BIN
img/kubectx-interactive.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 81 KiB |
17
kubectx
17
kubectx
@ -89,6 +89,17 @@ switch_context() {
|
|||||||
kubectl config use-context "${1}"
|
kubectl config use-context "${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
choose_context_interactive() {
|
||||||
|
local choice
|
||||||
|
choice="$(FZF_DEFAULT_COMMAND='kubectx' fzf --ansi || true)"
|
||||||
|
if [[ -z "${choice}" ]]; then
|
||||||
|
echo 2>&1 "error: you did not choose any of the options"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
set_context "${choice}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
set_context() {
|
set_context() {
|
||||||
local prev
|
local prev
|
||||||
prev="$(current_context)"
|
prev="$(current_context)"
|
||||||
@ -170,7 +181,11 @@ delete_context() {
|
|||||||
|
|
||||||
main() {
|
main() {
|
||||||
if [[ "$#" -eq 0 ]]; then
|
if [[ "$#" -eq 0 ]]; then
|
||||||
list_contexts
|
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||||
|
choose_context_interactive
|
||||||
|
else
|
||||||
|
list_contexts
|
||||||
|
fi
|
||||||
elif [[ "${1}" == "-d" ]]; then
|
elif [[ "${1}" == "-d" ]]; then
|
||||||
if [[ "$#" -lt 2 ]]; then
|
if [[ "$#" -lt 2 ]]; then
|
||||||
echo "error: missing context NAME" >&2
|
echo "error: missing context NAME" >&2
|
||||||
|
25
kubens
25
kubens
@ -85,6 +85,25 @@ switch_namespace() {
|
|||||||
echo "Active namespace is \"${2}\".">&2
|
echo "Active namespace is \"${2}\".">&2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
choose_namespace_interactive() {
|
||||||
|
# directly calling kubens via fzf might fail with a cryptic error like
|
||||||
|
# "$FZF_DEFAULT_COMMAND failed", so try to see if we can list namespaces
|
||||||
|
# locally first
|
||||||
|
if [[ -z "$(list_namespaces)" ]]; then
|
||||||
|
echo >&2 "error: could not list namespaces (is the cluster accessible?)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local choice
|
||||||
|
choice="$(FZF_DEFAULT_COMMAND='kubens' fzf --ansi || true)"
|
||||||
|
if [[ -z "${choice}" ]]; then
|
||||||
|
echo 2>&1 "error: you did not choose any of the options"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
set_namespace "${choice}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
set_namespace() {
|
set_namespace() {
|
||||||
local ctx prev
|
local ctx prev
|
||||||
ctx="$(current_context)"
|
ctx="$(current_context)"
|
||||||
@ -137,7 +156,11 @@ swap_namespace() {
|
|||||||
|
|
||||||
main() {
|
main() {
|
||||||
if [[ "$#" -eq 0 ]]; then
|
if [[ "$#" -eq 0 ]]; then
|
||||||
list_namespaces
|
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||||
|
choose_namespace_interactive
|
||||||
|
else
|
||||||
|
list_namespaces
|
||||||
|
fi
|
||||||
elif [[ "$#" -eq 1 ]]; then
|
elif [[ "$#" -eq 1 ]]; then
|
||||||
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||||
usage
|
usage
|
||||||
|
Loading…
Reference in New Issue
Block a user