-
Notifications
You must be signed in to change notification settings - Fork 354
/
update50.sh
86 lines (68 loc) · 3.03 KB
/
update50.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# Check for gh and install it if not available (temporary)
if ! command -v gh &> /dev/null; then
(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
&& wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
fi
# Check for -t flag for develop mode
if [ "$1" == "-t" ]; then
# Use the specified tag to build the URL
tag=$2
url="https://cs50.dev/devcontainer.json?tag=$tag"
# Fetch and save the JSON
curl --fail --header "Cache-Control: no-cache" --silent --location "$url" > "/workspaces/$RepositoryName/.devcontainer.json"
if [ $? -ne 0 ]; then
echo "Could not update codespace with tag $tag. Try again later."
exit 1
fi
# Trigger rebuild
if command -v gh &> /dev/null; then
# Use gh cli to rebuild if available
gh cs rebuild --codespace $CODESPACE_NAME --full
echo -e "\033[31mYour codespace is now being rebuilt, please keep the browser window open and wait for it to reload.\nDo not perform any actions until the rebuild is complete.\033[0m"
else
# Fall back to command50
command50 github.codespaces.rebuildEnvironment
fi
exit 0
fi
# Get remote JSON
remote=$(curl --fail --header "Cache-Control: no-cache" --silent --location https://cs50.dev/devcontainer.json)
if [ $? -ne 0 ]; then
echo "Could not update codespace. Try again later."
exit 1
fi
# Parse remote JSON
image=$(echo $remote | jq .image 2> /dev/null)
regex='"ghcr.io/cs50/codespace:([0-9a-z]*)"'
if [[ "$image" =~ $regex ]]; then
tag="${BASH_REMATCH[1]}"
else
echo "Could not determine latest version. Try again later."
exit 1
fi
# Get local version
issue=$(tail -1 /etc/issue 2> /dev/null)
# Get local JSON
local=$(cat "/workspaces/$RepositoryName/.devcontainer.json" 2> /dev/null)
# If versions differ (or forcibly updating)
if [ "$remote" != "$local" ] || [ "$tag" != "$issue" ] || [ "$1" == "-f" ] || [ "$1" == "--force" ]; then
# Update JSON
echo "$remote" > "/workspaces/$RepositoryName/.devcontainer.json"
# Trigger rebuild
if command -v gh &> /dev/null; then
# Use gh cli to rebuild if available
gh cs rebuild --codespace $CODESPACE_NAME --full
echo -e "\033[31mYour codespace is now being rebuilt, please keep the browser window open and wait for it to reload.\nDo not perform any actions until the rebuild is complete.\033[0m"
else
# Fall back to command50
command50 github.codespaces.rebuildEnvironment
fi
else
echo "Your codespace is already up-to-date!"
fi