refactor: use if/elif/else instead of case for HTTP client detection

Avoids shellcheck SC2195 warnings caused by Terraform $${} escaping
that shellcheck doesn't understand. Split into fetch() for stdout
output and fetch_to_file() for downloading to disk, matching the
pattern used in vault-github module.
This commit is contained in:
Atif Ali 2025-12-29 16:35:44 +00:00
parent f295070544
commit 7cdc1069b1

View File

@ -6,40 +6,34 @@ VAULT_TOKEN=${VAULT_TOKEN}
INSTALL_DIR=${INSTALL_DIR} INSTALL_DIR=${INSTALL_DIR}
VAULT_CLI_VERSION=${VAULT_CLI_VERSION} VAULT_CLI_VERSION=${VAULT_CLI_VERSION}
# Fetch URL content. If dest is provided, write to file; otherwise output to stdout. # Fetch URL content to stdout
# Usage: fetch <url> [dest]
fetch() { fetch() {
url="$1" url="$1"
dest="$${2:-}"
# Detect HTTP client on first run
if [ -z "$${HTTP_CLIENT:-}" ]; then
if command -v curl > /dev/null 2>&1; then if command -v curl > /dev/null 2>&1; then
HTTP_CLIENT="curl" curl -sSL --fail "$${url}"
elif command -v wget > /dev/null 2>&1; then elif command -v wget > /dev/null 2>&1; then
HTTP_CLIENT="wget" wget -qO- "$${url}"
elif command -v busybox > /dev/null 2>&1; then elif command -v busybox > /dev/null 2>&1; then
HTTP_CLIENT="busybox" busybox wget -qO- "$${url}"
else else
printf "curl, wget, or busybox is not installed. Please install curl or wget in your image.\n" printf "curl, wget, or busybox is not installed. Please install curl or wget in your image.\n"
return 1 return 1
fi fi
fi }
if [ -n "$${dest}" ]; then # Download URL to a file
# shellcheck disable=SC2195 fetch_to_file() {
case "$${HTTP_CLIENT}" in dest="$1"
curl) curl -sSL --fail "$${url}" -o "$${dest}" ;; url="$2"
wget) wget -O "$${dest}" "$${url}" ;; if command -v curl > /dev/null 2>&1; then
busybox) busybox wget -O "$${dest}" "$${url}" ;; curl -sSL --fail "$${url}" -o "$${dest}"
esac elif command -v wget > /dev/null 2>&1; then
wget -O "$${dest}" "$${url}"
elif command -v busybox > /dev/null 2>&1; then
busybox wget -O "$${dest}" "$${url}"
else else
# shellcheck disable=SC2195 printf "curl, wget, or busybox is not installed. Please install curl or wget in your image.\n"
case "$${HTTP_CLIENT}" in return 1
curl) curl -sSL --fail "$${url}" ;;
wget) wget -qO- "$${url}" ;;
busybox) busybox wget -qO- "$${url}" ;;
esac
fi fi
} }
@ -131,7 +125,7 @@ install() {
cd "$${TEMP_DIR}" || return 1 cd "$${TEMP_DIR}" || return 1
printf "Downloading from %s\n" "$${DOWNLOAD_URL}" printf "Downloading from %s\n" "$${DOWNLOAD_URL}"
if ! fetch "$${DOWNLOAD_URL}" vault.zip; then if ! fetch_to_file vault.zip "$${DOWNLOAD_URL}"; then
printf "Failed to download Vault.\n" printf "Failed to download Vault.\n"
rm -rf "$${TEMP_DIR}" rm -rf "$${TEMP_DIR}"
return 1 return 1