Skip to content

Commit

Permalink
Review code and improve style
Browse files Browse the repository at this point in the history
  • Loading branch information
bgandon committed Dec 5, 2024
1 parent d9c5519 commit 3c6ac6c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
33 changes: 17 additions & 16 deletions check
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#!/bin/bash

set -e
set -eo pipefail

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

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

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

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

>&2 echo "Latest version $latest_version"
>&2 echo "Latest version ${latest_version}"

jq -n --arg version "${latest_version}" '[{ version: $version }]' >&3
jq --null-input --arg version "${latest_version}" '[ { version: $version } ]' >&3
71 changes: 38 additions & 33 deletions in
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
#!/bin/bash

set -e
set -eo pipefail

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

destination=$1

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

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

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

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

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

>&2 echo "Fetching assets ${source_project} v${version}"
cd "$destination"
echo "$version" > version
echo "$source_project" > project

build_urls=$(curl -s "https://releases.hashicorp.com/${source_project}/index.json" | jq -r ".versions[\"${version}\"].builds[].url")
if [[ ! -z $params_regexp ]]; then
set +e
build_urls=$(echo "${build_urls}" | grep "${params_regexp}")
set -e
cd "${destination}"
echo "${version}" > version
echo "${source_project}" > project

build_urls=$(
curl --silent --fail --show-error --location \
--url "https://releases.hashicorp.com/${source_project}/index.json" \
| jq -r ".versions[\"${version}\"].builds[].url"
)
if [[ -n "${params_regexp}" ]]; then
set +e
build_urls=$(grep "${params_regexp}" <<< "${build_urls}")
set -e
fi
if [[ ! -z $build_urls ]]; then
for url in $build_urls; do
>&2 echo "Downloading $url"
curl -O "$url"
done
else
>&2 echo "regexp '$params_regexp' did not match any build URLs"
exit 1
if [[ -z "${build_urls}" ]]; then
>&2 echo "Regexp '${params_regexp}' did not match any build URLs"
exit 1
fi
for url in ${build_urls}; do
>&2 echo "Downloading ${url}"
curl --silent --fail --show-error --location --remote-name \
--url "${url}"
done

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

0 comments on commit 3c6ac6c

Please sign in to comment.