Dave Rodgman | 3e2c61d | 2024-01-04 16:20:20 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env bash |
| 2 | # |
| 3 | # Copyright The Mbed TLS Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 5 | # |
| 6 | # This swallows the output of the wrapped tool, unless there is an error. |
| 7 | # This helps reduce excess logging in the CI. |
| 8 | |
| 9 | # If you are debugging a build / CI issue, you can get complete unsilenced logs |
| 10 | # by un-commenting the following line (or setting VERBOSE_LOGS in your environment): |
| 11 | # VERBOSE_LOGS=1 |
| 12 | |
| 13 | # don't silence invocations containing these arguments |
| 14 | NO_SILENCE=" --version " |
| 15 | |
| 16 | TOOL=$(basename "$0") |
| 17 | |
| 18 | # Locate original tool |
| 19 | ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 ) |
| 20 | |
| 21 | if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then |
| 22 | ${ORIGINAL_TOOL} "$@" |
| 23 | EXIT_STATUS=$? |
| 24 | else |
| 25 | # Display the command being invoked - if it succeeds, this is all that will |
| 26 | # be displayed. |
| 27 | echo "${TOOL} $@" |
| 28 | |
| 29 | # Run original command and capture output & exit status |
| 30 | TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) |
| 31 | ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 |
| 32 | EXIT_STATUS=$? |
| 33 | |
| 34 | if [[ $EXIT_STATUS -ne 0 ]]; then |
| 35 | # On error, display the full output |
| 36 | cat ${TMPFILE} |
| 37 | fi |
| 38 | |
| 39 | # Remove tmpfile |
| 40 | rm ${TMPFILE} |
| 41 | fi |
| 42 | |
| 43 | # Propagate the exit status |
| 44 | exit $EXIT_STATUS |