From 3369d42e2dd185c04b149078cfcbfd0739f29677 Mon Sep 17 00:00:00 2001 From: rob salmond Date: Tue, 4 Feb 2020 10:39:47 -0700 Subject: [PATCH] add unset flag (#187) * add unset flag * test unsetting selected context * update readme with new unset flag * testdata notes * set a current context * cleanup * omit fixture changes --- README.md | 1 + kubectx | 8 ++++++++ test/kubectx.bats | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 7dbc455..7367f39 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ USAGE: kubectx -d : delete context ('.' for current-context) (this command won't delete the user/cluster entry that is used by the context) + kubectx -u, --unset : unset the current context ``` ### Usage diff --git a/kubectx b/kubectx index ae5ac6f..d2079d5 100755 --- a/kubectx +++ b/kubectx @@ -43,6 +43,7 @@ USAGE: $SELF -d [] : delete context ('.' for current-context) (this command won't delete the user/cluster entry that is used by the context) + $SELF -u, --unset : unset the current context $SELF -h,--help : show this message EOF @@ -185,6 +186,11 @@ delete_context() { $KUBECTL config delete-context "${ctx}" } +unset_context() { + echo "Unsetting current context." >&2 + $KUBECTL config unset current-context +} + main() { if hash kubectl 2>/dev/null; then KUBECTL=kubectl @@ -220,6 +226,8 @@ main() { # - it does not fail when current-context property is not set # - it does not return a trailing newline kubectl config current-context + elif [[ "${1}" == '-u' || "${1}" == '--unset' ]]; then + unset_context elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then usage elif [[ "${1}" =~ ^-(.*) ]]; then diff --git a/test/kubectx.bats b/test/kubectx.bats index cfb24c9..d5e0e64 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -239,3 +239,16 @@ load common [ "$status" -eq 0 ] [[ "$output" = "user2@cluster1" ]] } + +@test "unset selected context" { + use_config config2 + + run ${COMMAND} user1@cluster1 + [ "$status" -eq 0 ] + + run ${COMMAND} -u + [ "$status" -eq 0 ] + + run ${COMMAND} -c + [ "$status" -ne 0 ] +}