From 4a1d73d5feab02ff74741edadfaed5f192f5c50d Mon Sep 17 00:00:00 2001 From: uesteibar Date: Fri, 18 May 2018 15:46:05 +0200 Subject: [PATCH] Allow deleting multiple contexts at once fixes #39 Example: `kubectx -d ctx1 ctx2` --- kubectx | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/kubectx b/kubectx index 67714aa..eb7c1af 100755 --- a/kubectx +++ b/kubectx @@ -26,14 +26,14 @@ KUBECTX="${HOME}/.kube/kubectx" usage() { cat <<"EOF" USAGE: - kubectx : list the contexts - kubectx : switch to context - kubectx - : switch to the previous context - kubectx = : rename context to - kubectx =. : rename current-context to - kubectx -d : delete context ('.' for current-context) - (this command won't delete the user/cluster entry - that is used by the context) + kubectx : list the contexts + kubectx : switch to context + kubectx - : switch to the previous context + kubectx = : rename context to + kubectx =. : rename current-context to + kubectx -d [] : delete context ('.' for current-context) + (this command won't delete the user/cluster entry + that is used by the context) kubectx -h,--help : show this message EOF @@ -148,6 +148,13 @@ rename_context() { kubectl config rename-context "${old_name}" "${new_name}" } +delete_contexts() { + IFS=' ' read -ra CTXS <<< "${1}" + for i in "${CTXS[@]}"; do + delete_context "${i}" + done +} + delete_context() { local ctx ctx="${1}" @@ -165,11 +172,8 @@ main() { if [[ "$#" -lt 2 ]]; then echo "error: missing context NAME" >&2 usage - elif [[ "$#" -gt 2 ]]; then - echo "error: too many arguments" >&2 - usage fi - delete_context "${2}" + delete_contexts "${@:2}" elif [[ "$#" -gt 1 ]]; then echo "error: too many arguments" >&2 usage