Created
May 3, 2023 12:35
-
-
Save FutureGadget/05f5d0f04c701284b65fdbddaa374bed to your computer and use it in GitHub Desktop.
[Zsh] How to show a select menu to switch between Kubernetes contexts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put the uc function in the ~/.zshrc file. | |
# The `uc` function creates a menu that shows available kubernetes contexts you can choose from. | |
# usage: | |
# uc | |
# Available contexts: | |
# 1) prod | |
# 2) stg | |
# > 1 | |
# Switched to context: prod | |
uc() { | |
contexts=(${(f)"$(kubectl config get-contexts -o name)"}) | |
# Display a numbered list of contexts | |
echo "Available contexts:" | |
select context in $contexts; do | |
if [[ -n $context ]]; then | |
# Switch to the selected context | |
kubectl config use-context $context | |
echo "Switched to context: $context" | |
break | |
else | |
echo "Invalid option. Please try again." | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment