Skip to content

Instantly share code, notes, and snippets.

@l3laze
Created June 23, 2021 02:33
Show Gist options
  • Save l3laze/e3e46353c36ff23b36fdf6a38fb75ab2 to your computer and use it in GitHub Desktop.
Save l3laze/e3e46353c36ff23b36fdf6a38fb75ab2 to your computer and use it in GitHub Desktop.
Bash script to find url of latest GitHub Releases.
#!/usr/bin/env bash
github_latest () {
local usage="Usage: $(basename $0 | sed -E 's/\.sh//') [options] <owner> <repo>
Options:
-l | --like = Download release with name that contains string, e.g. amd64. *not a regex*"
local url
local dl_url
local data
local like
if [[ "$#" -lt 2 ]]; then
echo "$usage"
exit 1
fi
while [[ "$#" -gt 2 ]]; do
case "$1" in
help|?|-h|--help)
echo "$usage"
exit
;;
-l|--like)
if [[ "$#" -ge 2 ]]; then
like="$2"
shift; shift
else
echo "$usage"
exit 1
fi
;;
*)
echo "$usage"
exit 1
esac
done
url="https://api.github.com/repos/$1/$2/releases/latest"
data="$(curl -fsSL $url |
grep "_url.*$1/$2.*\(/download/\|/tarball\|/zipball\)" |
sed -E 's/.*\: "//' |
tr -d '",'
)"
# echo "Data='
#$data
#'"
if [[ "$like" != "" ]]; then
dl_url="$(echo ${data} | tr ' ' '\n' | grep $like)"
else
dl_url="$(echo ${data} |
grep -i "[$(uname)|$(arch)]" |
sed -E 's/\ .*//')"
if [[ "$dl_url" == "" ]]; then
dl_url="$(echo ${data} |
sed -E 's/\ .*//')"
fi
fi
# echo "Download URL='
#$dl_url
#'"
echo "$dl_url"
}
github_latest "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment