Skip to content

Commit 8c10afd

Browse files
committed
feat: Use core.panic. Closes #1
1 parent 1013a9d commit 8c10afd

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

pkg/src/public/bash-core.sh

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,29 @@ core.trap_add() {
1818
fi
1919
local function="$1"
2020

21-
# validation
21+
# Validation
2222
if [ -z "$function" ]; then
23-
core.print_error 'First argument must not be empty'
24-
return 1
23+
core.panic 'First argument must not be empty'
2524
fi
2625

2726
if (($# <= 1)); then
28-
core.print_error 'Must specify at least one signal'
29-
return 1
27+
core.panic 'Must specify at least one signal'
3028
fi
3129
for signal_spec in "${@:2}"; do
3230
if [ -z "$signal_spec" ]; then
33-
core.print_error 'Signal must not be an empty string'
34-
return 1
31+
core.panic 'Signal must not be an empty string'
3532
fi
3633

3734
local regex='^[0-9]+$'
3835
if [[ "$signal_spec" =~ $regex ]]; then
39-
core.print_error 'Passing numbers for the signal specs is prohibited'
40-
return 1
36+
core.panic 'Passing numbers for the signal specs is prohibited'
4137
fi; unset regex
4238
signal_spec=${signal_spec#SIG}
4339
if ! declare -f "$function" &>/dev/null; then
44-
core.print_error "Function '$function' is not defined"
45-
return 1
40+
core.panic "Function '$function' is not defined"
4641
fi
4742

48-
# start
43+
# Start
4944
___global_trap_table___["$signal_spec"]="${___global_trap_table___[$signal_spec]}"$'\x1C'"$function"
5045

5146
# rho (WET)
@@ -55,8 +50,7 @@ core.trap_add() {
5550
if ! eval "$global_trap_handler_name() {
5651
core.util.trap_handler_common '$signal_spec'
5752
}"; then
58-
core.print_error 'Could not eval function'
59-
return 1
53+
core.panic 'Could not eval function'
6054
fi
6155
# shellcheck disable=SC2064
6256
trap "$global_trap_handler_name" "$signal_spec"
@@ -78,34 +72,29 @@ core.trap_remove() {
7872
fi
7973
local function="$1"
8074

81-
# validation
75+
# Validation
8276
if [ -z "$function" ]; then
83-
core.print_error 'First argument must not be empty'
84-
return 1
77+
core.panic 'First argument must not be empty'
8578
fi
8679

8780
if (($# <= 1)); then
88-
core.print_error 'Must specify at least one signal'
89-
return 1
81+
core.panic 'Must specify at least one signal'
9082
fi
9183
for signal_spec in "${@:2}"; do
9284
if [ -z "$signal_spec" ]; then
93-
core.print_error 'Signal must not be an empty string'
94-
return 1
85+
core.panic 'Signal must not be an empty string'
9586
fi
9687

9788
local regex='^[0-9]+$'
9889
if [[ "$signal_spec" =~ $regex ]]; then
99-
core.print_error 'Passing numbers for the signal specs is prohibited'
100-
return 1
90+
core.panic 'Passing numbers for the signal specs is prohibited'
10191
fi; unset regex
10292
signal_spec="${signal_spec#SIG}"
10393
if ! declare -f "$function" &>/dev/null; then
104-
core.print_error "Function '$function' is not defined"
105-
return 1
94+
core.panic "Function '$function' is not defined"
10695
fi
10796

108-
# start
97+
# Start
10998
local -a trap_handlers=()
11099
local new_trap_handlers=
111100
IFS=$'\x1C' read -ra trap_handlers <<< "${___global_trap_table___[$signal_spec]}"
@@ -147,13 +136,11 @@ core.shopt_push() {
147136
local shopt_name="$2"
148137

149138
if [ -z "$shopt_action" ]; then
150-
core.print_error 'First argument cannot be empty'
151-
return 1
139+
core.panic 'First argument cannot be empty'
152140
fi
153141

154142
if [ -z "$shopt_name" ]; then
155-
core.print_error 'Second argument cannot be empty'
156-
return 1
143+
core.panic 'Second argument cannot be empty'
157144
fi
158145

159146
local -i previous_shopt_errcode=
@@ -165,17 +152,14 @@ core.shopt_push() {
165152

166153
if [ "$shopt_action" = '-s' ]; then
167154
if shopt -s "$shopt_name"; then :; else
168-
# on error, option will not be set
169-
return $?
155+
core.panic "Could not set shopt option" $?
170156
fi
171157
elif [ "$shopt_action" = '-u' ]; then
172158
if shopt -u "$shopt_name"; then :; else
173-
# on error, option will not be set
174-
return $?
159+
core.panic "Could not unset shopt option" $?
175160
fi
176161
else
177-
core.print_error "Accepted actions are either '-s' or '-u'"
178-
return 1
162+
core.panic "Accepted actions are either '-s' or '-u'"
179163
fi
180164

181165
if (( previous_shopt_errcode == 0)); then
@@ -197,23 +181,19 @@ core.shopt_pop() {
197181
fi
198182

199183
if (( ${#___global_shopt_stack___[@]} == 0 )); then
200-
core.print_error 'Unable to pop as nothing is in the shopt stack'
201-
return 1
184+
core.panic 'Unable to pop as nothing is in the shopt stack'
202185
fi
203186

204187
if (( ${#___global_shopt_stack___[@]} & 1 )); then
205-
core.print_error 'Shopt stack is malformed'
206-
return 1
188+
core.panic 'Shopt stack is malformed'
207189
fi
208190

209191
# Stack now guaranteed to have at least 2 elements (so the following accessors won't error)
210192
local shopt_action="${___global_shopt_stack___[-2]}"
211193
local shopt_name="${___global_shopt_stack___[-1]}"
212194

213195
if shopt -u "$shopt_name"; then :; else
214-
local errcode=$?
215-
core.print_error 'Could not restore previous shopt option'
216-
return $errcode
196+
core.panic 'Could not restore previous shopt option' $?
217197
fi
218198

219199
___global_shopt_stack___=("${___global_shopt_stack___[@]::${#___global_shopt_stack___[@]}-2}")
@@ -232,13 +212,11 @@ core.err_set() {
232212
ERRCODE=$1
233213
ERR=$2
234214
else
235-
core.print_error 'Incorrect function arguments'
236-
return 1
215+
core.panic 'Incorrect function arguments'
237216
fi
238217

239218
if [ -z "$ERR" ]; then
240-
core.print_error "Argument for 'ERR' cannot be empty"
241-
return 1
219+
core.panic "Argument for 'ERR' cannot be empty"
242220
fi
243221
}
244222

@@ -307,6 +285,7 @@ core.print_stacktrace() {
307285
done; unset -v i
308286

309287
if [ "$cd_failed" = 'yes' ]; then
288+
# Do NOT 'core.panic'
310289
core.print_error "A 'cd' failed, so the stacktrace may include relative paths"
311290
fi
312291
} >&2
@@ -337,7 +316,16 @@ core.print_info() {
337316

338317
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
339318
core.panic() {
340-
local code="${1-1}"
319+
local code='1'
320+
321+
if [[ $1 =~ [0-9]+ ]]; then
322+
code=$1
323+
elif [ -n "$1" ]; then
324+
if [ -n "$2" ]; then
325+
code=$2
326+
fi
327+
core.print_error "$1"
328+
fi
341329

342330
if core.err_exists; then
343331
core.err_print
@@ -390,7 +378,7 @@ core.get_package_info() {
390378
local toml_file="$basalt_package_dir/basalt.toml"
391379

392380
if [ ! -f "$toml_file" ]; then
393-
core.print_error "File '$toml_file' could not be found"
381+
core.panic "File '$toml_file' could not be found"
394382
fi
395383

396384
local regex=$'^[ \t]*'"${key_name}"$'[ \t]*=[ \t]*[\'"](.*)[\'"]'

0 commit comments

Comments
 (0)