From 87114b7a7f54bc82b946d1a1ca94cf1dea1f56a3 Mon Sep 17 00:00:00 2001 From: Andre Anjos <andre.dos.anjos@gmail.com> Date: Sat, 3 Mar 2018 08:29:33 +0100 Subject: [PATCH] Improve environment variable checker function --- gitlab/functions.sh | 48 +++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/gitlab/functions.sh b/gitlab/functions.sh index b98b26e..1cf58aa 100644 --- a/gitlab/functions.sh +++ b/gitlab/functions.sh @@ -73,50 +73,46 @@ log_error() { } -# Checks a given environment variable is set (non-zero size) -check_env() { - if [ -z "${1+abc}" ]; then +# Checks just if the variable is defined and has non-zero length +check_defined() { + if [ -z "${!1+abc}" ]; then log_error "Variable ${1} is undefined - aborting..."; exit 1 - else - log_info "${1}=${!1}"; + elif [ -z "${!1}" ]; then + log_error "Variable ${1} is zero-length - aborting..."; + exit 1 fi } +# Checks a given environment variable is set (non-zero size) +check_env() { + check_defined "${1}" + log_info "${1}=${!1}" +} + + # Checks a given environment variable array is set (non-zero size) check_array_env() { - if [ -z "${1+abc}" ]; then - log_error "Variable ${1} is undefined - aborting..."; - exit 1 - else - for i in "${!foo[@]}"; do - log_info "${1}[${i}]=${!1[${i}]}"; - done - fi + check_defined "${1}" + for i in "${!1[@]}"; do + log_info "${1}[${i}]=${!1[${i}]}"; + done } # Exports a given environment variable, verbosely export_env() { - if [ -z "${1+abc}" ]; then - log_error "Variable ${1} is undefined - aborting..."; - exit 1 - else - export ${1} - log_info "export ${1}=${!1}"; - fi + check_defined "${1}" + export ${1} + log_info "export ${1}=${!1}" } # Checks a given environment variable is set (non-zero size) check_pass() { - if [ -z "${1+abc}" ]; then - log_error "Variable ${1} is undefined - aborting..."; - exit 1 - else - log_info "${1}=********"; - fi + check_defined "${1}" + log_info "${1}=********" } -- GitLab