From 6385b402f7665e4cc3a7308ace34a82cf57dd129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Tue, 3 Oct 2023 15:48:49 +0200 Subject: [PATCH 1/2] Add ynh_exec_stderr_on_error that only prints stderr when command fails --- helpers/logging | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/helpers/logging b/helpers/logging index ab5d564aa..8989140f6 100644 --- a/helpers/logging +++ b/helpers/logging @@ -186,6 +186,26 @@ ynh_exec_fully_quiet() { fi } +# Execute a command and redirect stderr in /dev/null. Print stderr on error. +# +# usage: ynh_exec_stderr_on_error your command and args +# | arg: command - command to execute +# +# Note that you should NOT quote the command but only prefix it with ynh_exec_stderr_on_error +# +# Requires YunoHost version 11.2 or higher. +ynh_exec_stderr_on_error() { + logfile="$(mktemp)" + rc=0 + # Note that "$@" is used and not $@, c.f. https://unix.stackexchange.com/a/129077 + "$@" 2> "$logfile" || rc="$?" + if (( rc != 0 )); then + ynh_exec_warn cat "$logfile" + ynh_secure_remove "$logfile" + return "$rc" + fi +} + # Remove any logs for all the following commands. # # usage: ynh_print_OFF @@ -248,7 +268,7 @@ ynh_script_progression() { # Re-disable xtrace, ynh_handle_getopts_args set it back set +o xtrace # set +x weight=${weight:-1} - + # Always activate time when running inside CI tests if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then time=${time:-1} From bfb7dda42e11b294552ca09564ef48910518285d Mon Sep 17 00:00:00 2001 From: Alexandre Aubin <4533074+alexAubin@users.noreply.github.com> Date: Mon, 30 Oct 2023 14:03:26 +0100 Subject: [PATCH 2/2] Rename helper into "ynh_exec_and_print_stderr_only_if_error" --- helpers/logging | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/helpers/logging b/helpers/logging index 8989140f6..accb8f9b0 100644 --- a/helpers/logging +++ b/helpers/logging @@ -188,13 +188,13 @@ ynh_exec_fully_quiet() { # Execute a command and redirect stderr in /dev/null. Print stderr on error. # -# usage: ynh_exec_stderr_on_error your command and args +# usage: ynh_exec_and_print_stderr_only_if_error your command and args # | arg: command - command to execute # -# Note that you should NOT quote the command but only prefix it with ynh_exec_stderr_on_error +# Note that you should NOT quote the command but only prefix it with ynh_exec_and_print_stderr_only_if_error # # Requires YunoHost version 11.2 or higher. -ynh_exec_stderr_on_error() { +ynh_exec_and_print_stderr_only_if_error() { logfile="$(mktemp)" rc=0 # Note that "$@" is used and not $@, c.f. https://unix.stackexchange.com/a/129077