Simplify load_env
This commit is contained in:
parent
ababb6048b
commit
49a4d16951
1 changed files with 20 additions and 30 deletions
44
dnsdrone.sh
44
dnsdrone.sh
|
@ -60,37 +60,27 @@ 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
|
# shellcheck source=/dev/null
|
||||||
source "$envFile" || error "Failed to source .env file"
|
source "$envFile" || error "Failed to source .env file"
|
||||||
|
|
Loading…
Add table
Reference in a new issue