Skip to content

Commit

Permalink
Add support for personal access token in install shell script
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell committed Mar 26, 2024
1 parent d1722d5 commit e4ad9f4
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ REPOSITORY="filiptibell/rokit"

# Make sure we have prerequisites installed: curl + unzip
if ! command -v curl >/dev/null 2>&1; then
echo "Error: 'curl' is not installed." >&2
echo "ERROR: 'curl' is not installed." >&2
exit 1
fi

if ! command -v unzip >/dev/null 2>&1; then
echo "Error: 'unzip' is not installed." >&2
echo "ERROR: 'unzip' is not installed." >&2
exit 1
fi

Expand All @@ -35,6 +35,10 @@ case "$ARCH" in
exit 1 ;;
esac

if [ ! -z "$GITHUB_PAT" ]; then
echo "NOTE: Using provided GITHUB_PAT for authentication"
fi

# Construct file pattern for our desired zip file based on OS + arch
# NOTE: This only works for exact patterns "binary-X.Y.Z-os-arch.zip"
# and WILL break if the version contains extra metadata / pre-release
Expand All @@ -52,26 +56,34 @@ fi
FILE_PATTERN="${BINARY_NAME}-${VERSION_PATTERN}-${OS}-${ARCH}.zip"

# Use curl to fetch the latest release data from GitHub API
RELEASE_JSON_DATA=$(curl --proto '=https' --tlsv1.2 -sSf "$API_URL")
if [ ! -z "$GITHUB_PAT" ]; then
RELEASE_JSON_DATA=$(curl --proto '=https' --tlsv1.2 -sSf "$API_URL" -H "Authorization: token $GITHUB_PAT")
else
RELEASE_JSON_DATA=$(curl --proto '=https' --tlsv1.2 -sSf "$API_URL")
fi

# Check if the release was fetched successfully
if [ -z "$RELEASE_JSON_DATA" ] || echo "$RELEASE_JSON_DATA" | grep -q "Not Found"; then
echo "Error: Latest release was not found. Please check your network connection." >&2
echo "ERROR: Latest release was not found. Please check your network connection." >&2
exit 1
fi

# Try to extract the download URL from the response
RELEASE_DOWNLOAD_URL=$(echo "$RELEASE_JSON_DATA" | grep -o "\"browser_download_url\": \".*${FILE_PATTERN}\"" | cut -d '"' -f 4 | head -n 1)
if [ -z "$RELEASE_DOWNLOAD_URL" ]; then
echo "Error: Failed to find zip that matches the pattern \"$FILE_PATTERN\" in the latest release." >&2
echo "ERROR: Failed to find zip that matches the pattern \"$FILE_PATTERN\" in the latest release." >&2
exit 1
fi

# Download the file using curl and make sure it was successful
ZIP_FILE=$(echo "$RELEASE_DOWNLOAD_URL" | rev | cut -d '/' -f 1 | rev)
curl --proto '=https' --tlsv1.2 -L -o "$ZIP_FILE" -sSf "$RELEASE_DOWNLOAD_URL"
if [ ! -z "$GITHUB_PAT" ]; then
curl --proto '=https' --tlsv1.2 -L -o "$ZIP_FILE" -sSf "$RELEASE_DOWNLOAD_URL" -H "Authorization: token $GITHUB_PAT"
else
curl --proto '=https' --tlsv1.2 -L -o "$ZIP_FILE" -sSf "$RELEASE_DOWNLOAD_URL"
fi
if [ ! -f "$ZIP_FILE" ]; then
echo "Error: Failed to download the release archive '$ZIP_FILE'." >&2
echo "ERROR: Failed to download the release archive '$ZIP_FILE'." >&2
exit 1
fi

Expand All @@ -80,7 +92,7 @@ echo "Unzipping '$ZIP_FILE'..."
unzip -o -q "$ZIP_FILE" "$BINARY_NAME" -d .
rm "$ZIP_FILE"
if [ ! -f "$BINARY_NAME" ]; then
echo "Error: The file '$BINARY_NAME' does not exist in the archive." >&2
echo "ERROR: The file '$BINARY_NAME' does not exist in the archive." >&2
exit 1
fi

Expand Down

0 comments on commit e4ad9f4

Please sign in to comment.