From 2346f6f814f989a5c5dd8ef33d2ab2d0bacfec51 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sun, 7 Jun 2020 17:15:51 -0700 Subject: [PATCH] Add missing `_download_from()` function. --- hosts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/hosts b/hosts index f5a5355..4471c1d 100755 --- a/hosts +++ b/hosts @@ -218,6 +218,54 @@ _contains() { return 1 } +# _download_from() +# +# Usage: +# _download_from [] +# +# Description: +# Download the file at and print to standard output or , 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: