Skip to content

Commit 804caaa

Browse files
ee7petertseng
authored andcommitted
fetch-configlet: make some variables local
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
1 parent 6fa5c0f commit 804caaa

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

bin/fetch-configlet

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,6 @@
66

77
set -eo pipefail
88

9-
readonly LATEST='https://api.github.com/repos/exercism/configlet/releases/latest'
10-
11-
case "$(uname)" in
12-
Darwin*) os='mac' ;;
13-
Linux*) os='linux' ;;
14-
Windows*) os='windows' ;;
15-
MINGW*) os='windows' ;;
16-
MSYS_NT-*) os='windows' ;;
17-
*) os='linux' ;;
18-
esac
19-
20-
case "${os}" in
21-
windows*) ext='zip' ;;
22-
*) ext='tgz' ;;
23-
esac
24-
25-
case "$(uname -m)" in
26-
*64*) arch='64bit' ;;
27-
*686*) arch='32bit' ;;
28-
*386*) arch='32bit' ;;
29-
*) arch='64bit' ;;
30-
esac
31-
329
curlopts=(
3310
--silent
3411
--show-error
@@ -41,15 +18,25 @@ if [[ -n "${GITHUB_TOKEN}" ]]; then
4118
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
4219
fi
4320

44-
suffix="${os}-${arch}.${ext}"
45-
4621
get_download_url() {
47-
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" |
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+
*64*) arch='64bit' ;;
28+
*686*) arch='32bit' ;;
29+
*386*) arch='32bit' ;;
30+
*) arch='64bit' ;;
31+
esac
32+
local suffix="${os}-${arch}.${ext}"
33+
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${latest}" |
4834
grep "\"browser_download_url\": \".*/download/.*/configlet.*${suffix}\"$" |
4935
cut -d'"' -f4
5036
}
5137

5238
main() {
39+
local output_dir
5340
if [[ -d ./bin ]]; then
5441
output_dir="./bin"
5542
elif [[ $PWD == */bin ]]; then
@@ -59,9 +46,26 @@ main() {
5946
return 1
6047
fi
6148

49+
local os
50+
case "$(uname)" in
51+
Darwin*) os='mac' ;;
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='tgz' ;;
63+
esac
64+
6265
echo "Fetching configlet..." >&2
63-
download_url="$(get_download_url)"
64-
output_path="${output_dir}/latest-configlet.${ext}"
66+
local download_url
67+
download_url="$(get_download_url "${os}" "${ext}")"
68+
local output_path="${output_dir}/latest-configlet.${ext}"
6569
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
6670

6771
case "${ext}" in
@@ -71,12 +75,14 @@ main() {
7175

7276
rm -f "${output_path}"
7377

78+
local executable_ext
7479
case "${os}" in
7580
windows*) executable_ext='.exe' ;;
7681
*) executable_ext='' ;;
7782
esac
7883

79-
configlet_path="${output_dir}/configlet${executable_ext}"
84+
local configlet_path="${output_dir}/configlet${executable_ext}"
85+
local configlet_version
8086
configlet_version="$(${configlet_path} --version)"
8187
echo "Downloaded configlet ${configlet_version} to ${configlet_path}"
8288
}

0 commit comments

Comments
 (0)