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 | d4a5563 | 2024-02-15 14:39:48 +0000 | [diff] [blame^] | 21 | quote_args() { |
| 22 | # similar to printf '%q' "$@" |
| 23 | # but produce more human-readable results for common/simple cases like "a b" |
| 24 | local args=("$@") |
| 25 | s="" |
| 26 | for a in "${args[@]}"; do |
| 27 | simple_pattern='^[[:alnum:] _=+-]*$' |
| 28 | if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then |
| 29 | # a has spaces, but no other special characters that need escaping |
| 30 | # (quoting after removing spaces yields no backslashes) |
| 31 | # simplify quoted form to "$a" - e.g. yield "a b c" instead of a\ b\ c |
| 32 | q="\"$a\"" |
| 33 | else |
| 34 | # get bash to do the quoting |
| 35 | q=$(printf '%q' "$a") |
| 36 | fi |
| 37 | s="$s $q" |
| 38 | done |
| 39 | echo $s |
| 40 | } |
| 41 | |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 42 | if [[ ! " $@ " =~ " --version " ]]; then |
| 43 | # Display the command being invoked - if it succeeds, this is all that will |
| 44 | # be displayed. Don't do this for invocations with --version, because |
| 45 | # this output is often parsed by scripts, so we don't want to modify it. |
Dave Rodgman | d4a5563 | 2024-02-15 14:39:48 +0000 | [diff] [blame^] | 46 | echo "${TOOL} $(quote_args "$@")" |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 47 | fi |
| 48 | |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 49 | if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 50 | # Run original command with no output supression |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 51 | ${ORIGINAL_TOOL} "$@" |
| 52 | EXIT_STATUS=$? |
| 53 | else |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 54 | # Run original command and capture output & exit status |
| 55 | TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) |
| 56 | ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 |
| 57 | EXIT_STATUS=$? |
| 58 | |
| 59 | if [[ $EXIT_STATUS -ne 0 ]]; then |
| 60 | # On error, display the full output |
| 61 | cat ${TMPFILE} |
| 62 | fi |
| 63 | |
| 64 | # Remove tmpfile |
| 65 | rm ${TMPFILE} |
| 66 | fi |
| 67 | |
| 68 | # Propagate the exit status |
| 69 | exit $EXIT_STATUS |