Skip to content

Commit

Permalink
separate local declarations and function calls
Browse files Browse the repository at this point in the history
Suggested by SC2155 (https://github.com/koalaman/shellcheck/wiki/SC2155).

It captures some of the errors from kubectl that were previously described
in #5. However, this doesn't completely address that.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb committed Apr 3, 2018
1 parent 8033613 commit 6610d70
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
25 changes: 16 additions & 9 deletions kubectx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand All @@ -78,7 +82,8 @@ switch_context() {
}

set_context() {
local prev="$(current_context)"
local prev
prev="$(current_context)"

switch_context "${1}"

Expand All @@ -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
Expand All @@ -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
Expand Down
37 changes: 22 additions & 15 deletions kubens
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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}"
Expand All @@ -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}"
Expand All @@ -95,13 +99,15 @@ set_namespace() {
}

list_namespaces() {
local cur="$(current_namespace)"

local yellow=$(tput setaf 3)
local darkbg=$(tput setab 0)
local normal=$(tput sgr0)

for c in $(get_namespaces); do
local yellow darkbg normal
yellow=$(tput setaf 3)
darkbg=$(tput setab 0)
normal=$(tput sgr0)

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
Expand All @@ -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
Expand Down

0 comments on commit 6610d70

Please sign in to comment.