diff --git a/kubectx b/kubectx index 7113926..c80ebf8 100755 --- a/kubectx +++ b/kubectx @@ -45,10 +45,13 @@ get_contexts() { list_contexts() { set -u pipefail - local cur="$(current_context)" - local yellow=$(tput setaf 3) - local darkbg=$(tput setab 0) - local normal=$(tput sgr0) + local cur + cur="$(current_context)" + + local yellow darkbg normal + yellow=$(tput setaf 3) + darkbg=$(tput setab 0) + normal=$(tput sgr0) for c in $(get_contexts); do if [[ "${c}" = "${cur}" ]]; then @@ -66,7 +69,8 @@ read_context() { } save_context() { - local saved="$(read_context)" + local saved + saved="$(read_context)" if [[ "${saved}" != "${1}" ]]; then printf %s "${1}" > "${KUBECTX}" @@ -78,7 +82,8 @@ switch_context() { } set_context() { - local prev="$(current_context)" + local prev + prev="$(current_context)" switch_context "${1}" @@ -88,7 +93,8 @@ set_context() { } swap_context() { - local ctx="$(read_context)" + local ctx + ctx="$(read_context)" if [[ -z "${ctx}" ]]; then echo "error: No previous context found." >&2 exit 1 @@ -110,8 +116,9 @@ rename_context() { local old_name="${1}" local new_name="${2}" - local old_user="$(user_of_context "${old_name}")" - local old_cluster="$(cluster_of_context "${old_name}")" + local old_user old_cluster + old_user="$(user_of_context "${old_name}")" + old_cluster="$(cluster_of_context "${old_name}")" if [[ -z "$old_user" || -z "$old_cluster" ]]; then echo "error: Cannot retrieve context ${old_name}." >&2 diff --git a/kubens b/kubens index 7b908b3..7eaf98d 100755 --- a/kubens +++ b/kubens @@ -35,7 +35,8 @@ EOF } current_namespace() { - local cur_ctx=$(current_context) + local cur_ctx + cur_ctx="$(current_context)" ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" if [[ -z "${ns}" ]]; then echo "default" @@ -58,14 +59,16 @@ namespace_file() { } read_namespace() { - local f="$(namespace_file "${1}")" + local f + f="$(namespace_file "${1}")" [[ -f "${f}" ]] && cat "${f}" } save_namespace() { mkdir -p "${KUBENS_DIR}" - local f="$(namespace_file "${1}")" - local saved="$(read_namespace "${1}")" + local f saved + f="$(namespace_file "${1}")" + saved="$(read_namespace "${1}")" if [[ "${saved}" != "${2}" ]]; then printf %s "${2}" > "${f}" @@ -79,8 +82,9 @@ switch_namespace() { } set_namespace() { - local ctx="$(current_context)" - local prev="$(current_namespace)" + local ctx prev + ctx="$(current_context)" + prev="$(current_namespace)" if grep -q ^"${1}"\$ <(get_namespaces); then switch_namespace "${ctx}" "${1}" @@ -95,13 +99,15 @@ set_namespace() { } list_namespaces() { - local cur="$(current_namespace)" + local yellow darkbg normal + yellow=$(tput setaf 3) + darkbg=$(tput setab 0) + normal=$(tput sgr0) - local yellow=$(tput setaf 3) - local darkbg=$(tput setab 0) - local normal=$(tput sgr0) - - for c in $(get_namespaces); do + local cur ns_list + cur="$(current_namespace)" + ns_list=$(get_namespaces) + for c in $ns_list; do if [[ "${c}" = "${cur}" ]]; then echo "${darkbg}${yellow}${c}${normal}" else @@ -111,8 +117,9 @@ list_namespaces() { } swap_namespace() { - local ctx="$(current_context)" - local ns="$(read_namespace "${ctx}")" + local ctx ns + ctx="$(current_context)" + ns="$(read_namespace "${ctx}")" if [[ -z "${ns}" ]]; then echo "error: No previous namespace found for current context." >&2 exit 1