Display fzf when provided with an invalid context

This commit is contained in:
Philipp Schmitt 2021-11-24 10:52:34 +01:00
parent 207dd606bb
commit 9a67d765c6
No known key found for this signature in database
GPG Key ID: DC439C47EACB17F9

10
kubectx
View File

@ -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