From 013c66b61cb1924005779a28c4bfc7123b539a17 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 31 Oct 2022 23:23:30 +0100 Subject: [PATCH] fetch-configlet: make some variables local (#691) From e.g. the Google Shell Style Guide [1]: Ensure that local variables are only seen inside a function and its children by using `local` when declaring them. This avoids polluting the global name space and inadvertently setting variables that may have significance outside the function. Declaration and assignment must be separate statements when the assignment value is provided by a command substitution; as the `local` builtin does not propagate the exit code from the command substitution. [1] https://google.github.io/styleguide/shellguide.html#use-local-variables --- scripts/fetch-configlet | 64 ++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/scripts/fetch-configlet b/scripts/fetch-configlet index 24363524..df2c0d90 100755 --- a/scripts/fetch-configlet +++ b/scripts/fetch-configlet @@ -6,29 +6,6 @@ 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 @@ -41,15 +18,25 @@ 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}" | + local os="$1" + local ext="$2" + local latest='https://api.github.com/repos/exercism/configlet/releases/latest' + local arch + case "$(uname -m)" in + *64*) arch='64bit' ;; + *686*) arch='32bit' ;; + *386*) arch='32bit' ;; + *) arch='64bit' ;; + esac + local suffix="${os}-${arch}.${ext}" + curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${latest}" | grep "\"browser_download_url\": \".*/download/.*/configlet.*${suffix}\"$" | cut -d'"' -f4 } main() { + local output_dir if [[ -d ./bin ]]; then output_dir="./bin" elif [[ $PWD == */bin ]]; then @@ -59,9 +46,26 @@ main() { return 1 fi + local os + case "$(uname)" in + Darwin*) os='mac' ;; + Linux*) os='linux' ;; + Windows*) os='windows' ;; + MINGW*) os='windows' ;; + MSYS_NT-*) os='windows' ;; + *) os='linux' ;; + esac + + local ext + case "${os}" in + windows*) ext='zip' ;; + *) ext='tgz' ;; + esac + echo "Fetching configlet..." >&2 - download_url="$(get_download_url)" - output_path="${output_dir}/latest-configlet.${ext}" + local download_url + download_url="$(get_download_url "${os}" "${ext}")" + local output_path="${output_dir}/latest-configlet.${ext}" curl "${curlopts[@]}" --output "${output_path}" "${download_url}" case "${ext}" in @@ -71,12 +75,14 @@ main() { rm -f "${output_path}" + local executable_ext case "${os}" in windows*) executable_ext='.exe' ;; *) executable_ext='' ;; esac - configlet_path="${output_dir}/configlet${executable_ext}" + local configlet_path="${output_dir}/configlet${executable_ext}" + local configlet_version configlet_version="$(${configlet_path} --version)" echo "Downloaded configlet ${configlet_version} to ${configlet_path}" }