-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-instances.sh
executable file
·61 lines (50 loc) · 1.89 KB
/
update-instances.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh
# Exit on any error
set -e
echo "Updating PeerTubeScript.js"
# Ensure that required commands are available
command -v curl >/dev/null 2>&1 || { echo "Error: curl is not installed." >&2; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "Error: jq is not installed." >&2; exit 1; }
# Ensure that PeerTubeScript.js ends with exactly one newline
if [ -s PeerTubeScript.js ]; then
last_char_hex=$(tail -c 1 PeerTubeScript.js | od -An -t x1 | tr -d ' ')
if [ "$last_char_hex" != "0a" ]; then
echo >> PeerTubeScript.js
fi
else
# If the file is empty, add a newline
echo >> PeerTubeScript.js
fi
# Define markers
start_marker="// BEGIN AUTOGENERATED INSTANCES"
end_marker="// END AUTOGENERATED INSTANCES"
# Get the current date for autogenerated content
current_date=$(date +%Y-%m-%d)
# Fetch instances and prepare new content
instance_data=$(curl -s "https://instances.joinpeertube.org/api/v1/instances?start=0&count=1000" | jq -r '.data[].host')
# Check if we fetched valid data
if [ -z "$instance_data" ]; then
echo "Error: No instances fetched. Aborting update." >&2
exit 1
fi
# Generate new array content with minified formatting
new_array_content=$(echo "$instance_data" | awk -v date="$current_date" -v start_marker="$start_marker" -v end_marker="$end_marker" 'BEGIN {
print start_marker
print "// This content is autogenerated during deployment using update-instances.sh and content from https://instances.joinpeertube.org"
print "// Last updated at: " date
printf "INDEX_INSTANCES.instances = ["
}
{
printf "\"" $0 "\","
}
END {
print "];"
print end_marker
}')
# Remove existing block safely and append new content
temp_file=$(mktemp)
sed "\#$start_marker#,\#$end_marker#d" PeerTubeScript.js > "$temp_file"
echo "$new_array_content" >> "$temp_file"
mv "$temp_file" PeerTubeScript.js
# Print success message
echo "PeerTubeScript.js updated successfully"