Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

godownloader refactor for allow using a GH Token, other improvements #1673

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions godownloader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ usage() {
cat <<EOF
$this: download go binaries for astronomer/astro-cli

Usage: $this [-b] bindir [-d] [tag]
Usage: $this [-b] bindir [-d] [tag] [-t] token
-b sets bindir or installation directory, Defaults to ./bin
-d turns on debug logging
[tag] is a tag from
https://github.com/astronomer/astro-cli/releases
If tag is missing, then the latest will be used.
-t allows use of a GitHub Token to make Authenticated calls (with higher rate limts) to the GH API

Generated by godownloader
https://github.com/goreleaser/godownloader
Expand All @@ -28,10 +29,11 @@ parse_args() {
# over-ridden by flag below

BINDIR=${BINDIR:-./bin}
while getopts "b:dh?x" arg; do
while getopts "b:t:dh?x" arg; do
case "$arg" in
b) BINDIR="$OPTARG" ;;
d) log_set_priority 10 ;;
t) TOKEN="$OPTARG" ;;
h | \?) usage "$0" ;;
x) set -x ;;
esac
Expand Down Expand Up @@ -93,7 +95,7 @@ version_gte() {
fi
}
tag_to_version() {
if [ -z "${TAG}" ]; then
if [ -z "${TAG}" ] || [ $TAG == $LATEST_VERSION ]; then
log_info "Using Latest($LATEST_VERSION) version"
TAG="$LATEST_VERSION"
else
Expand Down Expand Up @@ -302,11 +304,20 @@ http_copy() {
github_release() {
owner_repo=$1
version=$2
if [ -z $version ]; then
version=$(curl https://api.github.com/repos/${owner_repo}/releases/latest -s | tr -s '\n' ' ' | sed 's/.*"tag_name": "//' | sed 's/".*//')
url=https://api.github.com/repos/${owner_repo}/releases
if [ -z $TOKEN ]; then
IFS=$'\n' read -r -d '' code body < <(curl -s -w "%{http_code}\n" -o \
>(IFS= read -r -d '' -u0 stdin; printf "%s" "$stdin") $url)
else
escaped_version=$(echo $version | sed -e 's/[]\/$*.^[]/\\&/g') # escape the version string
version=$(curl https://api.github.com/repos/${owner_repo}/releases -s | grep ''\"$escaped_version\.\*\"'' -m1 | sed 's/.*"tag_name": "//' | sed 's/".*//')
IFS=$'\n' read -r -d '' code body < <(curl -s -w "%{http_code}\n" -o \
>(IFS= read -r -d '' -u0 stdin; printf "%s" "$stdin") -H "Authorization: Bearer $TOKEN" $url)
fi
escaped_version=$(echo $version | sed -e 's/[]\/$*.^[]/\\&/g') # escape the version string
version=$(echo "$body" | grep ''\"$escaped_version\.\*\"'' -m1 | sed 's/.*"tag_name": "//' | sed 's/".*//')
if [ "$code" == "200" ]; then
log_debug "HTTP Status Code 200"
else
log_err "HTTP Status Code $code: $body"
fi
test -z "$version" && return 1
echo "$version"
Expand Down