From 9d44e1d84bc5ad13d054265058a084ffafc0ebcb Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Fri, 17 Mar 2017 16:16:36 +0100 Subject: [PATCH] [enh] New helper ynh_abort_if_errors (#245) * New helper ynh_check_error Helpers to implemente trap and set -eu. Simply to use, you only add `ynh_check_error` in your script. * Change helper's name * Propagate new name in comments --- data/helpers.d/system | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 data/helpers.d/system diff --git a/data/helpers.d/system b/data/helpers.d/system new file mode 100644 index 000000000..5f2ad385b --- /dev/null +++ b/data/helpers.d/system @@ -0,0 +1,43 @@ +# Manage a fail of the script +# +# Print a warning to inform that the script was failed +# Execute the ynh_clean_setup function if used in the app script +# +# usage of ynh_clean_setup function +# This function provide a way to clean some residual of installation that not managed by remove script. +# To use it, simply add in your script: +# ynh_clean_setup () { +# instructions... +# } +# This function is optionnal. +# +# Usage: ynh_exit_properly is used only by the helper ynh_abort_if_errors. +# You must not use it directly. +ynh_exit_properly () { + exit_code=$? + if [ "$exit_code" -eq 0 ]; then + exit 0 # Exit without error if the script ended correctly + fi + + trap '' EXIT # Ignore new exit signals + set +eu # Do not exit anymore if a command fail or if a variable is empty + + echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2 + + if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script. + ynh_clean_setup # Call the function to do specific cleaning for the app. + fi + + ynh_die # Exit with error status +} + +# Exit if an error occurs during the execution of the script. +# +# Stop immediatly the execution if an error occured or if a empty variable is used. +# The execution of the script is derivate to ynh_exit_properly function before exit. +# +# Usage: ynh_abort_if_errors +ynh_abort_if_errors () { + set -eu # Exit if a command fail, and if a variable is used unset. + trap ynh_exit_properly EXIT # Capturing exit signals on shell script +}