make defaults configurable

This commit is contained in:
fatalerrors
2026-04-15 13:38:08 +02:00
parent 85f02f4498
commit 9a006883b8
11 changed files with 269 additions and 40 deletions

View File

@@ -67,16 +67,38 @@ dwl()
local url="$1"
local output="$2"
if command -v curl >/dev/null 2>&1; then
[ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url"
# Honour preferred tool from configuration; fall back to auto-detection.
local preferred="${DWL_PREFERRED_TOOL:-}"
_try_curl() { [ -z "$output" ] && curl -sL "$url" || curl -sL -o "$output" "$url"; }
_try_wget() { [ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url"; }
_try_fetch() { [ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url"; }
if [[ -n "$preferred" ]]; then
command -v "$preferred" >/dev/null 2>&1 || {
echo "Error: preferred download tool '$preferred' is not installed." >&2
return 1
}
case "$preferred" in
curl) _try_curl ;;
wget) _try_wget ;;
fetch) _try_fetch ;;
*)
echo "Error: DWL_PREFERRED_TOOL '$preferred' is not supported (use curl, wget or fetch)." >&2
return 1
;;
esac
elif command -v curl >/dev/null 2>&1; then
_try_curl
elif command -v wget >/dev/null 2>&1; then
[ -z "$output" ] && wget -qO- "$url" || wget -q -O "$output" "$url"
_try_wget
elif command -v fetch >/dev/null 2>&1; then
[ -z "$output" ] && fetch -o - "$url" || fetch -o "$output" "$url"
_try_fetch
else
echo "Error: No download utility (curl, wget, or fetch) found." >&2
return 1
fi
unset -f _try_curl _try_wget _try_fetch
}
export -f dwl
# ------------------------------------------------------------------------------
@@ -236,13 +258,12 @@ myextip()
shift
done
# Fetch data. Allow overriding endpoint via env var MYEXTIP_URL
local MYEXTIP_URL
MYEXTIP_URL=${MYEXTIP_URL:-https://ip-api.com/json/}
# Fetch data. Allow overriding endpoint via MYEXTIP_DEFAULT_URL config key.
local api_url="${MYEXTIP_DEFAULT_URL:-https://ip-api.com/json/}"
local response
if ! response=$(dwl "$MYEXTIP_URL"); then
disp E "Failed to fetch external IP information from $MYEXTIP_URL"
if ! response=$(dwl "$api_url"); then
disp E "Failed to fetch external IP information from $api_url"
return 2
fi
@@ -272,3 +293,7 @@ export -f myextip
# ------------------------------------------------------------------------------
# EOF
load_conf "net"
# EOF