From 6723fcedc5712c553b840d1d46552e7b757c3bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20HOUZ=C3=89?= Date: Thu, 28 Nov 2019 17:22:52 +0100 Subject: [PATCH] feat: manage github auth to extend rate api limit --- install-hub.sh | 25 ++++++++++--------------- install.sh | 16 ++++++++++------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/install-hub.sh b/install-hub.sh index 4f78d5c..c388d54 100755 --- a/install-hub.sh +++ b/install-hub.sh @@ -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) @@ -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 } @@ -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 } @@ -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) diff --git a/install.sh b/install.sh index e81b404..5914e75 100755 --- a/install.sh +++ b/install.sh @@ -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) @@ -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 @@ -94,11 +98,11 @@ 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" @@ -106,8 +110,8 @@ downloadFile() { 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