Make path handling more robust
This commit is contained in:
@@ -18,14 +18,42 @@ ppjson() {
|
||||
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)"
|
||||
ENV_FILE="$ROOT_DIR/.env"
|
||||
if [ -f "$ENV_FILE" ]; then
|
||||
set -a
|
||||
# shellcheck source=/dev/null
|
||||
source "$ENV_FILE"
|
||||
set +a
|
||||
# Minimal .env parser:
|
||||
# - ignores comments/blank lines
|
||||
# - preserves spaces in values (quotes optional)
|
||||
# - 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
|
||||
|
||||
# ---------- Defaults (can be overridden by .env) ----------
|
||||
|
Reference in New Issue
Block a user