66 lines
1.5 KiB
Bash
Executable File
66 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Environment loading functions
|
|
# Usage: source "$(dirname "$0")/env.sh"
|
|
# Requires: logging.sh, utils.sh
|
|
|
|
# Load environment file
|
|
load_env() {
|
|
local env_file="${1:-.env}"
|
|
local project_root="${PROJECT_ROOT:-$(get_project_root)}"
|
|
local full_path="$project_root/$env_file"
|
|
|
|
if [ -f "$full_path" ]; then
|
|
log_debug "Loading environment from: $full_path"
|
|
set -a
|
|
source "$full_path"
|
|
set +a
|
|
return 0
|
|
else
|
|
log_debug "Environment file not found: $full_path"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Require environment variable
|
|
require_env() {
|
|
local var_name="$1"
|
|
local var_value="${!var_name}"
|
|
|
|
if [ -z "$var_value" ]; then
|
|
log_error "Required environment variable not set: $var_name"
|
|
exit 1
|
|
fi
|
|
|
|
log_debug "Environment variable $var_name is set"
|
|
}
|
|
|
|
# Require multiple environment variables
|
|
require_envs() {
|
|
local missing_vars=()
|
|
|
|
for var_name in "$@"; do
|
|
if [ -z "${!var_name}" ]; then
|
|
missing_vars+=("$var_name")
|
|
fi
|
|
done
|
|
|
|
if [ ${#missing_vars[@]} -gt 0 ]; then
|
|
log_error "Missing required environment variables: ${missing_vars[*]}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Get environment variable with default
|
|
get_env() {
|
|
local var_name="$1"
|
|
local default_value="${2:-}"
|
|
local var_value="${!var_name:-$default_value}"
|
|
|
|
if [ -z "$var_value" ] && [ -z "$default_value" ]; then
|
|
log_warn "Environment variable $var_name is not set and no default provided"
|
|
fi
|
|
|
|
echo "$var_value"
|
|
}
|
|
|