1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#!/usr/bin/env bash
## Commands defined by this file:
#
# * download-file --uri=<uri> --out=<filepath> [--no-tls-verify]
# • Download the HTTP resource specified by <uri> and save the respond body
# to <filepath>. Follows redirects.
#
# If `--no-tls-verify` is provided, TLS certificate validation will be
# disabled.
. "$(dirname "${BASH_SOURCE[0]}")/use.sh" base
download-file() {
declare uri
declare out
unset no_tls_verify
while [[ "$#" != 0 ]]; do
case "$1" in
--uri)
shift
uri=$1
;;
--uri=*)
uri="${1#--uri=*}"
;;
--out)
shift
out=$1
;;
--out=*)
out=${1#--out=*}
;;
--no-tls-verify)
# shellcheck disable=2034
no_tls_verify=1
;;
*)
fail "Unknown argument: $1"
esac
shift
done
if ! is-set uri || ! is-set out; then
fail "download-file requires --uri=<uri> and --out=<filepath> arguments"
return
fi
debug "Download [$uri] to [$out]"
if have-command curl; then
curl_argv=(
--silent
--show-error
--fail
--retry 5
--max-time 120
--location # (Follow redirects)
--output "$out"
)
if is-set no_tls_verify; then
curl_argv+=(--insecure)
fi
curl_argv+=(-- "$uri")
debug "Execute curl command: [curl ${curl_argv[*]}]"
output=$(curl "${curl_argv[@]}") || fail "$output" || return
debug "$output"
elif have-command wget; then
wget_argv=(
--output-document="$out"
--tries=5
--timeout=120
)
if is-set no_tls_verify; then
wget_argv+=(--no-check-certificate)
fi
wget_argv+=(-- "$uri")
debug "Execute wget command: [wget ${wget_argv[*]}]"
output=$(wget "${wget_argv[@]}" 2>&1) || fail "wget failed: $output" || return
debug "$output"
else
fail "This script requires either curl or wget to be available" || return
fi
debug "Download [$uri] to [$out] - Done"
}
if is-main; then
download-file "$@"
fi
|