harden the config reader against an empty or malformed .env
holcommon's empty .env made the loader evaluate one blank line, which tripped set -u. Verified afterwards on holcommon's real host stack: the CLI and the make engine resolve the same five files and render a byte-identical 241-line compose config.
This commit is contained in:
@@ -118,7 +118,7 @@ WORKDIR=${WORKDIR:-$PWD}
|
||||
WORKDIR=$(cd "$WORKDIR" 2>/dev/null && pwd -P) || myos_die "$MYOS_E_USAGE" "no such directory: $WORKDIR"
|
||||
|
||||
for _f in $(myos_conf_files); do myos_dotenv_load "$_f"; done
|
||||
myos_dotenv_load "$HOME/.config/myos/config"
|
||||
myos_dotenv_load "${HOME:-}/.config/myos/config"
|
||||
ENV=${ENV:-local}
|
||||
myos_dotenv_load "$WORKDIR/.env.$ENV"
|
||||
myos_dotenv_load "$WORKDIR/.env"
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ myos_cmd_doctor() {
|
||||
|
||||
printf 'configuration:\n'
|
||||
for _f in $(myos_conf_files); do _ok "$_f" "read"; done
|
||||
[ -f "$HOME/.config/myos/config" ] && _ok "$HOME/.config/myos/config" "read"
|
||||
[ -f "${HOME:-}/.config/myos/config" ] && _ok "${HOME:-}/.config/myos/config" "read"
|
||||
[ -f "$WORKDIR/.env" ] && _ok "$WORKDIR/.env" "read"
|
||||
_ok ENV "$ENV"
|
||||
_ok USER "$USER"
|
||||
|
||||
@@ -36,7 +36,10 @@ myos_dotenv_parse() {
|
||||
myos_dotenv_load() {
|
||||
[ -f "$1" ] || return 0
|
||||
while IFS= read -r _kv; do
|
||||
# an empty file still yields one empty line through the here-document
|
||||
[ -n "$_kv" ] || continue
|
||||
_k=${_kv%%=*}
|
||||
[ -n "$_k" ] || continue
|
||||
[ -n "$(myos_var "$_k")" ] && continue
|
||||
eval "$_k=\${_kv#*=}"
|
||||
done <<EOF
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ myos_path() {
|
||||
_wd=${WORKDIR:-$PWD}
|
||||
_name=${STACK_DIR_NAME:-stack}
|
||||
_out=
|
||||
for _d in "$_wd" "$_wd/.." "$HOME/.local/share" /usr/local/share /usr/share; do
|
||||
for _d in "$_wd" "$_wd/.." "${HOME:-/nonexistent}/.local/share" /usr/local/share /usr/share; do
|
||||
for _c in "$_d/$_name" "$_d/myos/$_name"; do
|
||||
[ -d "$_c" ] || continue
|
||||
_c=$(cd "$_c" && pwd -P)
|
||||
|
||||
+4
-1
@@ -6,7 +6,10 @@
|
||||
# to consul, fabio routes on the urlprefix- tags it finds there.
|
||||
|
||||
# myos_var NAME value of the variable named NAME, empty when unset
|
||||
myos_var() { eval "printf '%s' \"\${$1:-}\""; }
|
||||
myos_var() {
|
||||
[ -n "${1:-}" ] || return 0
|
||||
eval "printf '%s' \"\${$1:-}\""
|
||||
}
|
||||
|
||||
# myos_uri SERVICE PORT [BASE_URI]
|
||||
# <service>.<base uri>, unless <SERVICE>_SERVICE[_<port>]_NAME overrides the prefix
|
||||
|
||||
@@ -80,3 +80,27 @@ Describe 'lib/config.sh'
|
||||
End
|
||||
End
|
||||
End
|
||||
|
||||
Describe 'lib/config.sh robustness'
|
||||
setup() { MYOS_TMP=$(mktemp -d "${TMPDIR:-/tmp}/myos-cfg.XXXXXX"); }
|
||||
cleanup() { rm -rf "$MYOS_TMP"; }
|
||||
BeforeEach setup
|
||||
AfterEach cleanup
|
||||
|
||||
It 'loads an empty .env without complaining'
|
||||
: > "$MYOS_TMP/.env"
|
||||
When call myos_dotenv_load "$MYOS_TMP/.env"
|
||||
The status should be success
|
||||
The stderr should equal ""
|
||||
End
|
||||
It 'ignores a line with an empty key'
|
||||
printf '=orphan\nGOOD=1\n' > "$MYOS_TMP/.env"
|
||||
When call myos_dotenv_parse "$MYOS_TMP/.env"
|
||||
The output should equal "GOOD=1"
|
||||
End
|
||||
It 'returns empty for an unnamed variable'
|
||||
When call myos_var ""
|
||||
The output should equal ""
|
||||
The status should be success
|
||||
End
|
||||
End
|
||||
|
||||
Reference in New Issue
Block a user