-
-
Save chaspy/9fea6673cd8100a1c864c7874a9d0995 to your computer and use it in GitHub Desktop.
Select Kubernetes context with peco
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
#!/bin/bash | |
PECO_CMD="peco" | |
KUBECTL_CMD="kubectl" | |
if ! hash "${PECO_CMD}" 2> /dev/null; then | |
>&2 echo "error: ${PECO_CMD} is not installed" | |
>&2 echo "see https://github.com/peco/peco" | |
exit 1 | |
fi | |
if ! hash "${KUBECTL_CMD}" 2> /dev/null; then | |
>&2 echo "error: ${KUBECTL_CMD} is not installed" | |
>&2 echo "see https://kubernetes.io/docs/tasks/tools/install-kubectl/" | |
exit 1 | |
fi | |
_current_kube_context=$("${KUBECTL_CMD}" config current-context) | |
_selected_kube_context=$( | |
"${KUBECTL_CMD}" config get-contexts -o name \ | |
| sort \ | |
| awk "{ if (\$1 == \"${_current_kube_context}\") { print \"[*] \" \$1 } else { print \"[ ] \" \$1 } }" \ | |
| "${PECO_CMD}" \ | |
| awk '{ print $NF }' | |
) | |
if [ ! -z "${_selected_kube_context}" ]; then | |
"${KUBECTL_CMD}" config use-context "${_selected_kube_context}" | |
exit $? | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment