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