capture: set globals CAPTURED_VARS_NAMES and CAPTURED_VARS_ARRAY (instead of CAPTURED_VARS_ARRAY that was space-delimited); filter and sort last-minute

This commit is contained in:
Ricardo Pardini
2023-03-27 13:38:10 +02:00
committed by Igor Pečovnik
parent fb4477afb8
commit fc14d62c52

View File

@@ -9,24 +9,24 @@
function do_capturing_defs() { function do_capturing_defs() {
# make sure to local with a value, otherwise they will appear in the list... # make sure to local with a value, otherwise they will appear in the list...
local pre_exec_vars="" post_exec_vars="" new_vars_list="" onevar="" all_vars_array=() declare pre_exec_vars="" post_exec_vars="" new_vars_list="" onevar="" all_vars_array=()
pre_exec_vars="$(compgen -A variable | grep -E '[[:upper:]]+' | grep -v -e "^BASH_" | sort)" pre_exec_vars="$(compgen -A variable)"
# run parameters passed. if this fails, so will we, immediately, and not capture anything correctly. # run parameters passed. if this fails, so will we, immediately, and not capture anything correctly.
# if you ever find stacks referring here, please look at the caller and $1 # if you ever find stacks referring here, please look at the caller and $1
"$@" "$@"
post_exec_vars="$(compgen -A variable | grep -E '[[:upper:]]+' | grep -v -e "^BASH_" | sort)" post_exec_vars="$(compgen -A variable)"
new_vars_list="$(comm -13 <(echo "$pre_exec_vars") <(echo "${post_exec_vars}"))"
new_vars_list="$(comm -13 <(echo "$pre_exec_vars" | grep -E '[[:upper:]]+' | grep -v -e "^BASH_" | sort) <(echo "${post_exec_vars}" | grep -E '[[:upper:]]+' | grep -v -e "^BASH_" | sort))"
for onevar in ${new_vars_list}; do for onevar in ${new_vars_list}; do
# @TODO: rpardini: handle arrays and maps specially?
all_vars_array+=("$(declare -p "${onevar}")") all_vars_array+=("$(declare -p "${onevar}")")
done done
#IFS=$'\n'
export CAPTURED_VARS="${all_vars_array[*]}" declare -g CAPTURED_VARS_NAMES="${new_vars_list}"
#display_alert "Vars defined during ${*@Q}:" "${CAPTURED_VARS}" "debug" declare -ga CAPTURED_VARS_ARRAY=("${all_vars_array[@]}")
unset all_vars_array post_exec_vars new_vars_list pre_exec_vars onevar join_by unset all_vars_array post_exec_vars new_vars_list pre_exec_vars onevar
return 0 # return success explicitly , preemptively preventing short-circuit problems. return 0 # return success explicitly , preemptively preventing short-circuit problems.
} }