Skip to content

Commit

Permalink
add unset flag (#187)
Browse files Browse the repository at this point in the history
* add unset flag

* test unsetting selected context

* update readme with new unset flag

* testdata notes

* set a current context

* cleanup

* omit fixture changes
  • Loading branch information
rsalmond authored Feb 4, 2020
1 parent f48c419 commit 3369d42
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ USAGE:
kubectx -d <NAME> : delete context <NAME> ('.' 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
Expand Down
8 changes: 8 additions & 0 deletions kubectx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ USAGE:
$SELF -d <NAME> [<NAME...>] : delete context <NAME> ('.' 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/kubectx.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}

0 comments on commit 3369d42

Please sign in to comment.