Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Last active February 28, 2019 13:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yuya-takeyama/ebaab5a2c4273eade894135891c9fa1c to your computer and use it in GitHub Desktop.
Save yuya-takeyama/ebaab5a2c4273eade894135891c9fa1c to your computer and use it in GitHub Desktop.
Select Kubernetes context with peco
#!/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