Skip to content

Commit 31a86b4

Browse files
committed
improve
1 parent da78b96 commit 31a86b4

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

usr/bin/text-safety-scan

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ set -o pipefail
1313
# shellcheck source=../libexec/helper-scripts/log_run_die.sh
1414
source /usr/libexec/helper-scripts/log_run_die.sh
1515

16+
log_level=info
17+
1618
run_plugin_command() {
17-
local plugin_cmd plugin_args
19+
declare -g plugin_cmd
20+
local plugin_args
1821

1922
plugin_cmd="$1"
2023
shift
@@ -26,6 +29,8 @@ run_plugin_command() {
2629
else
2730
"${plugin_cmd}" "${plugin_args[@]}"
2831
fi
32+
33+
log info "${plugin_cmd} OK"
2934
}
3035

3136
cleanup() {
@@ -34,11 +39,27 @@ cleanup() {
3439
## captured_stdin_file is inherited from text_safety_scan function.
3540
## captured_stdin_file is set prior setting up the 'trap'.
3641
if [ -n "${captured_stdin_file-}" ]; then
37-
safe-rm -f -- "${captured_stdin_file}"
42+
safe-rm --force --verbose -- "${captured_stdin_file}"
43+
fi
44+
if [ "$_exit_code" = "0" ]; then
45+
log notice "OK"
46+
else
47+
log error "FAIL"
3848
fi
3949
exit "${_exit_code}"
4050
}
4151

52+
error_handler() {
53+
local _exit_code="${1:-$?}"
54+
trap - ERR
55+
log error "FAILED: ${plugin_cmd-"${BASH_COMMAND}"}"
56+
exit "${_exit_code}"
57+
}
58+
59+
trap 'cleanup' EXIT
60+
61+
trap "error_handler" ERR
62+
4263
text_safety_scan() {
4364
local file_list captured_stdin_file file_name plugin_list plugin_item
4465

@@ -55,15 +76,12 @@ text_safety_scan() {
5576
if ! captured_stdin_file="$(mktemp)"; then
5677
die 1 'Could not create temporary file for capturing stdin!'
5778
fi
58-
trap 'cleanup' EXIT
5979

6080
cat | sponge -- "${captured_stdin_file}"
6181

6282
for plugin_item in "${plugin_list[@]}"; do
6383
run_plugin_command "${plugin_item}"
6484
done
65-
66-
cleanup
6785
else
6886
for file_name in "${file_list[@]}"; do
6987
if ! test -r "$file_name"; then

0 commit comments

Comments
 (0)