From ec994aff892c4f8901b59520d3a8a94ef87887b3 Mon Sep 17 00:00:00 2001 From: Chad Metcalf Date: Mon, 26 Nov 2018 12:21:21 -0800 Subject: [PATCH] Check for kubectl.exe on Windows (#96) On the various flavors of bash for Windows kubectl won't resolve as the binary is kubectl.exe. Simple aliasing doesn't seem to work. So test for both otherwise fail with a not found error. --- kubectx | 25 +++++++++++++++---------- kubens | 21 ++++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/kubectx b/kubectx index dcc4b9e..0cd56f3 100755 --- a/kubectx +++ b/kubectx @@ -46,11 +46,11 @@ exit_err() { } current_context() { - kubectl config view -o=jsonpath='{.current-context}' + $KUBECTL config view -o=jsonpath='{.current-context}' } get_contexts() { - kubectl config get-contexts -o=name | sort -n + $KUBECTL config get-contexts -o=name | sort -n } list_contexts() { @@ -93,7 +93,7 @@ save_context() { } switch_context() { - kubectl config use-context "${1}" + $KUBECTL config use-context "${1}" } choose_context_interactive() { @@ -129,7 +129,7 @@ swap_context() { } context_exists() { - grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name) + grep -q ^"${1}"\$ <($KUBECTL config get-contexts -o=name) } rename_context() { @@ -142,10 +142,10 @@ rename_context() { if context_exists "${new_name}"; then echo "Context \"${new_name}\" exists, deleting..." >&2 - kubectl config delete-context "${new_name}" 1>/dev/null 2>&1 + $KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1 fi - kubectl config rename-context "${old_name}" "${new_name}" + $KUBECTL config rename-context "${old_name}" "${new_name}" } delete_contexts() { @@ -161,14 +161,19 @@ delete_context() { ctx="$(current_context)" || exit_err "error getting current context" fi echo "Deleting context \"${ctx}\"..." >&2 - kubectl config delete-context "${ctx}" + $KUBECTL config delete-context "${ctx}" } main() { - if ! hash kubectl 2>/dev/null; then - echo >&2 "kubectl is not installed" - exit 1 + if hash kubectl 2>/dev/null; then + KUBECTL=kubectl + elif hash kubectl.exe 2>/dev/null; then + KUBECTL=kubectl.exe + else + echo >&2 "kubectl is not installed" + exit 1 fi + if [[ "$#" -eq 0 ]]; then if [[ -t 1 && -z "${KUBECTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then choose_context_interactive diff --git a/kubens b/kubens index 4b811c1..04bd9f4 100755 --- a/kubens +++ b/kubens @@ -41,9 +41,11 @@ exit_err() { current_namespace() { local cur_ctx + cur_ctx="$(current_context)" || exit_err "error getting current context" - ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \ + 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 @@ -52,11 +54,11 @@ current_namespace() { } current_context() { - kubectl config view -o=jsonpath='{.current-context}' + $KUBECTL config view -o=jsonpath='{.current-context}' } get_namespaces() { - kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' + $KUBECTL get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}' } escape_context_name() { @@ -88,7 +90,7 @@ save_namespace() { switch_namespace() { local ctx="${1}" - kubectl config set-context "${ctx}" --namespace="${2}" + $KUBECTL config set-context "${ctx}" --namespace="${2}" echo "Active namespace is \"${2}\".">&2 } @@ -163,10 +165,15 @@ swap_namespace() { } main() { - if ! hash kubectl 2>/dev/null; then - echo >&2 "kubectl is not installed" - exit 1 + if hash kubectl 2>/dev/null; then + KUBECTL=kubectl + elif hash kubectl.exe 2>/dev/null; then + KUBECTL=kubectl.exe + else + echo >&2 "kubectl is not installed" + exit 1 fi + if [[ "$#" -eq 0 ]]; then if [[ -t 1 && -z ${KUBECTX_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then choose_namespace_interactive