Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FutureGadget/05f5d0f04c701284b65fdbddaa374bed to your computer and use it in GitHub Desktop.
Save FutureGadget/05f5d0f04c701284b65fdbddaa374bed to your computer and use it in GitHub Desktop.
[Zsh] How to show a select menu to switch between Kubernetes contexts
# 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