separate local declarations and function calls

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>
This commit is contained in:
Ahmet Alp Balkan 2018-04-02 22:20:35 -07:00
parent 80336137bd
commit 6610d70ca8
No known key found for this signature in database
GPG Key ID: 5C02521D7B216AD6
2 changed files with 37 additions and 23 deletions

25
kubectx
View File

@ -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

35
kubens
View File

@ -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