Files
trezor-suite/scripts/generate-s3-index.sh

162 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# This script generates an index.html file for trezor suite release directories
# Designed to be used within GitHub Actions workflows
#
# Usage:
# ./generate-s3-index.sh <bucket> <path>
#
# Example:
# ./generate-s3-index.sh data.trezor.io suite/releases/desktop/latest
# This will generate index.html for the latest release of Trezor Suite Desktop
set -e
if [ $# -lt 2 ]; then
echo "Usage: ./generate-s3-index.sh <bucket> <path>"
exit 1
fi
BUCKET=$1
S3_PATH=$2
echo "Generating index.html for s3://$BUCKET/$S3_PATH"
# Create a temporary directory
TEMP_DIR=$(mktemp -d)
TEMP_FILE="$TEMP_DIR/index.html"
TEMP_LIST="$TEMP_DIR/s3_list.txt"
cleanup() {
rm -rf "$TEMP_DIR"
}
trap cleanup EXIT
echo "Listing objects in s3://$BUCKET/$S3_PATH/..."
aws s3 ls "s3://$BUCKET/$S3_PATH/" >"$TEMP_LIST"
if ! aws s3 ls "s3://$BUCKET/$S3_PATH/" >"$TEMP_LIST"; then
echo "Error listing S3 objects"
exit 1
fi
echo "AWS S3 output:"
cat "$TEMP_LIST"
cat >"$TEMP_FILE" <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Index of $S3_PATH</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
line-height: 1.4;
color: #333;
max-width: 1000px;
margin: 0 auto;
padding: 10px;
font-size: 14px;
}
h1 {
border-bottom: 1px solid #eaecef;
padding-bottom: 5px;
margin: 0 0 10px 0;
font-size: 18px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 10px;
}
th, td {
text-align: left;
padding: 4px 8px;
border-bottom: 1px solid #eee;
}
th {
background-color: #f6f8fa;
font-weight: 600;
}
a {
color: #0366d6;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.dir a:before {
content: "📁 ";
}
.file a:before {
content: "📄 ";
}
.size {
text-align: right;
white-space: nowrap;
width: 100px;
}
.footer {
font-size: 12px;
color: #666;
margin-top: 5px;
}
</style>
</head>
<body>
<h1>Index of $S3_PATH</h1>
<table>
<tr>
<th>Name</th>
<th class="size">Size</th>
</tr>
<tr>
<td class="dir"><a href="../">Parent Directory</a></td>
<td class="size">-</td>
</tr>
EOF
while IFS= read -r line; do
[[ -z "$line" ]] && continue
echo "Processing line: $line"
if [[ $line =~ [0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\ +([0-9]+)\ +(.*) ]]; then
size="${BASH_REMATCH[1]}"
filename="${BASH_REMATCH[2]}"
echo "Found file: $filename (size: $size)"
[[ "$filename" == "index.html" ]] && continue
if ((size == 0)); then
formatted_size="0 B"
else
formatted_size=$(numfmt --to=iec-i --suffix=B --format="%.1f" "$size")
fi
printf " <tr>\n <td class=\"file\"><a href=\"%s\">%s</a></td>\n <td class=\"size\">%s</td>\n </tr>\n" "$filename" "$filename" "$formatted_size" >>"$TEMP_FILE"
else
echo "Line did not match expected format: $line"
fi
done <"$TEMP_LIST"
CURRENT_DATE=$(date "+%Y-%m-%d %H:%M:%S %Z")
cat >>"$TEMP_FILE" <<EOF
</table>
<div class="footer">Generated by Trezor Suite release tools on $CURRENT_DATE</div>
</body>
</html>
EOF
echo "Uploading index.html to s3://$BUCKET/$S3_PATH/index.html"
if aws s3 cp "$TEMP_FILE" "s3://$BUCKET/$S3_PATH/index.html"; then
echo "Successfully generated and uploaded index.html"
else
echo "Failed to upload index.html"
exit 1
fi