improve helper for sending

Now, the print the `swaks` output (transaction) (required for the next
commit) and they share a common helper which greatly eases parsing
arguments & Co.
This commit is contained in:
georglauterbach 2024-01-07 00:35:04 +01:00
parent ab1c72a458
commit 424f6d3af8
No known key found for this signature in database
GPG Key ID: F367F7C43C118578
1 changed files with 60 additions and 28 deletions

View File

@ -7,25 +7,14 @@
# ! ATTENTION: This file is loaded by `common.sh` - do not load it yourself! # ! ATTENTION: This file is loaded by `common.sh` - do not load it yourself!
# ! ATTENTION: This file requires helper functions from `common.sh`! # ! ATTENTION: This file requires helper functions from `common.sh`!
# Sends a mail from localhost (127.0.0.1) to a container. To send # Parse the arguments given to `_send_email` and `_send_email_unchecked`
# a custom email, create a file at `test/files/<TEST FILE>`, # and print the command that is to be exuted in these functions. How these
# and provide `<TEST FILE>` as an argument to this function. # functions then handle the result of the invocation depends on the function.
# #
# Parameters include all options that one can supply to `swaks` itself. # ## Note
# The `--data` parameter expects a relative path from `emails/`.
# #
# This functions performs **no** implicit `assert_success` to check whether # This function is internal and should not be used in tests.
# the e-mail transaction was successful. If this is not desirable, use function __parse_swaks_arguments() {
# `_send_email`.
#
# ## Attention
#
# This function assumes `CONTAINER_NAME` to be properly set (to the container
# name the command should be executed in)!
#
# This function will send the email in an "asynchronous" fashion,
# it will return without waiting for the Postfix mail queue to be emptied.
function _send_email_unchecked() {
[[ -v CONTAINER_NAME ]] || return 1 [[ -v CONTAINER_NAME ]] || return 1
# Parameter defaults common to our testing needs: # Parameter defaults common to our testing needs:
@ -68,28 +57,71 @@ function _send_email_unchecked() {
ADDITIONAL_SWAKS_OPTIONS+=("'Date: %DATE%\nTo: %TO_ADDRESS%\nFrom: %FROM_ADDRESS%\nSubject: test %DATE%\n%NEW_HEADERS%\n%BODY%\n'") ADDITIONAL_SWAKS_OPTIONS+=("'Date: %DATE%\nTo: %TO_ADDRESS%\nFrom: %FROM_ADDRESS%\nSubject: test %DATE%\n%NEW_HEADERS%\n%BODY%\n'")
fi fi
echo "swaks --server '${SERVER}' --port '${PORT}' --ehlo '${EHLO}' --from '${FROM}' --to '${TO}' ${ADDITIONAL_SWAKS_OPTIONS[*]}" >/tmp/lol echo "swaks --server '${SERVER}' --port '${PORT}' --ehlo '${EHLO}' --from '${FROM}' --to '${TO}' ${ADDITIONAL_SWAKS_OPTIONS[*]}"
_run_in_container_bash "swaks --server '${SERVER}' --port '${PORT}' --ehlo '${EHLO}' --from '${FROM}' --to '${TO}' ${ADDITIONAL_SWAKS_OPTIONS[*]}"
} }
# Sends a mail from localhost (127.0.0.1) to a container. To send a custom email: # Sends a mail from the container named by the environment variable `CONTAINER_NAME`
# to the same or another container.
# #
# 1. Create a file at `test/files/<TEST FILE>` # To send a custom email, create a file at `test/files/<TEST FILE>`,
# 2. Provide `<TEST FILE>` as an argument to this function. # and provide `<TEST FILE>` as an argument to this function.
# #
# Parameters include all options that one can supply to `swaks` itself. # Parameters include all options that one can supply to `swaks` itself.
# The `--data` parameter expects a relative path from `emails/`. # The `--data` parameter expects either, a relative path from `emails/` or
# "inline" data, e.g., `Date: 1 Jan 2024\nSubject: This is a test`.
# #
# This functions performs an implicit `assert_success` to check whether # ## Output
# the e-mail transaction was successful. If this is not desirable, use #
# `_send_email_unchecked`. # This functions prints the output of the transaction that `swaks` prints.
# #
# ## Attention # ## Attention
# #
# Please see the 'Attention' section of `_send_email_unchecked`, # This function assumes `CONTAINER_NAME` to be properly set (to the container
# name the command should be executed in)!
#
# This function will send the email in an "asynchronous" fashion,
# it will return without waiting for the Postfix mail queue to be emptied.
#
# This functions performs **no** implicit `assert_success` to check whether
# the e-mail transaction was successful. If this is not desirable, use
# `_send_email`. If you anticipate the sending to succeed, use `_send_email`
# instead.
function _send_email_unchecked() {
local COMMAND_STRING=$(__parse_swaks_arguments "${@}")
_run_in_container_bash "${COMMAND_STRING}"
local RETURN_VALUE=${?}
# shellcheck disable=SC2154
echo "${output}"
return "${RETURN_VALUE}"
}
# Sends a mail from the container named by the environment variable `CONTAINER_NAME`
# to the same or another container.
#
# To send a custom email, create a file at `test/files/<TEST FILE>`,
# and provide `<TEST FILE>` as an argument to this function.
#
# Parameters include all options that one can supply to `swaks` itself.
# The `--data` parameter expects either, a relative path from `emails/` or
# "inline" data, e.g., `Date: 1 Jan 2024\nSubject: This is a test`.
#
# ## Output
#
# This functions prints the output of the transaction that `swaks` prints.
#
# ## Attention
#
# This function assumes `CONTAINER_NAME` to be properly set (to the container
# name the command should be executed in)!
#
# This function will send the email in an "asynchronous" fashion,
# it will return without waiting for the Postfix mail queue to be emptied.
function _send_email() { function _send_email() {
_send_email_unchecked "${@}" local COMMAND_STRING=$(__parse_swaks_arguments "${@}")
_run_in_container_bash "${COMMAND_STRING}"
assert_success assert_success
# shellcheck disable=SC2154
echo "${output}"
} }
# Like `_send_email` with two major differences: # Like `_send_email` with two major differences: