| @@ -0,0 +1,51 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | ||
| 3 | + | # usage | |
| 4 | + | # downloadgithubasset.sh mozilla/sops | |
| 5 | + | # downloadgithubasset.sh mozilla/sops 3.4.0 | |
| 6 | + | ||
| 7 | + | if ! [ -x "$(command -v jq)" ]; then | |
| 8 | + | echo 'Error: jq is not installed.' >&2 | |
| 9 | + | exit 1 | |
| 10 | + | fi | |
| 11 | + | ||
| 12 | + | if [ -z "$1" ]; then | |
| 13 | + | echo 'No argument supplied, you must specify a github repo' >&2 | |
| 14 | + | exit 1 | |
| 15 | + | fi | |
| 16 | + | ||
| 17 | + | release=${2:-latest} | |
| 18 | + | ||
| 19 | + | if [ "$release" == "latest" ]; then | |
| 20 | + | url="https://api.github.com/repos/$1/releases/$release" | |
| 21 | + | else | |
| 22 | + | url="https://api.github.com/repos/$1/releases/tags/$release" | |
| 23 | + | fi | |
| 24 | + | ||
| 25 | + | details=$(curl -s "$url") | |
| 26 | + | ||
| 27 | + | tagname=$(echo "$details" | jq -r '.tag_name') | |
| 28 | + | ||
| 29 | + | if [ "$tagname" == "null" ]; then | |
| 30 | + | echo 'bad URL' >&2 | |
| 31 | + | exit 1 | |
| 32 | + | fi | |
| 33 | + | ||
| 34 | + | echo "tag: $tagname" | |
| 35 | + | ||
| 36 | + | # -c for multi line compressed output | |
| 37 | + | # -r for raw output | |
| 38 | + | ||
| 39 | + | assets=$(echo "$details" | jq -c '.assets[]') | |
| 40 | + | ||
| 41 | + | names=($(echo "$assets" | jq -cr '.name')) | |
| 42 | + | ||
| 43 | + | downloads=($(echo "$assets" | jq -cr '.browser_download_url')) | |
| 44 | + | ||
| 45 | + | select name in "${names[@]}"; do | |
| 46 | + | if [[ ! -z "$name" ]]; then | |
| 47 | + | download=$((REPLY - 1)) | |
| 48 | + | wget "${downloads[download]}" | |
| 49 | + | fi | |
| 50 | + | break | |
| 51 | + | done | |
willpower232 / downloadgithubasset.sh
Last active 1 month ago
Interactive terminal menu to download an asset from a known github repo release
Will Power revised this gist 6 years ago · dc4cb1a
1 file changed, 51 insertions