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 " |
| 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 |
Dave Rodgman | 634fe90 | 2024-02-15 16:04:36 +0000 | [diff] [blame] | 27 | simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-]*)$' |
Dave Rodgman | d4a5563 | 2024-02-15 14:39:48 +0000 | [diff] [blame] | 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) |
Dave Rodgman | 634fe90 | 2024-02-15 16:04:36 +0000 | [diff] [blame] | 31 | # simplify quoted form - e.g.: |
| 32 | # a b -> "a b" |
| 33 | # CFLAGS=a b -> CFLAGS="a b" |
| 34 | q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\"" |
Dave Rodgman | d4a5563 | 2024-02-15 14:39:48 +0000 | [diff] [blame] | 35 | else |
| 36 | # get bash to do the quoting |
| 37 | q=$(printf '%q' "$a") |
| 38 | fi |
| 39 | s="$s $q" |
| 40 | done |
| 41 | echo $s |
| 42 | } |
| 43 | |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 44 | if [[ ! " $@ " =~ " --version " ]]; then |
| 45 | # Display the command being invoked - if it succeeds, this is all that will |
| 46 | # be displayed. Don't do this for invocations with --version, because |
| 47 | # 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] | 48 | echo "${TOOL} $(quote_args "$@")" |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 49 | fi |
| 50 | |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 51 | if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then |
Dave Rodgman | 59f9df9 | 2024-02-15 12:27:03 +0000 | [diff] [blame] | 52 | # Run original command with no output supression |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 53 | ${ORIGINAL_TOOL} "$@" |
| 54 | EXIT_STATUS=$? |
| 55 | else |
Dave Rodgman | 99fa0d0 | 2024-01-04 16:20:20 +0000 | [diff] [blame] | 56 | # Run original command and capture output & exit status |
| 57 | TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) |
| 58 | ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 |
| 59 | EXIT_STATUS=$? |
| 60 | |
| 61 | if [[ $EXIT_STATUS -ne 0 ]]; then |
| 62 | # On error, display the full output |
| 63 | cat ${TMPFILE} |
| 64 | fi |
| 65 | |
| 66 | # Remove tmpfile |
| 67 | rm ${TMPFILE} |
| 68 | fi |
| 69 | |
| 70 | # Propagate the exit status |
| 71 | exit $EXIT_STATUS |