blob: 9722029083b7cab5ea7e37fd5dc7aaacbcd62145 [file] [log] [blame]
Dave Rodgman99fa0d02024-01-04 16:20:20 +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# don't silence invocations containing these arguments
14NO_SILENCE=" --version | test "
15
16TOOL=$(basename "$0")
17
18# Locate original tool
19ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 )
20
Dave Rodgmand4a55632024-02-15 14:39:48 +000021quote_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 Rodgman634fe902024-02-15 16:04:36 +000027 simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-]*)$'
Dave Rodgmand4a55632024-02-15 14:39:48 +000028 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 Rodgman634fe902024-02-15 16:04:36 +000031 # 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 Rodgmand4a55632024-02-15 14:39:48 +000035 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 Rodgman59f9df92024-02-15 12:27:03 +000044if [[ ! " $@ " =~ " --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 Rodgmand4a55632024-02-15 14:39:48 +000048 echo "${TOOL} $(quote_args "$@")"
Dave Rodgman59f9df92024-02-15 12:27:03 +000049fi
50
Dave Rodgman99fa0d02024-01-04 16:20:20 +000051if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
Dave Rodgman59f9df92024-02-15 12:27:03 +000052 # Run original command with no output supression
Dave Rodgman99fa0d02024-01-04 16:20:20 +000053 ${ORIGINAL_TOOL} "$@"
54 EXIT_STATUS=$?
55else
Dave Rodgman99fa0d02024-01-04 16:20:20 +000056 # 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}
68fi
69
70# Propagate the exit status
71exit $EXIT_STATUS