Skip to content

Commit bc47d7e

Browse files
committed
feat: Add bash-error
It is best to have one library for common Bash things. So, `bash-error` was merged into here, `bash-core`
1 parent 0c3a616 commit bc47d7e

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

pkg/lib/public/bash-core.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,25 @@ core.shopt_pop() {
186186

187187
___global_shopt_stack___=("${___global_shopt_stack___[@]::${#___global_shopt_stack___[@]}-2}")
188188
}
189+
190+
# Variables not checked: 'MAKE_TERMOUT', 'MAKE_TERMERR'
191+
192+
# TODO: 'COLORTERM'
193+
core.should_color_output() {
194+
# https://no-color.org
195+
if [[ -v NO_COLOR ]]; then
196+
return 1
197+
fi
198+
199+
# 0 => 2 colors
200+
# 1 => 16 colors
201+
# 2 => 256 colors
202+
# 3 => 16,777,216 colors
203+
if [[ -v FORCE_COLOR ]]; then
204+
return 0
205+
fi
206+
207+
if [[ $TERM == dumb ]]; then
208+
return 0
209+
fi
210+
}

pkg/lib/public/error.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# TODO: deprecate public/bash-error.sh
2+
3+
core.err_set() {
4+
if (($# == 1)); then
5+
ERRCODE=1
6+
ERR="$1"
7+
elif (($# == 2)); then
8+
ERRCODE="$1"
9+
ERR="$2"
10+
else
11+
printf '%s\n' "Error: bash-error: Incorrect function arguments"
12+
return 1
13+
fi
14+
15+
if [ -z "$ERR" ]; then
16+
printf '%s\n' "Error: bash-error: Argument for 'ERR' cannot be empty"
17+
return 1
18+
fi
19+
}
20+
21+
core.err_clear() {
22+
ERRCODE=
23+
ERR=
24+
}
25+
26+
core.err_exists() {
27+
if [ -z "$ERR" ]; then
28+
return 1
29+
else
30+
return 0
31+
fi
32+
}

tests/error.bats

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats
2+
3+
load './util/init.sh'
4+
5+
@test "core.err_exists works when set" {
6+
# Function sets error when it fails
7+
core.err_set 1 "Failed to eat grass"
8+
9+
# Callsite notices failure, and checks error
10+
core.err_exists
11+
}
12+
13+
@test "core.err_exists works when not set 1" {
14+
! core.err_exists
15+
}
16+
17+
@test "core.err_exists works when not set 2" {
18+
core.err_clear
19+
20+
! core.err_exists
21+
}
22+
23+
@test "core.err_set sets variables correctly" {
24+
core.err_set 2 "value_woof"
25+
26+
[ "$ERRCODE" = 2 ]
27+
[ "$ERR" = 'value_woof' ]
28+
}

0 commit comments

Comments
 (0)