Last active
February 28, 2019 13:27
-
-
Save yuya-takeyama/ebaab5a2c4273eade894135891c9fa1c 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