Skip to content

Commit

Permalink
cloud: Add assert()
Browse files Browse the repository at this point in the history
  • Loading branch information
spbnick committed Jan 19, 2024
1 parent c8dad0b commit a82d5a8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions kcidb/cloud/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ export TMPDIR=$(mktemp -d -t "kcidb_cloud.XXXXXXXXXX")
# Remove the directory with all the temporary files on exit
atexit_push "rm -Rf ${TMPDIR@Q}"

# Evaluate and execute a command string,
# exit shell with error message and status 1 if unsuccessfull.
# Args: [eval_arg]...
function assert()
{
# Use private global-style variable names
# to avoid clashes with "evaled" names
declare _ASSERT_ATTRS
declare _ASSERT_STATUS

# Prevent shell from exiting due to `set -e` if the command fails
read -rd '' _ASSERT_ATTRS < <(set +o) || [ $? == 1 ]
set +o errexit
(
eval "$_ASSERT_ATTRS"
eval "$@"
)
_ASSERT_STATUS=$?
eval "$_ASSERT_ATTRS"

if [ "$_ASSERT_STATUS" != 0 ]; then
echo "Assertion failed: $*" >&1
exit 1
fi
}

# Generate code declaring parameter variables with names and values passed
# through long-option command-line argument list, and assigning the positional
# arguments.
Expand Down

0 comments on commit a82d5a8

Please sign in to comment.