diff --git a/README.md b/README.md index d8bdd16..576d720 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ USAGE: kubectx : list the contexts kubectx : switch to context kubectx - : switch to the previous context + kubectx -c, --current : show the current context name kubectx = : rename context to kubectx =. : rename current-context to kubectx -d : delete context ('.' for current-context) @@ -62,6 +63,7 @@ USAGE: kubens : list the namespaces kubens : change the active namespace kubens - : switch to the previous namespace + kubens -c, --current : show the current namespace ``` diff --git a/kubectx b/kubectx index 0a85429..b168a9c 100755 --- a/kubectx +++ b/kubectx @@ -30,6 +30,7 @@ USAGE: kubectx : list the contexts kubectx : switch to context kubectx - : switch to the previous context + kubectx -c, --current : show the current context name kubectx = : rename context to kubectx =. : rename current-context to kubectx -d [] : delete context ('.' for current-context) @@ -207,6 +208,11 @@ main() { elif [[ "$#" -eq 1 ]]; then if [[ "${1}" == "-" ]]; then swap_context + elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then + # we don't call current_context here for two reasons: + # - it does not fail when current-context property is not set + # - it does not return a trailing newline + kubectl config current-context elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then usage elif [[ "${1}" =~ ^-(.*) ]]; then diff --git a/kubens b/kubens index 51f90aa..c010122 100755 --- a/kubens +++ b/kubens @@ -30,6 +30,7 @@ USAGE: kubens : list the namespaces in the current context kubens : change the active namespace of current context kubens - : switch to the previous namespace in this context + kubens -c, --current : show the current namespace kubens -h,--help : show this message EOF } @@ -197,6 +198,8 @@ main() { usage elif [[ "${1}" == "-" ]]; then swap_namespace + elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then + current_namespace elif [[ "${1}" =~ ^-(.*) ]]; then echo "error: unrecognized flag \"${1}\"" >&2 usage diff --git a/test/kubectx.bats b/test/kubectx.bats index 06a7e03..cfb24c9 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -113,6 +113,33 @@ load common [ "$status" -eq 1 ] } +@test "-c/--current fails when no context set" { + use_config config1 + + run "${COMMAND}" -c + echo "$output" + [ $status -eq 1 ] + run "${COMMAND}" --current + echo "$output" + [ $status -eq 1 ] +} + +@test "-c/--current prints the current context" { + use_config config1 + + run "${COMMAND}" user1@cluster1 + [ $status -eq 0 ] + + run "${COMMAND}" -c + echo "$output" + [ $status -eq 0 ] + [[ "$output" = "user1@cluster1" ]] + run "${COMMAND}" --current + echo "$output" + [ $status -eq 0 ] + [[ "$output" = "user1@cluster1" ]] +} + @test "rename context" { use_config config2 diff --git a/test/kubens.bats b/test/kubens.bats index 0978e6b..a439d86 100644 --- a/test/kubens.bats +++ b/test/kubens.bats @@ -102,3 +102,43 @@ load common [[ "$status" -eq 1 ]] [[ "$output" = *"current-context is not set"* ]] } + +@test "-c/--current works when no namespace is set on context" { + use_config config1 + switch_context user1@cluster1 + + run ${COMMAND} "-c" + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = "default" ]] + run ${COMMAND} "--current" + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = "default" ]] +} + +@test "-c/--current prints the namespace after it is set" { + use_config config1 + switch_context user1@cluster1 + ${COMMAND} ns1 + + run ${COMMAND} "-c" + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = "ns1" ]] + run ${COMMAND} "--current" + echo "$output" + [[ "$status" -eq 0 ]] + [[ "$output" = "ns1" ]] +} + +@test "-c/--current fails when current context is not set" { + use_config config1 + run ${COMMAND} -c + echo "$output" + [[ "$status" -eq 1 ]] + + run ${COMMAND} --current + echo "$output" + [[ "$status" -eq 1 ]] +}