Skip to content

Commit 4925399

Browse files
committed
fix: exit each script with status code 0 for correct error handling in scripts
1 parent fc94b8e commit 4925399

6 files changed

Lines changed: 73 additions & 40 deletions

File tree

helpers/backup_server.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ else
102102
echo "[ERROR] Backup failed."
103103
exit 1
104104
fi
105+
106+
exit 0

helpers/install_server.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
1313
cd "${SCRIPT_DIR}/../"
1414
source ./common/utils.sh
1515

16+
# parse command line arguments
17+
parse_args flags args "$@"
18+
1619
VERBOSE=false
17-
if in_array "-v" "$@"; then
20+
if in_array "-v" "${flags[@]}"; then
1821
VERBOSE=true
1922
fi
2023

@@ -29,8 +32,14 @@ if [ ! -f "./load_config.sh" ]; then
2932
fi
3033
source ./load_config.sh
3134

32-
# Read json string from stdin
33-
json_config=$(cat)
35+
# Read json string from first argument or stdin
36+
if [ -n "${args[0]}" ]; then
37+
# Read from the first argument
38+
json_config="${args[0]}"
39+
else
40+
# Read from stdin
41+
json_config=$(cat)
42+
fi
3443

3544
# Test if json_config is a valid JSON object
3645
if ! echo "$json_config" | jq 'empty' 2>/dev/null; then
@@ -147,3 +156,5 @@ else
147156
fi
148157

149158
log "[INFO] Successfully installed server '$server_alias' at $server_dir"
159+
160+
exit 0

helpers/schedule_server_stop.sh

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ cd ${SCRIPT_DIR}
66
source ../common/utils.sh
77

88
if (($# < 2)); then
9-
echo "Usage: $0 <server_alias> <seconds> <note|null> <kick_reason>"
10-
exit 1
9+
echo "Usage: $0 <server_alias> <seconds> <note|null> <kick_reason>"
10+
exit 1
1111
fi
1212

1313
server_alias="$1"
@@ -19,28 +19,30 @@ message="$*"
1919
print_timestamps=(300 180 120 60 30 10 5 4 3 2 1)
2020

2121
while ((total_seconds > 0)); do
22-
if in_array "$total_seconds" "${print_timestamps[@]}"; then
23-
echo "[INFO] Stopping server $server_alias in $(print_time $total_seconds)"
24-
prefix=""
25-
[ "$note" != "null" ] && prefix="[$note] "
26-
send_server_command "$server_alias" "say ${prefix}Stopping server in $(print_time $total_seconds)"
27-
if [ ! "$?" -eq 0 ]; then
28-
echo "[ERROR] Failed to send command to server $server_alias" >&2
29-
exit 1
30-
fi
22+
if in_array "$total_seconds" "${print_timestamps[@]}"; then
23+
echo "[INFO] Stopping server $server_alias in $(print_time $total_seconds)"
24+
prefix=""
25+
[ "$note" != "null" ] && prefix="[$note] "
26+
send_server_command "$server_alias" "say ${prefix}Stopping server in $(print_time $total_seconds)"
27+
if [ ! "$?" -eq 0 ]; then
28+
echo "[ERROR] Failed to send command to server $server_alias" >&2
29+
exit 1
3130
fi
32-
sleep 1
33-
((total_seconds--))
31+
fi
32+
sleep 1
33+
((total_seconds--))
3434
done
3535

3636
send_server_command "$server_alias" "kick @a $message"
3737
send_server_command "$server_alias" "stop"
3838
if [ ! "$?" -eq 0 ]; then
39-
echo "[ERROR] Failed to stop server $server_alias" >&2
40-
exit 1
39+
echo "[ERROR] Failed to stop server $server_alias" >&2
40+
exit 1
4141
fi
4242

4343
# wait for server to stop
4444
while [ "$(get_server_status "$server_alias")" == "Running" ]; do
45-
sleep 1
45+
sleep 1
4646
done
47+
48+
exit 0

javman.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
88
cd ${SCRIPT_DIR}
99
source ./common/utils.sh
1010

11-
# Parse command line arguments into flags and positional arguments
1211
VERBOSE=false
1312

13+
# Parse command line arguments into flags and positional arguments
1414
parse_args flags args "$@"
1515

1616
# parse the flags
@@ -173,3 +173,5 @@ install)
173173
exit 1
174174
;;
175175
esac
176+
177+
exit 0

papman.sh

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
88
cd ${SCRIPT_DIR}
99
source ./common/utils.sh
1010

11+
# logging
12+
VERBOSE=false
13+
log() {
14+
$VERBOSE && echo "$@" >&2
15+
}
16+
1117
# Load variables
1218
if [ ! -f "./load_config.sh" ]; then
1319
echo "[ERROR] load_config.sh not found in the script directory." >&2
@@ -18,7 +24,13 @@ source ./load_config.sh
1824
# Check for required commands
1925
check_prerequisites
2026

21-
# parse arguments
27+
# Parse command line arguments into flags and positional arguments
28+
parse_args flags args "$@"
29+
30+
if in_array "-v" "${flags[@]}"; then
31+
VERBOSE=true
32+
fi
33+
2234
operations=("install" "list-versions" "list-builds" "link")
2335

2436
print_usage() {
@@ -47,8 +59,8 @@ print_usage() {
4759
fi
4860
}
4961

50-
if [ -z "$1" ]; then
51-
print_usage "$1"
62+
if [ -z "${args[0]}" ]; then
63+
print_usage "${args[0]}"
5264
exit 1
5365
fi
5466

@@ -129,18 +141,18 @@ construct_latest_jar_name() {
129141
echo "${project}-latest.jar"
130142
}
131143

132-
operation="$1"
144+
operation="${args[0]}"
133145

134146
case "$operation" in
135147
install)
136-
if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
148+
if [ -z "${args[1]}" ] || [ -z "${args[2]}" ] || [ -z "${args[3]}" ]; then
137149
print_usage "$operation"
138150
exit 1
139151
fi
140152

141-
project="$2"
142-
version="$3"
143-
build="$4"
153+
project="${args[1]}"
154+
version="${args[2]}"
155+
build="${args[3]}"
144156

145157
build_info=$(fetch_api "${PAPER_API_ENDPOINT}/projects/${project}/versions/${version}/builds/${build}")
146158
api_jar_name=$(echo "$build_info" |
@@ -170,7 +182,7 @@ install)
170182
chmod +x "${output_path}"
171183
latest_jar_filename=$(get_latest_build_file "$project" "$version")
172184
if [ "$latest_jar_filename" == "null" ]; then
173-
echo "[ERROR] Unexpected failuer: No latest jar file found for project '$project' and version '$version'." >&2
185+
echo "[ERROR] Unexpected failure: No latest jar file found for project '$project' and version '$version'." >&2
174186
exit 1
175187
fi
176188
# ln -sf "${jar_output_directory}/${latest_jar_filename}" "${jar_output_directory}/${project}-latest.jar"
@@ -182,27 +194,27 @@ install)
182194
fi
183195
;;
184196
list-versions)
185-
if [ -z "$2" ]; then
197+
if [ -z "${args[1]}" ]; then
186198
print_usage "$operation"
187199
exit 1
188200
fi
189201

190-
list_versions "$2"
202+
list_versions "${args[1]}"
191203
;;
192204
list-builds)
193-
if [ -z "$2" ] || [ -z "$3" ]; then
205+
if [ -z "${args[1]}" ] || [ -z "${args[2]}" ]; then
194206
print_usage "$operation"
195207
exit 1
196208
fi
197209

198-
list_builds "$2" "$3"
210+
list_builds "${args[1]}" "${args[2]}"
199211
;;
200212
link)
201-
link_name="$2"
202-
project="$3"
203-
version="$4"
204-
build="$5"
205-
channel="$6"
213+
link_name="${args[1]}"
214+
project="${args[2]}"
215+
version="${args[3]}"
216+
build="${args[4]}"
217+
channel="${args[5]}"
206218
target_filename=''
207219
if [ -z "$link_name" ] || [ -z "$project" ] || [ -z "$version" ] || [ -z "$build" ]; then
208220
print_usage "$operation"
@@ -261,11 +273,13 @@ link)
261273
echo "[ERROR] Failed to create symlink '$link_name' -> '$target_filename'" >&2
262274
exit 1
263275
fi
264-
echo "[INFO] Successfully linked '$link_name' to '$target_filename'" >&2
276+
log "[INFO] Successfully linked '$link_name' to '$target_filename'" >&2
265277
;;
266278
*)
267279
echo "[ERROR] Unknown operation: $operation" >&2
268280
print_usage
269281
exit 1
270282
;;
271283
esac
284+
285+
exit 0

serman.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ source ./load_config.sh
1818
# Check for required commands
1919
check_prerequisites
2020

21-
# Parse command line arguments into flags and positional arguments
2221
VERBOSE=false
2322

23+
# Parse command line arguments into flags and positional arguments
2424
parse_args flags args "$@"
2525

2626
# parse the flags
@@ -389,3 +389,5 @@ stop)
389389
exit 1
390390
;;
391391
esac
392+
393+
exit 0

0 commit comments

Comments
 (0)