Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active May 2, 2024 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/cc3163147b5f838f1582c364d8e33087 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/cc3163147b5f838f1582c364d8e33087 to your computer and use it in GitHub Desktop.
Download latest GitHub releases by user, repo, and extension
#!/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
@pythoninthegrass
Copy link
Author

git clone git@gist.github.com:cc3163147b5f838f1582c364d8e33087.git dl-gh
cd dl-gh/
ln -s $(pwd)/dl-gh ~/.local/bin/dl-gh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment