blob: bbc685cd8bca6d553b603d2ba8d9845f072855cc [file] [log] [blame]
Dave Rodgman98a79cd2024-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
Dave Rodgmane03088b2024-02-26 12:48:49 +000014TOOL_WITH_PATH=$(dirname "$0")/$TOOL
15ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$TOOL_WITH_PATH" | head -n1)
Dave Rodgman98a79cd2024-02-26 12:37:44 +000016
17print_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
38if [[ ! " $* " =~ " --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
45fi
46
47if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
48 # Run original command with no output supression
49 exec "${ORIGINAL_TOOL}" "$@"
50else
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
66fi