Last active
May 2, 2024 18:41
-
-
Save pythoninthegrass/cc3163147b5f838f1582c364d8e33087 to your computer and use it in GitHub Desktop.
Download latest GitHub releases by user, repo, and extension
This file contains 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
#!/usr/bin/env bash | |
# shellcheck disable=SC2207 | |
base_url="https://github.com/$user_name/$repo_name/releases" | |
pkg_path=$(pwd) | |
distro=$(uname) # Linux / Darwin | |
arch=$(uname -m) # x86_64 / aarch64 / arm64 | |
help() { | |
echo -e "USAGE\n\t$(basename $0) <options>" | |
echo -e "OPTIONS" | |
echo -e "\t-h --help\tShow this help message and exit" | |
echo -e "\t-u --user\tGithub user name" | |
echo -e "\t-r --repo\tGithub repo name" | |
echo -e "\t-t --type\tPackage type (deb, rpm, tar.gz)" | |
} | |
if [[ $# -eq 0 ]]; then | |
help | |
exit 1 | |
fi | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case $key in | |
-h|--help) | |
help | |
exit 0 | |
;; | |
-u|--user) | |
user_name="$2" | |
shift | |
shift | |
;; | |
-r|--repo) | |
repo_name="$2" | |
shift | |
shift | |
;; | |
-t|--type) | |
pkg_type="$2" | |
shift | |
shift | |
;; | |
*) | |
help | |
;; | |
esac | |
done | |
latest=$(curl -s "$base_url" | awk -F '[<>]' "/\/${user_name}\/${repo_name}\/releases\/tag\// {print \$5}" | head -n 1) | |
pkg_urls=($(curl -s "https://api.github.com/repos/${user_name}/${repo_name}/releases/latest" \ | |
| awk "/browser_download_url/ && /${pkg_type}/ && /${latest}/ { print }" \ | |
| cut -d : -f 2,3 \ | |
| tr -d \" \ | |
| sed 's/^ *//g' | |
)) | |
check_pkg() { | |
if [[ -z "${pkg_urls[*]}" ]]; then | |
echo "No packages found for $pkg_type" | |
exit 1 | |
fi | |
if [[ "${#pkg_urls[@]}" -gt 1 ]]; then | |
pkg_url=$(echo "${pkg_urls[@]}" | tr ' ' '\n' | grep -iE "${distro}|${arch}") | |
[[ -z "$pkg_url" ]] && echo "No packages found for $distro/$arch"; exit 1 | |
fi | |
} | |
dl_pkg() { | |
curl -s -L -o "$1" "$2" | |
} | |
main() { | |
check_pkg | |
local pkg_name="${pkg_url##*/}" | |
local output="${pkg_path}/${pkg_name}" | |
dl_pkg "$output" "$pkg_url" | |
echo "$pkg_name downloaded to $pkg_path" | |
} | |
main "$@" | |
exit 0 |
Author
pythoninthegrass
commented
Dec 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment