Skip to content

Commit d9ef9a2

Browse files
committed
feat: change auto update script to allow getting release
1 parent edba2e4 commit d9ef9a2

File tree

1 file changed

+68
-19
lines changed

1 file changed

+68
-19
lines changed

server/scripts/auto-update.sh

+68-19
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,74 @@ WARNING=$(tput setaf 3)
88
INFO=$(tput setaf 6)
99
RESET=$(tput sgr0)
1010

11+
# Params
12+
13+
BUILD_BINARY="n"
14+
1115
# Vars
1216

1317
REPO_URL="https://github.com/DavidGomesDev/RustyController"
1418
RUSTY_HOME_DIR="$HOME/RustyController"
1519
BINARY_PATH="$RUSTY_HOME_DIR/server/target/release/rusty_controller"
1620
HASH_FILE="$RUSTY_HOME_DIR/current.sha256"
1721

22+
# Get params
23+
24+
show_usage () {
25+
echo "Parameters:"
26+
echo "-b: build binary (instead of downloading the latest release)"
27+
}
28+
29+
while getopts "bh" opt; do
30+
case ${opt} in
31+
b)
32+
BUILD_BINARY="y"
33+
;;
34+
h)
35+
show_usage
36+
exit 0
37+
;;
38+
?)
39+
echo "Invalid option: -${OPTARG}."
40+
show_usage
41+
exit 1
42+
;;
43+
esac
44+
done
45+
46+
echo "$START* Updating server$RESET"
47+
1848
cd "$HOME" || exit 1
1949

50+
download_latest () {
51+
# Get arch (and trim output)
52+
arch=$(dpkg --print-architecture | xargs echo -n)
53+
54+
mkdir -p target/release || exit 1
55+
wget -q "$REPO_URL/releases/download/latest/server-$arch" -O target/release/rusty_controller || exit 1
56+
57+
echo "$INFO* Downloaded latest release binary!$RESET"
58+
}
59+
2060
build () {
61+
echo
62+
echo "$INFO* Updating crates...$RESET"
63+
echo
64+
65+
cargo update -q || exit 1
66+
67+
echo "$INFO* Build...$RESET"
68+
echo
69+
70+
time cargo build --release -q || exit 1
71+
72+
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
73+
74+
echo "$newest_hash" > "$HASH_FILE"
75+
echo "$SUCCESS* Built successfully!$RESET"
76+
}
77+
78+
update () {
2179
echo "$INFO* Checking out main...$RESET"
2280
echo
2381

@@ -30,21 +88,12 @@ build () {
3088

3189
cd server/ || exit 1
3290

33-
echo
34-
echo "$INFO* Updating crates...$RESET"
35-
echo
36-
37-
cargo update -q || exit 1
38-
39-
echo "$INFO* Build...$RESET"
40-
echo
41-
42-
time cargo build --release -q || exit 1
43-
44-
newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}')
91+
if [[ "$BUILD_BINARY" == "y" ]]; then
92+
build
93+
else
94+
download_latest
95+
fi
4596

46-
echo "$newest_hash" > "$HASH_FILE"
47-
echo "$SUCCESS* Built successfully!$RESET"
4897
cd ..
4998
}
5099

@@ -54,10 +103,10 @@ launch () {
54103
}
55104

56105
if [[ ! -d "$RUSTY_HOME_DIR" ]]; then
57-
echo "$WARNING* Rusty not found. Cloning...$RESET"
106+
echo "$WARNING* Rusty repo not found. Cloning...$RESET"
58107
git clone "$REPO_URL"
59108

60-
build
109+
update
61110
launch
62111
exit 0
63112
fi
@@ -67,13 +116,13 @@ cd "$RUSTY_HOME_DIR" || exit 1
67116
if [[ -f "$HASH_FILE" ]]; then
68117
if [ ! -f "$BINARY_PATH" ]; then
69118
echo "$INFO* Binary not found, building...$RESET"
70-
build
119+
update
71120
launch
72121
exit 0
73122
fi
74123

75124
current_hash=$(cat "$HASH_FILE")
76-
build
125+
update
77126
if [[ "$current_hash" != "$newest_hash" ]]; then
78127
echo "$INFO* Built a new version!$RESET"
79128
launch
@@ -82,6 +131,6 @@ if [[ -f "$HASH_FILE" ]]; then
82131
fi
83132
else
84133
echo "$WARNING* Couldn't find current hash. Updating to latest version anyway.$RESET"
85-
build
134+
update
86135
launch
87136
fi

0 commit comments

Comments
 (0)