Skip to content

Instantly share code, notes, and snippets.

@manix84
Created February 26, 2025 16:20
Show Gist options
  • Select an option

  • Save manix84/bc5fdf0dccae0eb9b91662c51eb89fd6 to your computer and use it in GitHub Desktop.

Select an option

Save manix84/bc5fdf0dccae0eb9b91662c51eb89fd6 to your computer and use it in GitHub Desktop.
file_downloader() {
local url="" output=""
local downloader=""
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
--output=*)
output="${1#--output=}"
;;
--output)
output="$2"
shift
;;
*)
url="$1"
;;
esac
shift
done
if [[ -z "$url" ]]; then
echo "Error: No URL provided." >&2
return 1
fi
# Determine available downloader
if command -v wget &>/dev/null; then
downloader="wget"
elif command -v curl &>/dev/null; then
downloader="curl"
elif command -v fetch &>/dev/null; then
downloader="fetch"
elif command -v aria2c &>/dev/null; then
downloader="aria2c"
elif command -v httpie &>/dev/null; then
downloader="http"
else
echo "Error: No supported download utility found (wget, curl, fetch, aria2c, httpie)." >&2
return 1
fi
# Download file based on the selected tool
if [[ -n "$output" ]]; then
case "$downloader" in
wget)
wget --output-document="$output" "$url"
;;
curl)
curl -o "$output" "$url"
;;
fetch)
fetch -o "$output" "$url"
;;
aria2c)
aria2c -o "$output" "$url"
;;
http)
http --download "$url" --output "$output"
;;
esac
else
case "$downloader" in
wget)
wget "$url"
;;
curl)
curl -O "$url"
;;
fetch)
fetch "$url"
;;
aria2c)
aria2c "$url"
;;
http)
http --download "$url"
;;
esac
fi
}
usage_example() {
# Absolute file location
file_downloader http://raw.github.com/caiogondim/bullet-train-oh-my-zsh-theme/master/bullet-train.zsh-theme --output=$ZSH_CUSTOM/themes/bullet-train.zsh-theme
# Relative file location
file_downloader http://raw.github.com/caiogondim/bullet-train-oh-my-zsh-theme/master/bullet-train.zsh-theme --output=./themes/bullet-train.zsh-theme
# No file location (download to current directory)
file_downloader http://raw.github.com/caiogondim/bullet-train-oh-my-zsh-theme/master/bullet-train.zsh-theme
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment