diff --git a/kubectx b/kubectx index 4f02e5b..3bb6443 100755 --- a/kubectx +++ b/kubectx @@ -108,14 +108,20 @@ save_context() { } switch_context() { - $KUBECTL config use-context "${1}" + if ! $KUBECTL config use-context "${1}"; then + if [[ -t 1 && -z "${KUBECTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then + choose_context_interactive "${1}" + else + return 1 + fi + fi } choose_context_interactive() { local choice choice="$(_KUBECTX_FORCE_COLOR=1 \ FZF_DEFAULT_COMMAND="${SELF_CMD}" \ - fzf --ansi --no-preview || true)" + fzf --ansi --no-preview --query "${1:-}" --select-1 || true)" if [[ -z "${choice}" ]]; then echo 2>&1 "error: you did not choose any of the options" exit 1