关联 Issue: P25-1, P25-2, P25-3, P25-4, P25-5, P26-1, P26-3, P24-1, P24-2, P29-1, P29-2, P29-3, P29-4
44 行
916 B
Bash
44 行
916 B
Bash
#!/usr/bin/env bash
|
|
|
|
load_env_defaults() {
|
|
local env_file="${1:-.env}"
|
|
|
|
if [[ ! -f "$env_file" ]]; then
|
|
return 0
|
|
fi
|
|
|
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
|
if [[ "$line" =~ ^[[:space:]]*$ ]] || [[ "$line" =~ ^[[:space:]]*# ]]; then
|
|
continue
|
|
fi
|
|
|
|
if [[ "$line" != *=* ]]; then
|
|
continue
|
|
fi
|
|
|
|
local key="${line%%=*}"
|
|
local value="${line#*=}"
|
|
|
|
key="${key#"${key%%[![:space:]]*}"}"
|
|
key="${key%"${key##*[![:space:]]}"}"
|
|
value="${value#"${value%%[![:space:]]*}"}"
|
|
value="${value%"${value##*[![:space:]]}"}"
|
|
|
|
if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
|
|
continue
|
|
fi
|
|
|
|
if [[ -n "${!key+x}" ]]; then
|
|
continue
|
|
fi
|
|
|
|
if [[ "$value" =~ ^\"(.*)\"$ ]]; then
|
|
value="${BASH_REMATCH[1]}"
|
|
elif [[ "$value" =~ ^\'(.*)\'$ ]]; then
|
|
value="${BASH_REMATCH[1]}"
|
|
fi
|
|
|
|
export "$key=$value"
|
|
done < "$env_file"
|
|
}
|