Dave Rodgman | 99fa0d0 | 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 | test " |
| 15 | |
| 16 | TOOL=$(basename "$0") |
| 17 | |
| 18 | # Locate original tool |
| 19 | ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 ) |
| 20 | |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame^] | 21 | if [[ ! " $@ " =~ " --version " ]]; then |
| 22 | # Display the command being invoked - if it succeeds, this is all that will |
| 23 | # be displayed. Don't do this for invocations with --version, because |
| 24 | # this output is often parsed by scripts, so we don't want to modify it. |
| 25 | echo -n "${TOOL} " |
| 26 | printf '%q ' "$@" |
| 27 | echo |
| 28 | fi |
| 29 | |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 30 | if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame^] | 31 | # Run original command with no output supression |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 32 | ${ORIGINAL_TOOL} "$@" |
| 33 | EXIT_STATUS=$? |
| 34 | else |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 35 | # Run original command and capture output & exit status |
| 36 | TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) |
| 37 | ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 |
| 38 | EXIT_STATUS=$? |
| 39 | |
| 40 | if [[ $EXIT_STATUS -ne 0 ]]; then |
| 41 | # On error, display the full output |
| 42 | cat ${TMPFILE} |
| 43 | fi |
| 44 | |
| 45 | # Remove tmpfile |
| 46 | rm ${TMPFILE} |
| 47 | fi |
| 48 | |
| 49 | # Propagate the exit status |
| 50 | exit $EXIT_STATUS |