Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interactive terminal menu to download an asset from a known github repo release
#!/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