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