Skip to content

Commit 1cbd3df

Browse files
authored
Merge pull request #1762 from larsewi/run-and-print-on-failure
functions: Run command and print on failure
2 parents 5b3b413 + 8bf603e commit 1cbd3df

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

build-scripts/functions

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,5 +596,25 @@ rm_if_empty()
596596
fi
597597
}
598598

599+
# Use this function on verbose commands to silence the output unless it returns
600+
# a non-zero exit code
601+
run_and_print_on_failure()
602+
{
603+
local temp_output_file
604+
temp_output_file=$(mktemp)
605+
local exit_code=0
606+
if "$@" > "$temp_output_file" 2>&1; then
607+
: # NOOP
608+
else
609+
exit_code=$? # Store exit code for later
610+
echo "Error: Failed to run:" "$@"
611+
echo "--- Start of Output ---"
612+
cat "$temp_output_file"
613+
echo "--- End of Output (Error Code: $exit_code) ---"
614+
fi
615+
616+
rm -f "$temp_output_file"
617+
return $exit_code
618+
}
599619

600620
_IS_FUNCTIONS_SOURCED=yes

0 commit comments

Comments
 (0)