Skip to content

Commit 3369d42

Browse files
authored
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
1 parent f48c419 commit 3369d42

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ USAGE:
3131
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
3232
(this command won't delete the user/cluster entry
3333
that is used by the context)
34+
kubectx -u, --unset : unset the current context
3435
```
3536

3637
### Usage

kubectx

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ USAGE:
4343
$SELF -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
4444
(this command won't delete the user/cluster entry
4545
that is used by the context)
46+
$SELF -u, --unset : unset the current context
4647
4748
$SELF -h,--help : show this message
4849
EOF
@@ -185,6 +186,11 @@ delete_context() {
185186
$KUBECTL config delete-context "${ctx}"
186187
}
187188

189+
unset_context() {
190+
echo "Unsetting current context." >&2
191+
$KUBECTL config unset current-context
192+
}
193+
188194
main() {
189195
if hash kubectl 2>/dev/null; then
190196
KUBECTL=kubectl
@@ -220,6 +226,8 @@ main() {
220226
# - it does not fail when current-context property is not set
221227
# - it does not return a trailing newline
222228
kubectl config current-context
229+
elif [[ "${1}" == '-u' || "${1}" == '--unset' ]]; then
230+
unset_context
223231
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
224232
usage
225233
elif [[ "${1}" =~ ^-(.*) ]]; then

test/kubectx.bats

+13
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,16 @@ load common
239239
[ "$status" -eq 0 ]
240240
[[ "$output" = "user2@cluster1" ]]
241241
}
242+
243+
@test "unset selected context" {
244+
use_config config2
245+
246+
run ${COMMAND} user1@cluster1
247+
[ "$status" -eq 0 ]
248+
249+
run ${COMMAND} -u
250+
[ "$status" -eq 0 ]
251+
252+
run ${COMMAND} -c
253+
[ "$status" -ne 0 ]
254+
}

0 commit comments

Comments
 (0)