Created
February 26, 2025 16:20
-
-
Save manix84/bc5fdf0dccae0eb9b91662c51eb89fd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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