Skip to content

Commit

Permalink
Fix fetch-configlet script (#438)
Browse files Browse the repository at this point in the history
* Fix fetch-configlet script

Shamelessly dupes @coriolinus' fix: exercism/rust#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 exercism/bash#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.
  • Loading branch information
workingjubilee authored Mar 19, 2020
1 parent 98541c1 commit 2665ca0
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions bin/fetch-configlet
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"

0 comments on commit 2665ca0

Please sign in to comment.