blob: 0c4869644ef0907fa591e2acef7d59fa7b03cc41 [file] [log] [blame]
Dave Rodgman3e2c61d2024-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 Rodgman0fa6b362024-02-15 12:27:03 +000021if [[ ! " $@ " =~ " --version " ]]; then
22 # Display the command being invoked - if it succeeds, this is all that will
23 # be displayed. Don't do this for invocations with --version, because
24 # this output is often parsed by scripts, so we don't want to modify it.
25 echo -n "${TOOL} "
26 printf '%q ' "$@"
27 echo
28fi
29
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000030if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
Dave Rodgman0fa6b362024-02-15 12:27:03 +000031 # Run original command with no output supression
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000032 ${ORIGINAL_TOOL} "$@"
33 EXIT_STATUS=$?
34else
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000035 # Run original command and capture output & exit status
36 TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX)
37 ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1
38 EXIT_STATUS=$?
39
40 if [[ $EXIT_STATUS -ne 0 ]]; then
41 # On error, display the full output
42 cat ${TMPFILE}
43 fi
44
45 # Remove tmpfile
46 rm ${TMPFILE}
47fi
48
49# Propagate the exit status
50exit $EXIT_STATUS