diff --git a/dnsdrone.sh b/dnsdrone.sh index df9fa31..a7da2c4 100755 --- a/dnsdrone.sh +++ b/dnsdrone.sh @@ -60,40 +60,30 @@ check_dependencies() { } load_env() { - local fullRealPath fullLinkPath realPath linkPath realName linkName callDir - linkName="$(basename $0)" - fullLinkPath="$(readlink "$0")" - fullRealPath="$(realpath "$0")" - realName="$(basename "$fullRealPath")" - realPath="$(dirname "$fullRealPath")" - linkPath="$(dirname "$fullLinkPath")" - callDir="$(pwd)" - local skyfrittEnvPath="/opt/skyfritt-tools-env" - local envFile="" +local linkName realName envFile="" +linkName="$(basename "$0")" +realName="$(basename "$(realpath "$0")")" +local -r skyfrittEnvPath="/opt/skyfritt-tools-env" +local -a searchPaths=( + "$(pwd)/.env" + "$(dirname "$(realpath "$0")")/.env" + "$(dirname "$(realpath "$0")")/.env.${realName}" + "$(dirname "$(readlink "$0")")/.env.${linkName}" + "${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 - envFile="${callDir}/.env" - debug "Found .env in call directory: ${envFile}" - elif [[ -f "${realPath}/.env" ]]; then # Check in real path directory - envFile="${realPath}/.env" - 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}" +for path in "${searchPaths[@]}"; do + if [[ -f "$path" ]]; then + envFile="$path" + debug "Found .env file: ${envFile}" + break 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 [[ -z "${SERVER:-}" ]] && error "SERVER not set in .env"