Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement request: saving typing by adding an indexed context switch #302

Open
wu105 opened this issue Jun 17, 2021 · 4 comments
Open

Comments

@wu105
Copy link

wu105 commented Jun 17, 2021

See a wrapper 'kctx' below for details:

#!/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
@ahmetb
Copy link
Owner

ahmetb commented Jun 17, 2021

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?

@wu105
Copy link
Author

wu105 commented Jun 17, 2021

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.

@briantopping
Copy link

Does fzf work with the plugin added via krew? I just manage all my plugins that way, easier on a corporate laptop. Thanks!

@ahmetb
Copy link
Owner

ahmetb commented Jul 1, 2021

@briantopping no, you need to install fzf separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants