Add missing `_download_from()` function.

This commit is contained in:
William Melody 2020-06-07 17:15:51 -07:00
parent 0715b9ec66
commit 2346f6f814
1 changed files with 48 additions and 0 deletions

48
hosts
View File

@ -218,6 +218,54 @@ _contains() {
return 1
}
# _download_from()
#
# Usage:
# _download_from <url> [<outfile>]
#
# Description:
# Download the file at <url> and print to standard output or <outfile>, if
# present. Uses `curl` if available, falling back to `wget`.
_download_from() {
local _downloaded=0
local _target_path="${2:-}"
local _url="${1:-}"
if [[ -z "${_url}" ]] ||
[[ ! "${_url}" =~ ^ftp|^http|^file|^mailto|^news|^telnet|^gopher ]]
then
return 1
fi
if [[ -n "${_target_path}" ]]
then
if hash "curl" 2>/dev/null
then
curl -s -L "${_url}" -o "${_target_path}" &&
_downloaded=1
elif hash "wget" 2>/dev/null
then
wget --quiet -O "${_target_path}" "${_url}" 2>/dev/null &&
_downloaded=1
fi
else
if hash "curl" 2>/dev/null
then
curl -s -L "${_url}" &&
_downloaded=1
elif hash "wget" 2>/dev/null
then
wget --quiet -O - "${_url}" 2>/dev/null &&
_downloaded=1
fi
fi
if ! ((_downloaded))
then
return 1
fi
}
# _join()
#
# Usage: