Skip to content

Commit

Permalink
fetch-configlet: make some variables local
Browse files Browse the repository at this point in the history
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

exercism/configlet#691
  • Loading branch information
ee7 authored and petertseng committed Nov 17, 2022
1 parent db945bd commit 0aaa960
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions bin/fetch-configlet
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}"
}
Expand Down

0 comments on commit 0aaa960

Please sign in to comment.