Skip to content

Commit ad4a54b

Browse files
authored
bin/fetch-configlet: sync with upstream (#2259)
The fetch-configlet script in this repo was outdated. Update it to the upstream version [1], which has many improvements. Closes: #2258 [1] https://github.com/exercism/configlet/blob/85745ac34f7c/scripts/fetch-configlet
1 parent 7319a5e commit ad4a54b

File tree

1 file changed

+81
-43
lines changed

1 file changed

+81
-43
lines changed

bin/fetch-configlet

Lines changed: 81 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,90 @@
11
#!/usr/bin/env bash
22

3+
# This file is a copy of the
4+
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file.
5+
# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes.
6+
37
set -eo pipefail
48

5-
readonly LATEST='https://api.github.com/repos/exercism/configlet/releases/latest'
6-
7-
case "$(uname)" in
8-
(Darwin*) OS='mac' ;;
9-
(Linux*) OS='linux' ;;
10-
(Windows*) OS='windows' ;;
11-
(MINGW*) OS='windows' ;;
12-
(MSYS_NT-*) OS='windows' ;;
13-
(*) OS='linux' ;;
14-
esac
15-
16-
case "$OS" in
17-
(windows*) EXT='zip' ;;
18-
(*) EXT='tgz' ;;
19-
esac
20-
21-
case "$(uname -m)" in
22-
(*64*) ARCH='64bit' ;;
23-
(*686*) ARCH='32bit' ;;
24-
(*386*) ARCH='32bit' ;;
25-
(*) ARCH='64bit' ;;
26-
esac
27-
28-
if [ -z "${GITHUB_TOKEN}" ]
29-
then
30-
HEADER=''
31-
else
32-
HEADER="authorization: Bearer ${GITHUB_TOKEN}"
33-
fi
9+
curlopts=(
10+
--silent
11+
--show-error
12+
--fail
13+
--location
14+
--retry 3
15+
)
3416

35-
FILENAME="configlet-${OS}-${ARCH}.${EXT}"
17+
if [[ -n "${GITHUB_TOKEN}" ]]; then
18+
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
19+
fi
3620

37-
get_url () {
38-
curl --header "$HEADER" -s "$LATEST" |
39-
awk -v filename=$FILENAME '$1 ~ /browser_download_url/ && $2 ~ filename { print $2 }' |
40-
tr -d '"'
21+
get_download_url() {
22+
local os="$1"
23+
local ext="$2"
24+
local latest='https://api.github.com/repos/exercism/configlet/releases/latest'
25+
local arch
26+
case "$(uname -m)" in
27+
x86_64) arch='x86-64' ;;
28+
*686*) arch='i386' ;;
29+
*386*) arch='i386' ;;
30+
*) arch='x86-64' ;;
31+
esac
32+
local suffix="${os}_${arch}.${ext}"
33+
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${latest}" |
34+
grep "\"browser_download_url\": \".*/download/.*/configlet.*${suffix}\"$" |
35+
cut -d'"' -f4
4136
}
4237

43-
URL=$(get_url)
38+
main() {
39+
local output_dir
40+
if [[ -d ./bin ]]; then
41+
output_dir="./bin"
42+
elif [[ $PWD == */bin ]]; then
43+
output_dir="$PWD"
44+
else
45+
echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2
46+
return 1
47+
fi
48+
49+
local os
50+
case "$(uname)" in
51+
Darwin*) os='macos' ;;
52+
Linux*) os='linux' ;;
53+
Windows*) os='windows' ;;
54+
MINGW*) os='windows' ;;
55+
MSYS_NT-*) os='windows' ;;
56+
*) os='linux' ;;
57+
esac
58+
59+
local ext
60+
case "${os}" in
61+
windows*) ext='zip' ;;
62+
*) ext='tar.gz' ;;
63+
esac
64+
65+
echo "Fetching configlet..." >&2
66+
local download_url
67+
download_url="$(get_download_url "${os}" "${ext}")"
68+
local output_path="${output_dir}/latest-configlet.${ext}"
69+
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
70+
71+
case "${ext}" in
72+
*zip) unzip "${output_path}" -d "${output_dir}" ;;
73+
*) tar xzf "${output_path}" -C "${output_dir}" ;;
74+
esac
75+
76+
rm -f "${output_path}"
77+
78+
local executable_ext
79+
case "${os}" in
80+
windows*) executable_ext='.exe' ;;
81+
*) executable_ext='' ;;
82+
esac
83+
84+
local configlet_path="${output_dir}/configlet${executable_ext}"
85+
local configlet_version
86+
configlet_version="$(${configlet_path} --version)"
87+
echo "Downloaded configlet ${configlet_version} to ${configlet_path}"
88+
}
4489

45-
case "$EXT" in
46-
(*zip)
47-
curl --header "$HEADER" -s --location "$URL" -o bin/latest-configlet.zip
48-
unzip bin/latest-configlet.zip -d bin/
49-
rm bin/latest-configlet.zip
50-
;;
51-
(*) curl --header "$HEADER" -s --location "$URL" | tar xz -C bin/ ;;
52-
esac
90+
main

0 commit comments

Comments
 (0)