Skip to content

Commit

Permalink
Some improvements to install.sh (#2738)
Browse files Browse the repository at this point in the history
* Added --show-error flag to curl command line

This change will show more information when the download fails.

  -S, --show-error
      When used with -s, --silent, it makes curl show an error message if it fails.

* Reuse download functions

* Remove tmp files after use
  • Loading branch information
cmaglie authored Oct 25, 2024
1 parent 7055f2a commit a008ef0
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ getFile() {
GETFILE_URL="$1"
GETFILE_FILE_PATH="$2"
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
GETFILE_HTTP_STATUS_CODE=$(curl -s -w '%{http_code}' -L "$GETFILE_URL" -o "$GETFILE_FILE_PATH")
GETFILE_HTTP_STATUS_CODE=$(curl --silent --show-error --write-out '%{http_code}' --location "$GETFILE_URL" -o "$GETFILE_FILE_PATH")
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
TMP_FILE=$(mktemp)
wget --server-response --content-on-error -q -O "$GETFILE_FILE_PATH" "$GETFILE_URL" 2>"$TMP_FILE"
GETFILE_HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
rm -f "$TMP_FILE"
fi
echo "$GETFILE_HTTP_STATUS_CODE"
}
Expand Down Expand Up @@ -155,15 +156,10 @@ downloadFile() {
echo "Trying to find a release using the GitHub API."

LATEST_RELEASE_URL="https://api.github.com/repos/${PROJECT_OWNER}/$PROJECT_NAME/releases/tags/$TAG"
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
HTTP_RESPONSE=$(curl -sL --write-out 'HTTPSTATUS:%{http_code}' "$LATEST_RELEASE_URL")
HTTP_STATUS_CODE=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
TMP_FILE=$(mktemp)
BODY=$(wget --server-response --content-on-error -q -O - "$LATEST_RELEASE_URL" 2>"$TMP_FILE" || true)
HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
fi
TMP_BODY_FILE=$(mktemp)
HTTP_STATUS_CODE=$(getFile "$LATEST_RELEASE_URL" "$TMP_BODY_FILE")
BODY=$(cat "$TMP_BODY_FILE")
rm -f "$TMP_BODY_FILE"
if [ "$HTTP_STATUS_CODE" != 200 ]; then
echo "Request failed with HTTP status code $HTTP_STATUS_CODE"
fail "Body: $BODY"
Expand Down

0 comments on commit a008ef0

Please sign in to comment.