You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/usr/bin/env bash
#
# wrap on kubectx adding -i option to switch to context by displayed index
# info on kubectx https://github.com/ahmetb/kubectx
[[ -n $DEBUG ]] && set -x
set -eou pipefail
IFS=$'\n\t'
declare -A ctxs
indexedswitch() {
local index=1
local ctx
local cctx=$(kubectl config current-context)
local number='^[0-9]+$'
while read ctx; do
ctxs[$index]=$ctx
printf "%s \t%s %s\n" "$index" "$([ "$ctx" = "$cctx" ] && echo "*" || echo " ")" "$ctx"
index=$(expr $index + 1)
done < <(kubectx)
echo -n "context name or number to switch to, enter to quit: "
read ctx
if [ -n "$ctx" ]; then
if [[ $ctx =~ $number ]]; then
kubectx "${ctxs[$ctx]}"
else
kubectx $ctx
fi
fi
}
if [ "${1:-""}" = "-i" ]; then
indexedswitch
else
kubectx $@
if [ "${1:-""}" = "-h" -o "${1:-""}" = "--help" ]; then
echo "When calling via wrapper name kctx"
echo " kctx -i : switch to context by index"
fi
fi
The text was updated successfully, but these errors were encountered:
If you install fzf (see readme) you actually get to choose by hitting arrow keys or writing the ctx/ns name partially or closely as it does fuzzy string matching. Does that help?
Definitely it helps and I learned something new. However, I probably can not use fzf with dumb terminals, e.g., executing the command remotely or in emacs shells.
I find it helpful to mark the current context when listing.
By the way, is there a tool changing the context for the current login session without affecting other sessions logged in to the same account at the same time? We may need to define aliases and use kubectl command line switches.
See a wrapper 'kctx' below for details:
The text was updated successfully, but these errors were encountered: