Interactive terminal menu to download an asset from a known github repo release
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
#!/bin/bash | |
# usage | |
# downloadgithubasset.sh mozilla/sops | |
# downloadgithubasset.sh mozilla/sops 3.4.0 | |
if ! [ -x "$(command -v jq)" ]; then | |
echo 'Error: jq is not installed.' >&2 | |
exit 1 | |
fi | |
if [ -z "$1" ]; then | |
echo 'No argument supplied, you must specify a github repo' >&2 | |
exit 1 | |
fi | |
release=${2:-latest} | |
if [ "$release" == "latest" ]; then | |
url="https://api.github.com/repos/$1/releases/$release" | |
else | |
url="https://api.github.com/repos/$1/releases/tags/$release" | |
fi | |
details=$(curl -s "$url") | |
tagname=$(echo "$details" | jq -r '.tag_name') | |
if [ "$tagname" == "null" ]; then | |
echo 'bad URL' >&2 | |
exit 1 | |
fi | |
echo "tag: $tagname" | |
# -c for multi line compressed output | |
# -r for raw output | |
assets=$(echo "$details" | jq -c '.assets[]') | |
names=($(echo "$assets" | jq -cr '.name')) | |
downloads=($(echo "$assets" | jq -cr '.browser_download_url')) | |
select name in "${names[@]}"; do | |
if [[ ! -z "$name" ]]; then | |
download=$((REPLY - 1)) | |
wget "${downloads[download]}" | |
fi | |
break | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment