Make path handling more robust
This commit is contained in:
@@ -18,14 +18,42 @@ ppjson() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# ---------- Load .env from repo root (respects var references) ----------
|
# ---------- Load .env from repo root (robust, handles spaces & ${VAR}) ----------
|
||||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
ENV_FILE="$ROOT_DIR/.env"
|
ENV_FILE="$ROOT_DIR/.env"
|
||||||
if [ -f "$ENV_FILE" ]; then
|
if [ -f "$ENV_FILE" ]; then
|
||||||
set -a
|
# Minimal .env parser:
|
||||||
# shellcheck source=/dev/null
|
# - ignores comments/blank lines
|
||||||
source "$ENV_FILE"
|
# - preserves spaces in values (quotes optional)
|
||||||
set +a
|
# - expands ${VAR} references using current environment / previously set keys
|
||||||
|
while IFS= read -r __line || [ -n "$__line" ]; do
|
||||||
|
# trim leading/trailing whitespace
|
||||||
|
__line="${__line#"${__line%%[![:space:]]*}"}"
|
||||||
|
__line="${__line%"${__line##*[![:space:]]}"}"
|
||||||
|
# skip empty or comment
|
||||||
|
[ -z "$__line" ] && continue
|
||||||
|
[ "${__line:0:1}" = "#" ] && continue
|
||||||
|
# split at first '='
|
||||||
|
case "$__line" in
|
||||||
|
*=*)
|
||||||
|
__key="${__line%%=*}"
|
||||||
|
__val="${__line#*=}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
continue
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
# strip surrounding quotes if present
|
||||||
|
if [[ "$__val" == \"*\" && "$__val" == *\" ]]; then
|
||||||
|
__val="${__val:1:${#__val}-2}"
|
||||||
|
elif [[ "$__val" == \'*\' && "$__val" == *\' ]]; then
|
||||||
|
__val="${__val:1:${#__val}-2}"
|
||||||
|
fi
|
||||||
|
# expand ${VAR} references
|
||||||
|
eval "__val_expanded=\"${__val}\""
|
||||||
|
export "${__key}=${__val_expanded}"
|
||||||
|
done < "$ENV_FILE"
|
||||||
|
unset __line __key __val __val_expanded
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ---------- Defaults (can be overridden by .env) ----------
|
# ---------- Defaults (can be overridden by .env) ----------
|
||||||
|
Reference in New Issue
Block a user