fix: Ensure parsed config has final newline appended (when possible)

This functionality was handled in `accounts.sh` via a similar sed command (that the linked references also offer).

`printf` is better for this, no shellcheck comment required either.

We additionally don't attempt to modify files that are read-only.
This commit is contained in:
polarathene 2024-01-24 18:26:29 +13:00
parent 3db863aa8e
commit 84f23d5936
2 changed files with 18 additions and 14 deletions

View File

@ -25,10 +25,6 @@ function _create_accounts() {
_log 'trace' "Regenerating postfix user list" _log 'trace' "Regenerating postfix user list"
echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox echo "# WARNING: this file is auto-generated. Modify ${DATABASE_ACCOUNTS} to edit the user list." > /etc/postfix/vmailbox
# checking that ${DATABASE_ACCOUNTS} ends with a newline
# shellcheck disable=SC1003
sed -i -e '$a\' "${DATABASE_ACCOUNTS}"
chown dovecot:dovecot "${DOVECOT_USERDB_FILE}" chown dovecot:dovecot "${DOVECOT_USERDB_FILE}"
chmod 640 "${DOVECOT_USERDB_FILE}" chmod 640 "${DOVECOT_USERDB_FILE}"
@ -163,10 +159,6 @@ function _create_masters() {
_log 'trace' "Regenerating dovecot masters list" _log 'trace' "Regenerating dovecot masters list"
# checking that ${DATABASE_DOVECOT_MASTERS} ends with a newline
# shellcheck disable=SC1003
sed -i -e '$a\' "${DATABASE_DOVECOT_MASTERS}"
chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}" chown dovecot:dovecot "${DOVECOT_MASTERDB_FILE}"
chmod 640 "${DOVECOT_MASTERDB_FILE}" chmod 640 "${DOVECOT_MASTERDB_FILE}"

View File

@ -17,13 +17,25 @@ function _escape_for_sed() {
# Returns input after filtering out lines that are: # Returns input after filtering out lines that are:
# empty, white-space, comments (`#` as the first non-whitespace character) # empty, white-space, comments (`#` as the first non-whitespace character)
function _get_valid_lines_from_file() { function _get_valid_lines_from_file() {
# Correctly detect missing final newline: _append_final_newline_if_missing "${1}"
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
}
function _append_final_newline_if_missing() {
# Correctly detect a missing final newline and fix it:
# https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716 # https://stackoverflow.com/questions/38746/how-to-detect-file-ends-in-newline#comment82380232_25749716
# https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file/441200#441200
# https://unix.stackexchange.com/questions/159557/how-to-non-invasively-test-for-write-access-to-a-file
if [[ $(tail -c1 "${1}" | wc -l) -gt 0 ]]; then if [[ $(tail -c1 "${1}" | wc -l) -gt 0 ]]; then
# Avoid fixing when the destination is read-only:
if [[ test -w "${1}" ]]; then
printf '\n' >> "${1}"
_log 'warn' "${1} was missing a final newline. This has been fixed."
else
_log 'warn' "${1} is missing a final newline. The last line will not be processed!" _log 'warn' "${1} is missing a final newline. The last line will not be processed!"
fi fi
fi
grep --extended-regexp --invert-match "^\s*$|^\s*#" "${1}" || true
} }
# Provide the name of an environment variable to this function # Provide the name of an environment variable to this function