From e1d6fb6bd978568b0db455b9e85e6b265e65556f Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Fri, 24 Feb 2023 12:42:16 +0100 Subject: [PATCH 1/3] fix: Don't escape # in grep pattern Running `pelias` yields the error `grep: warning: stray \ before #`. This can be fixed by not escaping `#` in the grep pattern. --- lib/env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/env.sh b/lib/env.sh index e935f9f5..e2f63aed 100644 --- a/lib/env.sh +++ b/lib/env.sh @@ -58,7 +58,7 @@ function env_load_stream(){ # load DATA_DIR and other vars from docker-compose .env file # note: strips comments and empty lines -[ -f .env ] && env_load_stream < <(grep -v '^$\|^\s*$\#' .env) +[ -f .env ] && env_load_stream < <(grep -v '^$\|^\s*$#' .env) # use the default compose file unless one was specified # if [ -z "${COMPOSE_FILE}" ]; then From 58fc64907b61ba92b6a7a05eb622b681d2b832ce Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Fri, 24 Feb 2023 12:53:41 +0100 Subject: [PATCH 2/3] fix: Remove line comments The second part of the grep pattern does now remove lines starting with 0 or more whitespace characters followed by #. --- lib/env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/env.sh b/lib/env.sh index e2f63aed..48b772ff 100644 --- a/lib/env.sh +++ b/lib/env.sh @@ -58,7 +58,7 @@ function env_load_stream(){ # load DATA_DIR and other vars from docker-compose .env file # note: strips comments and empty lines -[ -f .env ] && env_load_stream < <(grep -v '^$\|^\s*$#' .env) +[ -f .env ] && env_load_stream < <(grep -v '^$\|^\s*#' .env) # use the default compose file unless one was specified # if [ -z "${COMPOSE_FILE}" ]; then From 709579b31c77d091bb96370df04dda85e5fbe4b4 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Thu, 2 Mar 2023 13:26:56 +0100 Subject: [PATCH 3/3] refactor: Add quotes; remove subshell; add comments One subshell `(...) &&` was replaced with `if [...]; then`. --- lib/env.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/env.sh b/lib/env.sh index 48b772ff..13378fa1 100644 --- a/lib/env.sh +++ b/lib/env.sh @@ -42,13 +42,19 @@ function env_check(){ # loads environment vars from a stream (such as a file) # example: env_load_stream < .env function env_load_stream(){ - [[ -n $DATA_DIR ]] && printf "DATA_DIR is already set to '$DATA_DIR' - this may cause the DATA_DIR specified in the .env to be ignored\n" + [[ -n $DATA_DIR ]] && printf 'DATA_DIR is already set to "%s" - this may cause the DATA_DIR specified in the .env to be ignored\n' "$DATA_DIR" + # split line into $key and $value by first occurrence of = while IFS='=' read -r key value; do - ([ -z $key ] || [ -z $value ]) && printf 'Invalid environment var "%s=%s"\n' $key $value && exit 1 - if [ -z ${!key} ]; then + # assert that the line has characters before and after = by checking if $key or $value is empty + if [ -z "$key" ] || [ -z "$value" ]; then + printf 'Invalid environment var "%s=%s"\n' "$key" "$value" + exit 1 + fi + if [ -z "${!key}" ]; then + # this wil fail if $key starts with whitespace characters export "${key}=${value}" elif $ENV_DISPLAY_WARNINGS; then - printf '[warn] skip setting environment var "%s=%s", already set "%s=%s"\n' $key $value $key ${!key} + printf '[warn] skip setting environment var "%s=%s", already set "%s=%s"\n' "$key" "$value" "$key" "${!key}" fi done } @@ -57,7 +63,7 @@ function env_load_stream(){ # export LC_ALL=en_US.UTF-8 # load DATA_DIR and other vars from docker-compose .env file -# note: strips comments and empty lines +# note: strips line comments ('\s*#') and blank lines ('^$') [ -f .env ] && env_load_stream < <(grep -v '^$\|^\s*#' .env) # use the default compose file unless one was specified