Skip to content

Instantly share code, notes, and snippets.

@chaspy
Forked from yuya-takeyama/peco-kubectx
Created September 9, 2018 15:45
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 chaspy/9fea6673cd8100a1c864c7874a9d0995 to your computer and use it in GitHub Desktop.
Save chaspy/9fea6673cd8100a1c864c7874a9d0995 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