🔧 use rename-context in place of set-context

This commit is contained in:
Bhargav Nookala 2017-11-02 16:48:58 -07:00
parent ed2afa7572
commit 9ed6690978

14
kubectx
View File

@ -36,9 +36,9 @@ usage() {
cat <<"EOF" cat <<"EOF"
USAGE: USAGE:
kubectx : list the contexts kubectx : list the contexts
kubectx <NAME> : switch to context kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context kubectx - : switch to the previous context
kubectx <NEW_NAME>=<NAME> : create alias for context kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx -h,--help : show this message kubectx -h,--help : show this message
EOF EOF
exit 1 exit 1
@ -107,7 +107,7 @@ cluster_of_context() {
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}" -o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}"
} }
alias_context() { rename_context() {
local old_name="${1}" local old_name="${1}"
local new_name="${2}" local new_name="${2}"
@ -119,11 +119,7 @@ alias_context() {
exit 1 exit 1
fi fi
kubectl config set-context "${new_name}" \ kubectl config rename-context "${old_name}" "${new_name}"
--cluster="${old_cluster}" \
--user="${old_user}" \
echo "Aliased \"${old_name}\" as \"${new_name}\"." >&2
} }
main() { main() {
@ -141,7 +137,7 @@ main() {
echo "error: unrecognized flag \"${1}\"" >&2 echo "error: unrecognized flag \"${1}\"" >&2
usage usage
elif [[ "${1}" =~ (.+)=(.+) ]]; then elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}" rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else else
set_context "${1}" set_context "${1}"
fi fi