blob: e3114a86fc4325f1fec17263ebebb231c1929781 [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 "
15
16TOOL=$(basename "$0")
17
18# Locate original tool
19ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 )
20
Dave Rodgman90dbba52024-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
27 simple_pattern='^[[:alnum:] _=+-]*$'
28 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)
31 # simplify quoted form to "$a" - e.g. yield "a b c" instead of a\ b\ c
32 q="\"$a\""
33 else
34 # get bash to do the quoting
35 q=$(printf '%q' "$a")
36 fi
37 s="$s $q"
38 done
39 echo $s
40}
41
Dave Rodgman0fa6b362024-02-15 12:27:03 +000042if [[ ! " $@ " =~ " --version " ]]; then
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 Rodgman90dbba52024-02-15 14:39:48 +000046 echo "${TOOL} $(quote_args "$@")"
Dave Rodgman0fa6b362024-02-15 12:27:03 +000047fi
48
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000049if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
Dave Rodgman0fa6b362024-02-15 12:27:03 +000050 # Run original command with no output supression
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000051 ${ORIGINAL_TOOL} "$@"
52 EXIT_STATUS=$?
53else
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000054 # Run original command and capture output & exit status
55 TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX)
56 ${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1
57 EXIT_STATUS=$?
58
59 if [[ $EXIT_STATUS -ne 0 ]]; then
60 # On error, display the full output
61 cat ${TMPFILE}
62 fi
63
64 # Remove tmpfile
65 rm ${TMPFILE}
66fi
67
68# Propagate the exit status
69exit $EXIT_STATUS