From 2665ca0972b1f3f01b778e9361d59141fc7f86d4 Mon Sep 17 00:00:00 2001 From: Jubilee <46493976+workingjubilee@users.noreply.github.com> Date: Thu, 19 Mar 2020 14:37:29 -0700 Subject: [PATCH] Fix fetch-configlet script (#438) * Fix fetch-configlet script Shamelessly dupes @coriolinus' fix: https://github.com/exercism/rust/pull/929 "Looks like Github has recently started returning non-uppercased HTTP headers, at least some of the time. This broke the script, which looked for a case-sensitive 'Location' header to find the newest version. We can see this problem in spurious build failures like this." "This fixes the script such that it no longer cares whether the initial L is capital or not, and if it breaks again in the future, it will give a more informative error." * Add retry to fetch-configlet Riffing off of @glennj's fix https://github.com/exercism/bash/pull/424 This fixes the problem by adding retries to the curl attempt, combined with the case-sensitivity fix. Also some of the logic has been reflowed. Most notably, we now dynamically check for what archive extension to use. --- bin/fetch-configlet | 73 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 15 deletions(-) diff --git a/bin/fetch-configlet b/bin/fetch-configlet index 7e51d1068..9920f9d7c 100755 --- a/bin/fetch-configlet +++ b/bin/fetch-configlet @@ -1,18 +1,19 @@ #!/usr/bin/env bash -LATEST=https://github.com/exercism/configlet/releases/latest +set -eo pipefail -OS=$( -case $(uname) in - (Darwin*) - echo "mac";; - (Linux*) - echo "linux";; - (Windows*) - echo "windows";; - (*) - echo "linux";; -esac) +readonly RELEASES='https://github.com/exercism/configlet/releases' + +get_version () { + curl --silent --head "${RELEASES}/latest" | + awk -v FS=/ -v RS='\r\n' 'tolower($1) ~ /location:/ {print $NF}' +} + +VERSION="$(get_version)" +if [ -z "$VERSION" ]; then + echo "Latest configlet release could not be determined; aborting" + exit 1 +fi ARCH=$( case $(uname -m) in @@ -26,7 +27,49 @@ case $(uname -m) in echo 64bit;; esac) -VERSION="$(curl --head --silent $LATEST | awk -v FS=/ '/Location:/{print $NF}' | tr -d '\r')" -URL=https://github.com/exercism/configlet/releases/download/$VERSION/configlet-$OS-${ARCH}.tgz +OS=$( +case $(uname) in + (Darwin*) + echo "mac";; + (Linux*) + echo "linux";; + (Windows*) + echo "windows";; + (*) + echo "linux";; +esac) + +case "$OS" in + (windows*) EXT='zip' ;; + (*) EXT='tgz' ;; +esac + +URL="${RELEASES}/download/$VERSION/configlet-$OS-${ARCH}.${EXT}" +localfile=bin/latest-configlet.${EXT} + +fetch_archive() { + local file_info=$1 + for delay in {0..4}; do + sleep "$delay" + curl --silent --location "$URL" -o "$localfile" + if file "$localfile" 2>&1 | grep -q "$file_info"; then + # fetched successfully + return 0 + fi + done + echo "Cannot fetch $URL" >&2 + return 1 +} + +case "$EXT" in + zip) + fetch_archive "Zip archive data" + unzip "$localfile" -d bin/ + ;; + tgz) + fetch_archive "gzip compressed data" + tar xzf "$localfile" -C bin/ + ;; +esac -curl -s --location "$URL" | tar xz -C bin/ +rm -f "$localfile"