From 3aeb4e76d218590df0554dbbe2b3b7907ccd1f4a Mon Sep 17 00:00:00 2001 From: Robert James Hernandez Date: Wed, 7 Nov 2018 09:27:34 -0800 Subject: [PATCH] fix subshell error handling (#95) fixes #5 --- kubectx | 16 +++++++++++----- kubens | 21 ++++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/kubectx b/kubectx index cfa7c00..dcc4b9e 100755 --- a/kubectx +++ b/kubectx @@ -40,6 +40,11 @@ USAGE: EOF } +exit_err() { + echo >&2 "${1}" + exit 1 +} + current_context() { kubectl config view -o=jsonpath='{.current-context}' } @@ -50,8 +55,9 @@ get_contexts() { list_contexts() { set -u pipefail - local cur - cur="$(current_context)" + local cur ctx_list + cur="$(current_context)" || exit_err "error getting current context" + ctx_list=$(get_contexts) || exit_err "error getting context list" local yellow darkbg normal yellow=$(tput setaf 3 || true) @@ -62,7 +68,7 @@ list_contexts() { cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow} cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg} - for c in $(get_contexts); do + for c in $ctx_list; do if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}" else @@ -103,7 +109,7 @@ choose_context_interactive() { set_context() { local prev - prev="$(current_context)" + prev="$(current_context)" || exit_err "error getting current context" switch_context "${1}" @@ -152,7 +158,7 @@ delete_context() { local ctx ctx="${1}" if [[ "${ctx}" == "." ]]; then - ctx="$(current_context)" + ctx="$(current_context)" || exit_err "error getting current context" fi echo "Deleting context \"${ctx}\"..." >&2 kubectl config delete-context "${ctx}" diff --git a/kubens b/kubens index c246b0f..4b811c1 100755 --- a/kubens +++ b/kubens @@ -34,10 +34,16 @@ USAGE: EOF } +exit_err() { + echo >&2 "${1}" + exit 1 +} + current_namespace() { local cur_ctx - cur_ctx="$(current_context)" - ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" + cur_ctx="$(current_context)" || exit_err "error getting current context" + ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \ + || exit_err "error getting current namespace" if [[ -z "${ns}" ]]; then echo "default" else @@ -107,8 +113,8 @@ choose_namespace_interactive() { set_namespace() { local ctx prev - ctx="$(current_context)" - prev="$(current_namespace)" + ctx="$(current_context)" || exit_err "error getting current context" + prev="$(current_namespace)" || exit_error "error getting current namespace" if grep -q ^"${1}"\$ <(get_namespaces); then switch_namespace "${ctx}" "${1}" @@ -133,8 +139,9 @@ list_namespaces() { cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg} local cur ns_list - cur="$(current_namespace)" - ns_list=$(get_namespaces) + cur="$(current_namespace)" || exit_err "error getting current namespace" + ns_list=$(get_namespaces) || exit_err "error getting namespace list" + for c in $ns_list; do if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}" @@ -146,7 +153,7 @@ list_namespaces() { swap_namespace() { local ctx ns - ctx="$(current_context)" + ctx="$(current_context)" || exit_err "error getting current context" ns="$(read_namespace "${ctx}")" if [[ -z "${ns}" ]]; then echo "error: No previous namespace found for current context." >&2