Files
trezor-suite/scripts/generate-s3-directory-index.sh
2025-06-03 10:41:41 +02:00

143 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# generate-s3-directory-index.sh
#
# This script generates an index.html file for AWS S3 directories to list subdirectories
# Designed to be used within GitHub Actions workflows
#
# Usage:
# ./generate-s3-directory-index.sh <bucket> <path>
#
# Example:
# ./generate-s3-directory-index.sh data.trezor.io suite/releases/desktop
set -e
if [ $# -lt 2 ]; then
echo "Usage: ./generate-s3-directory-index.sh <bucket> <path>"
exit 1
fi
BUCKET=$1
S3_PATH=$2
echo "Generating directory index.html for s3://$BUCKET/$S3_PATH"
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 directories in s3://$BUCKET/$S3_PATH/..."
aws s3 ls "s3://$BUCKET/$S3_PATH/" | grep "PRE" >"$TEMP_LIST"
if ! aws s3 ls "s3://$BUCKET/$S3_PATH/" | grep "PRE" >"$TEMP_LIST"; then
echo "Error listing S3 directories"
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: "📁 ";
}
.footer {
font-size: 12px;
color: #666;
margin-top: 5px;
}
</style>
</head>
<body>
<h1>Index of $S3_PATH</h1>
<table>
<tr>
<th>Name</th>
</tr>
<tr>
<td class="dir"><a href="../">Parent Directory</a></td>
</tr>
EOF
while IFS= read -r line; do
[[ -z "$line" ]] && continue
echo "Processing line: $line"
if [[ $line =~ PRE\ (.*)/ ]]; then
dirname="${BASH_REMATCH[1]}"
echo "Found directory: $dirname"
[[ "$dirname" == "index.html" ]] && continue
printf " <tr>\n <td class=\"dir\"><a href=\"%s/\">%s/</a></td>\n </tr>\n" "$dirname" "$dirname" >>"$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