blob: 3eae91c4f04016699610c99360c27ddf38cc063e [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
Dave Rodgman00bc7902024-02-26 11:43:11 +000014NO_SILENCE=" --version | test"
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000015
16TOOL=$(basename "$0")
17
18# Locate original tool
Dave Rodgman00bc7902024-02-26 11:43:11 +000019ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$0" | head -n1 )
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000020
Dave Rodgman00bc7902024-02-26 11:43:11 +000021print_quoted_args() {
Dave Rodgman90dbba52024-02-15 14:39:48 +000022 # similar to printf '%q' "$@"
23 # but produce more human-readable results for common/simple cases like "a b"
Dave Rodgman00bc7902024-02-26 11:43:11 +000024 for a in "$@"; do
25 simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-./:@]*)$'
Dave Rodgman90dbba52024-02-15 14:39:48 +000026 if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then
27 # a has spaces, but no other special characters that need escaping
28 # (quoting after removing spaces yields no backslashes)
Dave Rodgman11106982024-02-15 16:04:36 +000029 # simplify quoted form - e.g.:
30 # a b -> "a b"
31 # CFLAGS=a b -> CFLAGS="a b"
32 q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\""
Dave Rodgman90dbba52024-02-15 14:39:48 +000033 else
Dave Rodgman00bc7902024-02-26 11:43:11 +000034 # get bash to do the quoting (which may result in no quotes or escaping,
35 # if none is needed).
Dave Rodgman90dbba52024-02-15 14:39:48 +000036 q=$(printf '%q' "$a")
37 fi
Dave Rodgman00bc7902024-02-26 11:43:11 +000038 printf "%s " "$q"
Dave Rodgman90dbba52024-02-15 14:39:48 +000039 done
Dave Rodgman90dbba52024-02-15 14:39:48 +000040}
41
Dave Rodgman00bc7902024-02-26 11:43:11 +000042if [[ ! " $* " =~ " --version " ]]; then
Dave Rodgman0fa6b362024-02-15 12:27:03 +000043 # 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 Rodgman00bc7902024-02-26 11:43:11 +000046 printf %s "${TOOL} "
47 print_quoted_args "$@"
48 echo
Dave Rodgman0fa6b362024-02-15 12:27:03 +000049fi
50
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000051if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
Dave Rodgman0fa6b362024-02-15 12:27:03 +000052 # Run original command with no output supression
Dave Rodgman00bc7902024-02-26 11:43:11 +000053 exec "${ORIGINAL_TOOL}" "$@"
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000054else
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000055 # Run original command and capture output & exit status
Dave Rodgman00bc7902024-02-26 11:43:11 +000056 TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX")
57 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000058 EXIT_STATUS=$?
59
60 if [[ $EXIT_STATUS -ne 0 ]]; then
61 # On error, display the full output
Dave Rodgman00bc7902024-02-26 11:43:11 +000062 cat "${TMPFILE}"
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000063 fi
64
65 # Remove tmpfile
Dave Rodgman00bc7902024-02-26 11:43:11 +000066 rm "${TMPFILE}"
Dave Rodgman3e2c61d2024-01-04 16:20:20 +000067
Dave Rodgman00bc7902024-02-26 11:43:11 +000068 # Propagate the exit status
69 exit $EXIT_STATUS
70fi