Skip to content

Commit 3c6ac6c

Browse files
committed
Review code and improve style
1 parent d9c5519 commit 3c6ac6c

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

check

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#!/bin/bash
22

3-
set -e
3+
set -eo pipefail
44

55
exec 3>&1 # make stdout available as fd 3 for the result
66
exec 1>&2 # redirect all output to stderr for logging
77

8-
payload=$(mktemp "$TMPDIR/resource-check.XXXXXX")
9-
cat > "$payload" <&0
8+
payload=$(mktemp "${TMPDIR}/resource-check.XXXXXX")
9+
cat > "${payload}" <&0
1010

11-
source_project=$(jq -r '.source.project // empty' < "$payload")
12-
if [[ "${source_project}X" == "X" ]]; then
13-
>&2 echo "Source parameter 'project' is missing"
14-
exit 1
11+
source_project=$(jq --raw-output '.source.project // empty' < "${payload}")
12+
if [[ -z "${source_project}" ]]; then
13+
>&2 echo "Source parameter 'project' is missing"
14+
exit 1
1515
fi
1616

17-
curl -sf "https://releases.hashicorp.com/${source_project}/index.json" > /dev/null || (
18-
>&2 echo "Unknown hashicorp project '$source_project'"
19-
exit 1
17+
curl --silent --fail --show-error --location --output /dev/null \
18+
--url "https://releases.hashicorp.com/${source_project}/index.json" || (
19+
>&2 echo "Unknown hashicorp project '${source_project}'"
20+
exit 1
2021
)
2122
>&2 echo "Looking up versions of '${source_project}'"
22-
latest_version=$(curl -s "https://releases.hashicorp.com/${source_project}/index.json" \
23-
| jq -r '.versions | keys[]' \
24-
| grep -v "beta" | grep -v "rc" | grep -v "alpha" | grep -v "+" \
25-
| sort -V | tail -n1)
23+
latest_version=$(curl --silent --fail --url "https://releases.hashicorp.com/${source_project}/index.json" \
24+
| jq -r '.versions | keys[]' \
25+
| grep -v "beta" | grep -v "rc" | grep -v "alpha" | grep -v "+" \
26+
| sort -V | tail -n1)
2627

27-
>&2 echo "Latest version $latest_version"
28+
>&2 echo "Latest version ${latest_version}"
2829

29-
jq -n --arg version "${latest_version}" '[{ version: $version }]' >&3
30+
jq --null-input --arg version "${latest_version}" '[ { version: $version } ]' >&3

in

+38-33
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,58 @@
11
#!/bin/bash
22

3-
set -e
3+
set -eo pipefail
44

55
exec 3>&1 # make stdout available as fd 3 for the result
66
exec 1>&2 # redirect all output to stderr for logging
77

88
destination=$1
99

10-
payload=$(mktemp "$TMPDIR/resource-check.XXXXXX")
11-
cat > "$payload" <&0
10+
payload=$(mktemp "${TMPDIR}/resource-check.XXXXXX")
11+
cat > "${payload}" <&0
1212

13-
version=$(jq -r '.version.version // empty' < "$payload")
14-
if [[ "${version}X" == "X" ]]; then
15-
>&2 echo "Version info 'version' is missing"
16-
exit 1
13+
version=$(jq -r '.version.version // empty' < "${payload}")
14+
if [[ -z ${version} ]]; then
15+
>&2 echo "Version info 'version' is missing"
16+
exit 1
1717
fi
1818

19-
source_project=$(jq -r '.source.project // empty' < "$payload")
20-
if [[ "${source_project}X" == "X" ]]; then
21-
>&2 echo "Source parameter 'project' is missing"
22-
exit 1
19+
source_project=$(jq -r '.source.project // empty' < "${payload}")
20+
if [[ -z ${source_project} ]]; then
21+
>&2 echo "Source parameter 'project' is missing"
22+
exit 1
2323
fi
2424

25-
params_regexp=$(jq -r '.params.regexp // empty' < "$payload")
25+
params_regexp=$(jq -r '.params.regexp // empty' < "${payload}")
2626

27-
curl -sf "https://releases.hashicorp.com/${source_project}/index.json" > /dev/null || (
28-
>&2 echo "Unknown hashicorp project '$source_project'"
29-
exit 1
27+
curl --silent --fail --show-error --location --output /dev/null \
28+
--url "https://releases.hashicorp.com/${source_project}/index.json" || (
29+
>&2 echo "Unknown hashicorp project '${source_project}'"
30+
exit 1
3031
)
3132

3233
>&2 echo "Fetching assets ${source_project} v${version}"
33-
cd "$destination"
34-
echo "$version" > version
35-
echo "$source_project" > project
36-
37-
build_urls=$(curl -s "https://releases.hashicorp.com/${source_project}/index.json" | jq -r ".versions[\"${version}\"].builds[].url")
38-
if [[ ! -z $params_regexp ]]; then
39-
set +e
40-
build_urls=$(echo "${build_urls}" | grep "${params_regexp}")
41-
set -e
34+
cd "${destination}"
35+
echo "${version}" > version
36+
echo "${source_project}" > project
37+
38+
build_urls=$(
39+
curl --silent --fail --show-error --location \
40+
--url "https://releases.hashicorp.com/${source_project}/index.json" \
41+
| jq -r ".versions[\"${version}\"].builds[].url"
42+
)
43+
if [[ -n "${params_regexp}" ]]; then
44+
set +e
45+
build_urls=$(grep "${params_regexp}" <<< "${build_urls}")
46+
set -e
4247
fi
43-
if [[ ! -z $build_urls ]]; then
44-
for url in $build_urls; do
45-
>&2 echo "Downloading $url"
46-
curl -O "$url"
47-
done
48-
else
49-
>&2 echo "regexp '$params_regexp' did not match any build URLs"
50-
exit 1
48+
if [[ -z "${build_urls}" ]]; then
49+
>&2 echo "Regexp '${params_regexp}' did not match any build URLs"
50+
exit 1
5151
fi
52+
for url in ${build_urls}; do
53+
>&2 echo "Downloading ${url}"
54+
curl --silent --fail --show-error --location --remote-name \
55+
--url "${url}"
56+
done
5257

53-
jq -n --arg version "${version}" '{version: { version: $version }}' >&3
58+
jq --null-input --arg version "${version}" '{ version: { version: $version } }' >&3

0 commit comments

Comments
 (0)