Created
August 28, 2019 12:24
-
-
Save snoby/b58613273ef87b6a980437019778096f to your computer and use it in GitHub Desktop.
Download from enterprise github behind Oauth redirect.
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
#!/usr/bin/env bash | |
# | |
# gh-dl-release! It works! | |
# | |
# This script downloads an asset from latest or specific Github release of a | |
# private repo. Feel free to extract more of the variables into command line | |
# parameters. | |
# | |
# PREREQUISITES | |
# | |
# curl, jq | |
# | |
# If your version/tag doesn't match, the script will exit with error. | |
# if the file you specify isn't there it will break as well | |
# example cmd line (with fake token): | |
# ./gh-dl-release -t 7e10abdcd7684c27b3c470e35a02a7100llcb0126a7 -r Platform-Common/dummie -v 2.0.1.0 -f dumpy-2.0.1.0.tgz -o $PWD/ | |
# | |
set -e | |
usage() { echo "Usage: $0 -t <github token> -r <repo> -v <version> -f <file> -o <output path> -q" 1>&2; exit 1; } | |
while getopts "qt:v:r:f:o:" o; do | |
case "${o}" in | |
t) | |
TOKEN=${OPTARG} | |
;; | |
v) | |
VERSION=${OPTARG} | |
;; | |
r) | |
REPO=${OPTARG} | |
;; | |
f) | |
FILE=${OPTARG} | |
;; | |
o) | |
OUTPUT=${OPTARG} | |
;; | |
q) | |
QUIET=1 | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "$OUTPUT" ]; then | |
OUTPUT=$FILE | |
fi | |
if [ -z "$TOKEN" ] || [ -z "$VERSION" ] || [ -z "$REPO" ] || [ -z "$FILE" ] || [ -z "$OUTPUT" ]; then | |
usage | |
fi | |
GITHUB="https://sqbu-github.cisco.com"; | |
alias errcho='>&2 echo' | |
gh_curl_list() { | |
curl -s -H "Authorization: token $TOKEN" "$GITHUB/api/v3/repos/$REPO/releases" | |
} | |
version_exists() { | |
jq ". | map(select(.tag_name == \"$VERSION\")) | length" | |
} | |
maybe_echo() { | |
if [ -z "$QUIET" ]; then | |
echo "$@" | |
fi | |
} | |
echo_error() { | |
>&2 maybe_echo "ERROR: $@" | |
} | |
echo_info() { | |
maybe_echo "INFO: $@" | |
} | |
echo_success() { | |
maybe_echo SUCCESS: $@ | |
} | |
if [ "$VERSION" = "latest" ]; then | |
# Github should return the latest release first. | |
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id" | |
else | |
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id" | |
fi | |
RESPONSE=$(gh_curl_list) | |
if [ "$(echo $RESPONSE | version_exists)" = "0" ]; then | |
echo_error "version '$VERSION' not found" | |
exit 1 | |
else | |
echo_info "Found version '$VERSION'" | |
echo $RESPONSE > /tmp/response.json | |
fi | |
ASSET_ID=$(echo "$RESPONSE" | jq "$parser") | |
if [ "$ASSET_ID" = "null" ]; then | |
echo_error "Could not find asset '$FILE'" | |
exit 1 | |
else | |
echo_info "Found asset '$FILE' id is $ASSET_ID" | |
fi | |
echo -e "Attempting to download file asset: \n$GITHUB/api/v3/repos/$REPO/releases/assets/$ASSET_ID \nto \n$OUTPUT/$FILE\n" | |
curl -# -L -H "Authorization: token $TOKEN" -H "Accept: application/octet-stream" \ | |
"$GITHUB/api/v3/repos/$REPO/releases/assets/$ASSET_ID" \ | |
--output "$OUTPUT/$FILE" | |
if [ 0 -eq $? ]; then | |
echo "Downloaded file successfully" | |
else | |
echo "Curl returned an error downloading file" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment