From 897d84f0f9f9575046785ca774d55841d3368641 Mon Sep 17 00:00:00 2001 From: Casper Date: Sat, 29 Oct 2022 11:04:35 +0200 Subject: [PATCH] scripts: Improve error handling, when parameters are missing (#2854) --- CHANGELOG.md | 1 + target/bin/addalias | 2 +- target/bin/adddovecotmasteruser | 2 +- target/bin/addmailuser | 2 +- target/bin/addrelayhost | 2 +- target/bin/addsaslpassword | 2 +- target/bin/delalias | 2 +- target/bin/deldovecotmasteruser | 5 +---- target/bin/delmailuser | 5 ++--- target/bin/delquota | 2 +- target/bin/excluderelaydomain | 3 +-- target/bin/open-dkim | 2 +- target/bin/setquota | 2 +- target/bin/updatedovecotmasteruser | 2 +- target/bin/updatemailuser | 2 +- target/scripts/helpers/utils.sh | 10 ++++++++++ 16 files changed, 26 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 167994e0..b3e0e3aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file. The format ### Fixed +- **scripts**: Improve error handling, when parameters are missing ([#2854](https://github.com/docker-mailserver/docker-mailserver/pull/2854)) - **scripts**: Fix unbound variable error ([#2849](https://github.com/docker-mailserver/docker-mailserver/pull/2849), [#2853](https://github.com/docker-mailserver/docker-mailserver/pull/2853)) - **scripts**: Make fetchmail data persistant ([#2851](https://github.com/docker-mailserver/docker-mailserver/pull/2851)) - **scripts**: Run `user-patches.sh` right before starting daemons ([#2817](https://github.com/docker-mailserver/docker-mailserver/pull/2817)) diff --git a/target/bin/addalias b/target/bin/addalias index 12941886..a58bd7f8 100755 --- a/target/bin/addalias +++ b/target/bin/addalias @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 2 "${@}" local MAIL_ALIAS="${1}" local RECIPIENT="${2}" diff --git a/target/bin/adddovecotmasteruser b/target/bin/adddovecotmasteruser index c59a980c..79bc51ad 100755 --- a/target/bin/adddovecotmasteruser +++ b/target/bin/adddovecotmasteruser @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" shift diff --git a/target/bin/addmailuser b/target/bin/addmailuser index 8dd5406b..2b6f3556 100755 --- a/target/bin/addmailuser +++ b/target/bin/addmailuser @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" shift diff --git a/target/bin/addrelayhost b/target/bin/addrelayhost index a486db46..5463f15e 100755 --- a/target/bin/addrelayhost +++ b/target/bin/addrelayhost @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 2 "${@}" local DOMAIN="${1}" local HOST="${2}" diff --git a/target/bin/addsaslpassword b/target/bin/addsaslpassword index 5c88246c..27dd67cd 100755 --- a/target/bin/addsaslpassword +++ b/target/bin/addsaslpassword @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 2 "${@}" local DOMAIN="${1}" local RELAY_ACCOUNT="${2}" diff --git a/target/bin/delalias b/target/bin/delalias index 0afc53d8..c8b27745 100755 --- a/target/bin/delalias +++ b/target/bin/delalias @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 2 "${@}" local MAIL_ALIAS="${1}" local RECIPIENT="${2}" diff --git a/target/bin/deldovecotmasteruser b/target/bin/deldovecotmasteruser index 532eb99f..efd4e781 100755 --- a/target/bin/deldovecotmasteruser +++ b/target/bin/deldovecotmasteruser @@ -5,10 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } - - # Validate Parameters: - [[ -z ${*} ]] && { __usage ; _exit_with_error 'No account specified' ; } + _require_n_parameters_or_print_usage 1 "${@}" # Actual command to perform: for MAIL_ACCOUNT in "${@}" diff --git a/target/bin/delmailuser b/target/bin/delmailuser index f9b15e2d..0621e6b2 100755 --- a/target/bin/delmailuser +++ b/target/bin/delmailuser @@ -5,12 +5,11 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" + # Tests expect early exit without error if no DB exists: [[ -s ${DATABASE_ACCOUNTS} ]] || return 0 - # Validate Parameters: - [[ -z ${*} ]] && { __usage ; _exit_with_error 'No account specified' ; } _maildel_request_if_missing # TODO: May want to lock all database files prior to loop? (DATABASE_ACCOUNTS DATABASE_QUOTA DATABASE_VIRTUAL) diff --git a/target/bin/delquota b/target/bin/delquota index 0a5e4c0f..1282a827 100755 --- a/target/bin/delquota +++ b/target/bin/delquota @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" diff --git a/target/bin/excluderelaydomain b/target/bin/excluderelaydomain index 94f38965..d6bdc2a9 100755 --- a/target/bin/excluderelaydomain +++ b/target/bin/excluderelaydomain @@ -5,10 +5,9 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local DOMAIN="${1}" - [[ -z ${DOMAIN} ]] && { __usage ; _exit_with_error 'No domain specified' ; } _exclude_domain_from_relayhosts } diff --git a/target/bin/open-dkim b/target/bin/open-dkim index 5da2ba03..1c461ae0 100755 --- a/target/bin/open-dkim +++ b/target/bin/open-dkim @@ -51,7 +51,7 @@ ${ORANGE}EXIT STATUS${RESET} " } -[[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } +_require_n_parameters_or_print_usage 0 "${@}" while [[ ${#} -gt 0 ]] do diff --git a/target/bin/setquota b/target/bin/setquota index c15b995d..cfb1dc42 100755 --- a/target/bin/setquota +++ b/target/bin/setquota @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" shift diff --git a/target/bin/updatedovecotmasteruser b/target/bin/updatedovecotmasteruser index 621f116b..bc89f882 100755 --- a/target/bin/updatedovecotmasteruser +++ b/target/bin/updatedovecotmasteruser @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" shift diff --git a/target/bin/updatemailuser b/target/bin/updatemailuser index 327bcde6..5d2f8012 100755 --- a/target/bin/updatemailuser +++ b/target/bin/updatemailuser @@ -5,7 +5,7 @@ source /usr/local/bin/helpers/index.sh function _main { - [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + _require_n_parameters_or_print_usage 1 "${@}" local MAIL_ACCOUNT="${1}" shift diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index 04601ad4..573fb4a2 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -34,3 +34,13 @@ function _chown_var_mail_if_necessary chown -R 5000:5000 /var/mail || return 1 fi } + +function _require_n_parameters_or_print_usage +{ + local COUNT + COUNT=${1} + shift + + [[ ${1:-} == 'help' ]] && { __usage ; exit 0 ; } + [[ ${#} -lt ${COUNT} ]] && { __usage ; exit 1 ; } +}