Skip to content

Instantly share code, notes, and snippets.

@MineBartekSA
Last active January 19, 2023 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MineBartekSA/5234f09c1048f9ba007d0a26a8fd654e to your computer and use it in GitHub Desktop.
Save MineBartekSA/5234f09c1048f9ba007d0a26a8fd654e to your computer and use it in GitHub Desktop.
github-release - A simple github release asset downloader
#!/bin/bash
#
# github-release v1.0.0
# A simple github release asset downloader
# Author: MineBartekSA
# Gist: https://gist.github.com/MineBartekSA/5234f09c1048f9ba007d0a26a8fd654e
#
function help() {
echo "Usage: $0 <repository> [options]"
echo "Options:"
echo " -t, --tag <release tag> - Set the release tag"
echo " -f, --filter <filter> - Add a asset filter"
echo " -o, --output <name> - Set the output name"
echo " -a, --auth <secret> - Authenticate using OAuth2 key/secret or personal access token"
echo " -d, --dry - Will only print asset url(s)"
echo " -s, --silent - Disable output"
echo " -v, --version - See app version"
echo " -h, --help - See this message"
echo
echo "Example:"
echo " $0 example/repo -v v1.1.3 -f linux -f gnu -f .tar"
}
if [ $# -lt 1 ]
then
exec 1>&2
help
exit 2
fi
case $1 in
-v | --version)
echo "github-release v1.0.0"
exit 0
;;
-h | --help)
help
exit 0
;;
esac
curl --version >> /dev/null
if [ $? -ne 0 ]
then
echo -e "\e[91mcURL not found!\e[0m" >&2
echo "Please check if you have installed cURL on your system" >&2
exit 1
fi
jq --version >> /dev/null
if [ $? -ne 0 ]
then
echo -e "\e[91mjq not found!\e[0m" >&2
echo "Please check if you have installed jq on your system" >&2
exit 1
fi
repo=$1
tag="latest"
output=""
curl_add=""
dry=0
silent=0
to_filter=()
declare -i count=2
while [ $count -le $# ]
do
case ${!count} in
-t | --tag)
count+=1
tag=${!count}
;;
-f | --filter)
count+=1
to_filter+=(${!count})
;;
-o | --output)
count+=1
output=${!count}
;;
-a | --auth)
count+=1
curl_add+="-u ${!count}"
;;
-d | --dry)
dry=1
;;
-d | --silent)
silent=1
;;
esac
count+=1
done
filter=".assets[] | "
for f in ${to_filter[*]}
do
filter+="select(.name | contains(\"$f\")) | "
done
filter+=".browser_download_url"
[ "$tag" != "latest" ] && tag="tags/$tag"
data=$(curl -sSL --fail-with-body $curl_add "https://api.github.com/repos/$repo/releases/$tag")
if [ $? -ne 0 ]
then
echo "Failed to fetch release information" >&2
echo $data | jq . >&2
exit 3
fi
read -r -d '' -a urls <<< $(echo $data | jq -r "$filter")
if [ $dry -eq 1 ]
then
IFS=$'\n'
echo "${urls[*]}"
exit 0
fi
if [ ${#urls[@]} -eq 0 ]
then
[ $silent -eq 0 ] && echo "Nothing to download"
exit 0
fi
[ $silent -eq 0 ] && echo "Downloading"
if [ "$output" == "" ]
then
for url in ${urls[@]}
do
filename=${url##*/}
[ $silent -eq 0 ] && echo -e "\e[1m$filename\e[0m: $url"
curl -sSL --fail-with-body $url -o $filename
done
exit 0
fi
function extension {
ext=${1##*.}
if [ "$ext" == "gz" ]
then
isTar=${1##*.tar.}
[ "$isTar" == "gz" ] && ext="tar.gz"
fi
printf $ext
}
if [ ${#urls[@]} -eq 1 ]
then
url=${urls[0]}
ext=$(extension ${url##*/})
out=$output.$ext
[ $silent -eq 0 ] && echo -e "\e[1m$out\e[0m: $url"
curl -sSL $url -o $out
exit 0
fi
for index in ${!urls[@]}
do
url=${urls[$index]}
ext=$(extension ${url##*/})
out=$output-$[$index + 1].$ext
[ $silent -eq 0 ] && echo -e "\e[1m$out\e[0m: $url"
curl -sSL --fail-with-body $url -o $out
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment