docker-mailserver/test/linting/lint.sh

168 lines
4.6 KiB
Bash
Raw Normal View History

2020-11-06 13:04:23 +00:00
#! /bin/bash
2021-06-01 16:12:17 +00:00
# version v0.2.0 unstable
# executed by Make during CI or manually
# task checks files against linting targets
SCRIPT="lint.sh"
if [[ "$(uname)" == "Darwin" ]]
then
readlink() {
greadlink "${@:+$@}" # Requires coreutils
}
fi
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
REPO_ROOT="$(realpath "${SCRIPT_DIR}"/../../)"
HADOLINT_VERSION=2.8.0
ECLINT_VERSION=2.3.5
SHELLCHECK_VERSION=0.8.0
set -eEuo pipefail
shopt -s inherit_errexit
2021-06-01 16:12:17 +00:00
trap '__log_err "${FUNCNAME[0]:-?}" "${BASH_COMMAND:-?}" ${LINENO:-?} ${?:-?}' ERR
function __log_err
{
printf "\n--- \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
" - script = ${SCRIPT:-${0}}" \
" - function = ${1} / ${2}" \
" - line = ${3}" \
" - exit code = ${4}"
}
function __log_info
{
printf "\n--- \e[34m%s\e[0m\n%s\n%s\n\n" \
2020-12-22 19:07:08 +00:00
"${SCRIPT:-${0}}" \
" - type = INFO" \
" - version = ${*}"
}
function __log_failure
{
printf "\n--- \e[91m%s\e[0m\n%s\n%s\n\n" \
2020-12-22 19:07:08 +00:00
"${SCRIPT:-${0}}" \
" - type = FAILURE" \
" - message = ${*:-errors encountered}"
}
function __log_success
{
printf "\n--- \e[32m%s\e[0m\n%s\n%s\n\n" \
"${SCRIPT}" \
" - type = SUCCESS" \
" - message = no errors detected"
}
2021-06-01 16:12:17 +00:00
function __in_path
{
command -v "${@:+$@}" &>/dev/null && return 0 ; return 1 ;
2021-06-01 16:12:17 +00:00
}
function _eclint
{
local SCRIPT='EDITORCONFIG LINTER'
2021-06-01 16:12:17 +00:00
if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \
--workdir "/ci" \
--name eclint \
"mstruebing/editorconfig-checker:${ECLINT_VERSION}" ec -config "/ci/test/linting/.ecrc.json"
then
2020-12-22 19:07:08 +00:00
__log_success
else
__log_failure
return 1
fi
}
function _hadolint
{
local SCRIPT='HADOLINT'
if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \
--workdir "/ci" \
"hadolint/hadolint:v${HADOLINT_VERSION}-alpine" hadolint --config "/ci/test/linting/.hadolint.yaml" Dockerfile
then
2020-12-22 19:07:08 +00:00
__log_success
else
__log_failure
return 1
fi
}
function _shellcheck
{
local SCRIPT='SHELLCHECK'
fix: `check-for-changes.sh` should not fall out of sync with shared logic (#2260) Removes duplicate logic from `check-for-changes.sh` that is used/maintained elsewhere to avoid risk of problems, as this code is already starting to diverge / rot. --- Previously the change detection support has had code added for rebuilding config upon change detection which is the same as code run during startup scripts. Unfortunately over time this has fallen out of sync. Mostly the startup scripts would get maintenance and the contributor and reviewers may not have been aware of the duplicate code handled by `check-for-changes.sh`. That code was starting to diverge in addition to some changes in structure (_eg: relay host logic seems interleaved here vs separated out in startup scripts_). I wanted to address this before it risks becoming a much bigger headache. Rather than bloat `helper-functions.sh` further, I've added a `helpers/` folder extracting relevant common logic between startup scripts and `changedetector`. If you want to follow that process I've kept scoped commits to make those diffs easier. Some minor changes/improvements were added but nothing significant. --- - chore: Extract relay host logic to new `relay.sh` helper - chore: Extract `/etc/postfix/sasl_passwd` logic to new `sasl.sh` helper - chore: Extract `postfix-accounts.cf` logic to new `accounts.sh` helper - chore: Extract `/etc/aliases` logic to new `aliases.sh` helper - chore: Extract `/etc/postfix/vhost` logic to new `postfix.sh` helper - chore: Add inline docs for Postfix configs > These are possibly more verbose than needed and can be reduced at a later stage. > They are helpful during this refactor process while investigating that everything is handled correctly. `accounts.sh`: - Add note regarding potential bug for bare domain setups with `/etc/postfix/vhost` and `mydestination` sharing same domain value. `relay.sh`: - Remove the tabs for a single space delimiter, revised associated comment. - Add PR reference for original `_populate_relayhost_map` implementation which has some useful details. Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Co-authored-by: Casper <casperklein@users.noreply.github.com>
2021-11-20 20:33:49 +00:00
# File paths for shellcheck:
F_SH="$(find . -type f -iname '*.sh' \
-not -path './test/bats/*' \
-not -path './test/test_helper/*' \
-not -path './target/docker-configomat/*'
)"
# macOS lacks parity for `-executable` but presently produces the same results: https://stackoverflow.com/a/4458361
[[ "$(uname)" == "Darwin" ]] && FIND_EXEC="-perm +111 -type l -or" || FIND_EXEC="-executable"
# shellcheck disable=SC2248
F_BIN="$(find 'target/bin' ${FIND_EXEC} -type f)"
F_BATS="$(find 'test' -maxdepth 1 -type f -iname '*.bats')"
fix: `check-for-changes.sh` should not fall out of sync with shared logic (#2260) Removes duplicate logic from `check-for-changes.sh` that is used/maintained elsewhere to avoid risk of problems, as this code is already starting to diverge / rot. --- Previously the change detection support has had code added for rebuilding config upon change detection which is the same as code run during startup scripts. Unfortunately over time this has fallen out of sync. Mostly the startup scripts would get maintenance and the contributor and reviewers may not have been aware of the duplicate code handled by `check-for-changes.sh`. That code was starting to diverge in addition to some changes in structure (_eg: relay host logic seems interleaved here vs separated out in startup scripts_). I wanted to address this before it risks becoming a much bigger headache. Rather than bloat `helper-functions.sh` further, I've added a `helpers/` folder extracting relevant common logic between startup scripts and `changedetector`. If you want to follow that process I've kept scoped commits to make those diffs easier. Some minor changes/improvements were added but nothing significant. --- - chore: Extract relay host logic to new `relay.sh` helper - chore: Extract `/etc/postfix/sasl_passwd` logic to new `sasl.sh` helper - chore: Extract `postfix-accounts.cf` logic to new `accounts.sh` helper - chore: Extract `/etc/aliases` logic to new `aliases.sh` helper - chore: Extract `/etc/postfix/vhost` logic to new `postfix.sh` helper - chore: Add inline docs for Postfix configs > These are possibly more verbose than needed and can be reduced at a later stage. > They are helpful during this refactor process while investigating that everything is handled correctly. `accounts.sh`: - Add note regarding potential bug for bare domain setups with `/etc/postfix/vhost` and `mydestination` sharing same domain value. `relay.sh`: - Remove the tabs for a single space delimiter, revised associated comment. - Add PR reference for original `_populate_relayhost_map` implementation which has some useful details. Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Co-authored-by: Casper <casperklein@users.noreply.github.com>
2021-11-20 20:33:49 +00:00
# This command is a bit easier to grok as multi-line.
# There is a `.shellcheckrc` file, but it's only supports half of the options below, thus kept as CLI:
# `SCRIPTDIR` is a special value that represents the path of the script being linted,
# all sourced scripts share the same SCRIPTDIR source-path of the original script being linted.
CMD_SHELLCHECK=(shellcheck
--external-sources
--check-sourced
--severity=style
--color=auto
--wiki-link-count=50
--enable=all
--exclude=SC2154
--exclude=SC2310
--exclude=SC2311
--exclude=SC2312
--source-path=SCRIPTDIR
"${F_SH} ${F_BIN} ${F_BATS}"
)
fix: `check-for-changes.sh` should not fall out of sync with shared logic (#2260) Removes duplicate logic from `check-for-changes.sh` that is used/maintained elsewhere to avoid risk of problems, as this code is already starting to diverge / rot. --- Previously the change detection support has had code added for rebuilding config upon change detection which is the same as code run during startup scripts. Unfortunately over time this has fallen out of sync. Mostly the startup scripts would get maintenance and the contributor and reviewers may not have been aware of the duplicate code handled by `check-for-changes.sh`. That code was starting to diverge in addition to some changes in structure (_eg: relay host logic seems interleaved here vs separated out in startup scripts_). I wanted to address this before it risks becoming a much bigger headache. Rather than bloat `helper-functions.sh` further, I've added a `helpers/` folder extracting relevant common logic between startup scripts and `changedetector`. If you want to follow that process I've kept scoped commits to make those diffs easier. Some minor changes/improvements were added but nothing significant. --- - chore: Extract relay host logic to new `relay.sh` helper - chore: Extract `/etc/postfix/sasl_passwd` logic to new `sasl.sh` helper - chore: Extract `postfix-accounts.cf` logic to new `accounts.sh` helper - chore: Extract `/etc/aliases` logic to new `aliases.sh` helper - chore: Extract `/etc/postfix/vhost` logic to new `postfix.sh` helper - chore: Add inline docs for Postfix configs > These are possibly more verbose than needed and can be reduced at a later stage. > They are helpful during this refactor process while investigating that everything is handled correctly. `accounts.sh`: - Add note regarding potential bug for bare domain setups with `/etc/postfix/vhost` and `mydestination` sharing same domain value. `relay.sh`: - Remove the tabs for a single space delimiter, revised associated comment. - Add PR reference for original `_populate_relayhost_map` implementation which has some useful details. Co-authored-by: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Co-authored-by: Casper <casperklein@users.noreply.github.com>
2021-11-20 20:33:49 +00:00
# The linter can reference additional source-path values declared in scripts,
# which in our case rarely benefit from extending from `SCRIPTDIR` and instead
# should use a relative path from the project root (mounted at `/ci`), eg `target/scripts/`.
# Note that `SCRIPTDIR` will strip a prefix variable for a source path, which can be useful
# if `SCRIPTDIR` would always be the same value, and combined with relative path via another
# `source-path=SCRIPTDIR/relative/path/to/scripts` in the .sh file.
# These source-path values can apply to the entire file (and sourced files) if not wrapped in a function scope.
# Otherwise it only applies to the line below it. You can declare multiple source-paths, they don't override the previous.
# `source=relative/path/to/file.sh` will check the source value in each source-path as well.
# shellcheck disable=SC2068
if docker run --rm --tty \
--volume "${REPO_ROOT}:/ci:ro" \
--workdir "/ci" \
"koalaman/shellcheck-alpine:v${SHELLCHECK_VERSION}" ${CMD_SHELLCHECK[@]}
then
2020-12-22 19:07:08 +00:00
__log_success
else
__log_failure
return 1
fi
}
2021-06-01 16:12:17 +00:00
function __main
{
2021-06-01 16:12:17 +00:00
case "${1:-}" in
'eclint' ) _eclint ;;
'hadolint' ) _hadolint ;;
'shellcheck' ) _shellcheck ;;
*)
2021-06-01 16:12:17 +00:00
__log_failure "'${1:-}' is not a command nor an option."
return 3
;;
esac
}
__main "${@:+$@}" || exit ${?}