From 3c6ac6cbe6e6313ba5c4f0bf9b9b14b7d7f96e31 Mon Sep 17 00:00:00 2001 From: Benjamin Gandon Date: Thu, 5 Dec 2024 03:37:34 +0100 Subject: [PATCH] Review code and improve style --- check | 33 +++++++++++++-------------- in | 71 ++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/check b/check index d521c95..d1d7143 100755 --- a/check +++ b/check @@ -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 diff --git a/in b/in index b0127c4..6a3687b 100755 --- a/in +++ b/in @@ -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