Simplify load_env

This commit is contained in:
Ruben 2025-03-04 18:26:36 +01:00
parent ababb6048b
commit 49a4d16951

View file

@ -60,40 +60,30 @@ check_dependencies() {
} }
load_env() { load_env() {
local fullRealPath fullLinkPath realPath linkPath realName linkName callDir local linkName realName envFile=""
linkName="$(basename $0)" linkName="$(basename "$0")"
fullLinkPath="$(readlink "$0")" realName="$(basename "$(realpath "$0")")"
fullRealPath="$(realpath "$0")" local -r skyfrittEnvPath="/opt/skyfritt-tools-env"
realName="$(basename "$fullRealPath")" local -a searchPaths=(
realPath="$(dirname "$fullRealPath")" "$(pwd)/.env"
linkPath="$(dirname "$fullLinkPath")" "$(dirname "$(realpath "$0")")/.env"
callDir="$(pwd)" "$(dirname "$(realpath "$0")")/.env.${realName}"
local skyfrittEnvPath="/opt/skyfritt-tools-env" "$(dirname "$(readlink "$0")")/.env.${linkName}"
local envFile="" "${skyfrittEnvPath}/.env.${linkName}"
)
debug "Checking for .env file in multiple locations" debug "Checking for .env file in multiple locations"
if [[ -f "${callDir}/.env" ]]; then # Check in call directory for path in "${searchPaths[@]}"; do
envFile="${callDir}/.env" if [[ -f "$path" ]]; then
debug "Found .env in call directory: ${envFile}" envFile="$path"
elif [[ -f "${realPath}/.env" ]]; then # Check in real path directory debug "Found .env file: ${envFile}"
envFile="${realPath}/.env" break
debug "Found .env in real path directory: ${envFile}"
elif [[ -f "${realPath}/.env.${realName}" ]]; then # Check for .env.realName in real path
envFile="${realPath}/.env.${realName}"
debug "Found .env.${realName} in real path: ${envFile}"
elif [[ -f "${linkPath}/.env.${linkName}" ]]; then # Check for .env.linkName in link path
envFile="${linkPath}/.env.${linkName}"
debug "Found .env.${linkName} in link path: ${envFile}"
elif [[ -f "${skyfrittEnvPath}/.env.${linkName}" ]]; then # Check in skyfritt env path
envFile="${skyfrittEnvPath}/.env.${linkName}"
debug "Found .env.${linkName} in skyfritt path: ${envFile}"
fi fi
done
[[ -z "$envFile" ]] && error ".env file not found in any location" # shellcheck source=/dev/null
source "$envFile" || error "Failed to source .env file"
# shellcheck source=/dev/null
source "$envFile" || error "Failed to source .env file"
# Validate required environment variables # Validate required environment variables
[[ -z "${SERVER:-}" ]] && error "SERVER not set in .env" [[ -z "${SERVER:-}" ]] && error "SERVER not set in .env"