2022-06-29 07:44:09 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
# This file is a copy of the
|
|
|
|
|
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file.
|
|
|
|
|
# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes.
|
|
|
|
|
|
|
|
|
|
set -eo pipefail
|
|
|
|
|
|
|
|
|
|
curlopts=(
|
|
|
|
|
--silent
|
|
|
|
|
--show-error
|
|
|
|
|
--fail
|
|
|
|
|
--location
|
|
|
|
|
--retry 3
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if [[ -n "${GITHUB_TOKEN}" ]]; then
|
|
|
|
|
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
get_download_url() {
|
2022-11-17 16:00:20 +00:00
|
|
|
local os="$1"
|
|
|
|
|
local ext="$2"
|
|
|
|
|
local latest='https://api.github.com/repos/exercism/configlet/releases/latest'
|
|
|
|
|
local arch
|
|
|
|
|
case "$(uname -m)" in
|
2024-07-10 15:55:28 +01:00
|
|
|
aarch64|arm64) arch='arm64' ;;
|
|
|
|
|
x86_64) arch='x86-64' ;;
|
|
|
|
|
*686*) arch='i386' ;;
|
|
|
|
|
*386*) arch='i386' ;;
|
|
|
|
|
*) arch='x86-64' ;;
|
2022-11-17 16:00:20 +00:00
|
|
|
esac
|
|
|
|
|
local suffix="${os}_${arch}.${ext}"
|
|
|
|
|
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${latest}" |
|
2022-06-29 07:44:09 +02:00
|
|
|
grep "\"browser_download_url\": \".*/download/.*/configlet.*${suffix}\"$" |
|
|
|
|
|
cut -d'"' -f4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
2022-11-17 16:00:20 +00:00
|
|
|
local output_dir
|
2022-06-29 07:44:09 +02:00
|
|
|
if [[ -d ./bin ]]; then
|
|
|
|
|
output_dir="./bin"
|
|
|
|
|
elif [[ $PWD == */bin ]]; then
|
|
|
|
|
output_dir="$PWD"
|
|
|
|
|
else
|
|
|
|
|
echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
2022-11-17 16:00:20 +00:00
|
|
|
local os
|
2024-07-10 15:55:28 +01:00
|
|
|
case "$(uname -s)" in
|
2022-11-17 16:00:20 +00:00
|
|
|
Darwin*) os='macos' ;;
|
|
|
|
|
Linux*) os='linux' ;;
|
|
|
|
|
Windows*) os='windows' ;;
|
|
|
|
|
MINGW*) os='windows' ;;
|
|
|
|
|
MSYS_NT-*) os='windows' ;;
|
|
|
|
|
*) os='linux' ;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
local ext
|
|
|
|
|
case "${os}" in
|
2024-07-10 15:55:28 +01:00
|
|
|
windows) ext='zip' ;;
|
|
|
|
|
*) ext='tar.gz' ;;
|
2022-11-17 16:00:20 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
echo "Fetching configlet..." >&2
|
|
|
|
|
local download_url
|
|
|
|
|
download_url="$(get_download_url "${os}" "${ext}")"
|
|
|
|
|
local output_path="${output_dir}/latest-configlet.${ext}"
|
2022-06-29 07:44:09 +02:00
|
|
|
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
|
|
|
|
|
|
|
|
|
|
case "${ext}" in
|
2024-07-10 15:55:28 +01:00
|
|
|
zip) unzip "${output_path}" -d "${output_dir}" ;;
|
|
|
|
|
*) tar xzf "${output_path}" -C "${output_dir}" ;;
|
2022-06-29 07:44:09 +02:00
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
rm -f "${output_path}"
|
2022-11-17 16:00:20 +00:00
|
|
|
|
|
|
|
|
local executable_ext
|
|
|
|
|
case "${os}" in
|
2024-07-10 15:55:28 +01:00
|
|
|
windows) executable_ext='.exe' ;;
|
|
|
|
|
*) executable_ext='' ;;
|
2022-11-17 16:00:20 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
local configlet_path="${output_dir}/configlet${executable_ext}"
|
|
|
|
|
local configlet_version
|
|
|
|
|
configlet_version="$(${configlet_path} --version)"
|
|
|
|
|
echo "Downloaded configlet ${configlet_version} to ${configlet_path}"
|
2022-06-29 07:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main
|