Skip to content

Commit

Permalink
feat: manage github auth to extend rate api limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien HOUZÉ committed Nov 28, 2019
1 parent a2b9de7 commit 6723fce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
25 changes: 10 additions & 15 deletions install-hub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ PROJECT_NAME="hub"
: ${USE_SUDO:="true"}
: ${HUB_INSTALL_DIR:="/usr/local"}

CURL_OPTS="-SsL"
if ! [[ -z "$GITHUB_USER" && -z "$GITHUB_TOKEN" ]] ; then
CURL_OPTS="$CURL_OPTS -u $GITHUB_USER:$GITHUB_TOKEN"
fi

# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
Expand Down Expand Up @@ -41,8 +46,8 @@ verifySupported() {
exit 1
fi

if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then
echo "Either curl or wget is required"
if ! type "curl" > /dev/null; then
echo "Either curl is required"
exit 1
fi
}
Expand All @@ -51,18 +56,12 @@ verifySupported() {
checkDesiredVersion() {
if [ "x$DESIRED_VERSION" == "x" ]; then
local latest_release_url="https://api.github.com/repos/github/hub/releases/latest"
if type "curl" > /dev/null; then
TAG=$(curl --silent $latest_release_url | grep "tag_name" | sed -E 's/.*"([^"]+)".*/\1/')
elif type "wget" > /dev/null; then
TAG=$(wget -O - $latest_release_url | grep "tag_name" | sed -E 's/.*"([^"]+)".*/\1/')
fi

TAG=$(curl $CURL_OPTS $latest_release_url | grep "tag_name" | sed -E 's/.*"([^"]+)".*/\1/')
VERSION=$(echo $TAG | sed 's/v//')
echo "Latest version $VERSION"
else
TAG="v$DESIRED_VERSION"
VERSION="$DESIRED_VERSION"

fi
}

Expand All @@ -87,17 +86,13 @@ checkHubInstalledVersion() {
# for that binary.
downloadFile() {
HUB_DIST="hub-$OS-$ARCH-$TAG.tgz"
DOWNLOAD_URL=$(curl -s https://api.github.com/repos/github/hub/releases/tags/$TAG | grep -E "browser_download_url\": \".+$OS-$ARCH.+\.tgz\"" | sed -E 's|.+(https://[^"]+).+|\1|')
DOWNLOAD_URL=$(curl $CURL_OPTS https://api.github.com/repos/github/hub/releases/tags/$TAG | grep -E "browser_download_url\": \".+$OS-$ARCH.+\.tgz\"" | sed -E 's|.+(https://[^"]+).+|\1|')

HUB_TMP_ROOT="$(mktemp -dt hub-installer-XXXXXX)"
HUB_TMP_FILE="$HUB_TMP_ROOT/$HUB_DIST"

echo "Downloading $DOWNLOAD_URL"
if type "curl" > /dev/null; then
curl -SsL "$DOWNLOAD_URL" -o "$HUB_TMP_FILE"
elif type "wget" > /dev/null; then
wget -q -O "$HUB_TMP_FILE" "$DOWNLOAD_URL"
fi
curl $CURL_OPTS "$DOWNLOAD_URL" -o "$HUB_TMP_FILE"
}

# runs the given command as root (detects if we are root already)
Expand Down
16 changes: 10 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/usr/bin/env bash

PROJECT_NAME="github"

: ${USE_SUDO:="true"}
: ${GH_CLI_INSTALL_DIR:="/usr/local/bin"}

CURL_OPTS="-SsL"
if ! [[ -z "$GITHUB_USER" && -z "$GITHUB_TOKEN" ]] ; then
CURL_OPTS="$CURL_OPTS -u $GITHUB_USER:$GITHUB_TOKEN"
fi

# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
Expand Down Expand Up @@ -67,7 +71,7 @@ checkDesiredVersion() {
if [ "x$DESIRED_VERSION" == "x" ]; then
# Get tag from release URL
local latest_release_url="https://github.com/inextensodigital/github/releases/latest"
TAG=$(curl -Ls -o /dev/null -w %{url_effective} $latest_release_url | grep -oE "[^/]+$" )
TAG=$(curl $CURL_OPTS -o /dev/null -w %{url_effective} $latest_release_url | grep -oE "[^/]+$" )
else
TAG=$DESIRED_VERSION
fi
Expand All @@ -94,20 +98,20 @@ checkGithubInstalledVersion() {
# for that binary.
downloadFile() {
DOWNLOAD_URL=$(
curl -s https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG |
curl $CURL_OPTS https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG |
jq -r '.assets[] | .browser_download_url' | grep -E "$OS-$ARCH(.exe)?\$"
)
CHECKSUM_URL=$(
curl -s https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG |
curl $CURL_OPTS https://api.github.com/repos/inextensodigital/github/releases/tags/$TAG |
jq -r '.assets[] | .browser_download_url' | grep -E "$OS-$ARCH(.exe)?\$"
)
CHECKSUM_URL="$DOWNLOAD_URL.sha256"
GH_CLI_TMP_ROOT="$(mktemp -dt github-installer-XXXXXX)"
GH_CLI_TMP_FILE="$GH_CLI_TMP_ROOT/$PROJECT_NAME"
GH_CLI_SUM_FILE="$GH_CLI_TMP_ROOT/$PROJECT_NAME.sha256"
echo "Downloading $DOWNLOAD_URL"
curl -SsL "$CHECKSUM_URL" -o "$GH_CLI_SUM_FILE"
curl -SsL "$DOWNLOAD_URL" -o "$GH_CLI_TMP_FILE"
curl $CURL_OPTS "$CHECKSUM_URL" -o "$GH_CLI_SUM_FILE"
curl $CURL_OPTS "$DOWNLOAD_URL" -o "$GH_CLI_TMP_FILE"
}

# installFile verifies the SHA256 for the file, then unpacks and
Expand Down

0 comments on commit 6723fce

Please sign in to comment.