From a008ef0b50a366acaf7f20ac83c502a798065361 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 25 Oct 2024 15:59:03 +0200 Subject: [PATCH] Some improvements to `install.sh` (#2738) * 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 --- install.sh | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/install.sh b/install.sh index 73a023a235a..1adb4245a1d 100755 --- a/install.sh +++ b/install.sh @@ -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" } @@ -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"