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