From 977e4a98fab19efaf0a8f546a1695e0432b26491 Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Thu, 20 Oct 2022 14:51:58 +0200 Subject: [PATCH] Update bin/fetch-configlet script The script has been tweaked to work better across all platforms. --- bin/fetch-configlet | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 bin/fetch-configlet diff --git a/bin/fetch-configlet b/bin/fetch-configlet new file mode 100755 index 0000000000..827088ab17 --- /dev/null +++ b/bin/fetch-configlet @@ -0,0 +1,74 @@ +#!/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 + +readonly LATEST='https://api.github.com/repos/exercism/configlet/releases/latest' + +case "$(uname)" in + Darwin*) os='mac' ;; + Linux*) os='linux' ;; + Windows*) os='windows' ;; + MINGW*) os='windows' ;; + MSYS_NT-*) os='windows' ;; + *) os='linux' ;; +esac + +case "${os}" in + windows*) ext='zip' ;; + *) ext='tgz' ;; +esac + +case "$(uname -m)" in + *64*) arch='64bit' ;; + *686*) arch='32bit' ;; + *386*) arch='32bit' ;; + *) arch='64bit' ;; +esac + +curlopts=( + --silent + --show-error + --fail + --location + --retry 3 +) + +if [[ -n "${GITHUB_TOKEN}" ]]; then + curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}") +fi + +suffix="${os}-${arch}.${ext}" + +get_download_url() { + curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" | + grep "\"browser_download_url\": \".*/download/.*/configlet.*${suffix}\"$" | + cut -d'"' -f4 +} + +main() { + 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 + + download_url="$(get_download_url)" + output_path="${output_dir}/latest-configlet.${ext}" + curl "${curlopts[@]}" --output "${output_path}" "${download_url}" + + case "${ext}" in + *zip) unzip "${output_path}" -d "${output_dir}" ;; + *) tar xzf "${output_path}" -C "${output_dir}" ;; + esac + + rm -f "${output_path}" +} + +main