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

Fix fetch-configlet script #438

Merged
merged 2 commits into from
Mar 19, 2020
Merged
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
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"