Skip to content

Instantly share code, notes, and snippets.

@willpower232
Created January 27, 2020 16:47
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 willpower232/950669f8933b56c7b23595ab611e1ee7 to your computer and use it in GitHub Desktop.
Save willpower232/950669f8933b56c7b23595ab611e1ee7 to your computer and use it in GitHub Desktop.
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