2016-12-24 13:52:00 +00:00
# Contributing
`docker-mailserver` is OpenSource. That means that you can contribute on enhancements, bug fixing or improving the documentation in the Wiki.
2020-09-09 15:19:48 +00:00
1. [Issues & PRs ](#issues--prs )
2020-10-06 12:45:55 +00:00
1. [Opening an Issue ](#opening-an-issue )
2020-09-09 15:19:48 +00:00
2. [Pull Request ](#pull-requests )
2. [Coding Style ](#coding-style )
1. [Bash and Shell ](#bash-and-shell )
2. [YAML ](#yaml )
2020-09-05 14:19:12 +00:00
## Issues & PRs
2020-10-06 12:45:55 +00:00
### Opening an Issue
2016-12-24 13:52:00 +00:00
2020-10-06 12:45:55 +00:00
When opening an issue, please provide details use case to let the community reproduce your problem. Please start the mail server with env `DMS_DEBUG=1` and paste the output into the issue. **Use the issue templates** to provide the necessary information. Issues which do not use these templates are not worked on and closed.
2016-12-24 13:52:00 +00:00
2020-09-05 14:19:12 +00:00
### Pull Requests
2016-12-24 13:52:00 +00:00
2017-01-19 12:31:04 +00:00
#### Submit a Pull-Request
You want to add a feature? Feel free to start creating an issue explaining what you want to do and how you're thinking doing it. Other users may have the same need and collaboration may lead to better results.
2016-12-24 13:52:00 +00:00
The development workflow is the following:
2020-10-06 12:45:55 +00:00
- Fork the project and clone your fork
2016-12-24 13:52:00 +00:00
- Create a branch using `git checkout -b branch_name` (you can use `issue-xxx` if fixing an existing issue)
2017-04-17 16:30:04 +00:00
- Run `git submodule init` and `git submodule update` to get the BATS submodules
2016-12-24 13:52:00 +00:00
- Code :-)
- Add integration tests in `test/tests.bats`
2019-10-18 12:42:00 +00:00
- Use `make clean all` to build image locally and run tests
2019-07-23 14:12:12 +00:00
Note that tests work on Linux only; they hang on Mac and Windows.
2017-01-19 12:31:04 +00:00
- Document your improvements in `README.md` or Wiki depending on content
2020-09-09 15:19:48 +00:00
- [Commit][commit], if possible with [signing your commit with a GPG key][gpg], push and make a pull-request
2016-12-24 13:52:00 +00:00
- Pull-request is automatically tested on Travis
2017-01-19 12:31:04 +00:00
- When tests are green, a review may be done
- When changed are validated, your branch is merged into `master`
2016-12-24 13:52:00 +00:00
- `master` is automatically tested on Travis
- Docker builds a new `latest` image
2020-09-05 14:19:12 +00:00
## Coding Style
### Bash and Shell
2020-09-23 08:21:37 +00:00
When refactoring, writing or altering scripts, that is Shell and Bash scripts, in any way, adhere to these rules:
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
1. **Adjust your style of coding to the style that is already present** ! Even if you do not like it, this is due to consistency. There was a lot of work involved in making all scripts consistent.
2. **Use `shellcheck` to check your scripts** ! Your contributions are checked by TravisCI too, so you will need to do this. You can **lint your work with `make lint`** to check against all targets.
3. **Use the provided `.editorconfig`** file.
4. Use `/bin/bash` or `/usr/bin/env bash` instead of `/bin/sh` . Adjust the style accordingly.
2020-09-05 14:19:12 +00:00
5. `setup.sh` provides a good starting point to look for.
2020-10-06 12:45:55 +00:00
6. When appropriate, use the `set` builtin. We recommend `set -euEo pipefail` or `set -uE` .
2020-09-05 14:19:12 +00:00
#### Styling rules
2020-09-09 15:19:48 +00:00
##### Initial Description
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
When writing a script, provide the version and the script's task. Please use [semantic versioning][semver].
2020-09-05 14:19:12 +00:00
``` BASH
#!/usr/bin/env bash
# version 0.1.0
#
# <TASK DESCRIPTION> -> cut this off
# to make it not longer than approx.
2020-09-09 15:19:48 +00:00
# 80 cols.
2020-09-05 14:19:12 +00:00
```
2020-09-09 15:19:48 +00:00
##### If-Else-Statements
2020-09-05 14:19:12 +00:00
``` BASH
2020-09-09 15:19:48 +00:00
# when using braces, use double braces
# remember you do not need "" when using [[ ]]
if [[ < CONDITION1 > ]] & & [[ -f ${FILE} ]]
2020-09-05 14:19:12 +00:00
then
< CODE TO RUN >
2020-09-23 08:21:37 +00:00
# when running commands, you don't need braces
2020-09-09 15:19:48 +00:00
elif < COMMAND TO RUN >
2020-09-05 14:19:12 +00:00
< CODE TO TUN >
else
2020-09-09 15:19:48 +00:00
< CODE TO TUN >
2020-09-05 14:19:12 +00:00
fi
2020-09-23 08:21:37 +00:00
# equality checks with numbers are done
# with -eq/-ne/-lt/-ge, not != or ==
2020-11-05 12:32:42 +00:00
if [[ ${VAR} -ne 42 ]] || [[ ${SOME_VAR} -eq 6 ]]
2020-09-05 14:19:12 +00:00
then
< CODE TO RUN >
fi
```
2020-09-09 15:19:48 +00:00
##### Variables & Braces
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
Variables are always uppercase. We always use braces.
If you forgot this and want to change it later, you can use [this link][regex]. The used regex is `\$([^{("\\'\/])([a-zA-Z0-9_]*)([^}\/ \t'"\n.\]:(=\\-]*)` , where you should in practice be able to replace all variable occurrences without braces with occurrences with braces.
2020-09-05 14:19:12 +00:00
``` BASH
# good
local VAR="good"
2020-09-09 15:19:48 +00:00
local NEW="${VAR}"
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
# bad -> TravisCI will fail
2020-09-05 14:19:12 +00:00
var="bad"
2020-10-06 12:45:55 +00:00
new=$var
2020-09-05 14:19:12 +00:00
```
2020-09-09 15:19:48 +00:00
##### Loops
2020-09-05 14:19:12 +00:00
Like `if-else` , loops look like this
``` BASH
for / while < LOOP CONDITION >
do
< CODE TO RUN >
done
```
2020-09-09 15:19:48 +00:00
##### Functions
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
It's always nice to see the use of functions as it also provides a clear structure. If scripts are small, this is unnecessary, but if they become larger, please consider using functions. When doing so, provide `function _main` .
2020-09-05 14:19:12 +00:00
``` BASH
2020-10-02 13:45:57 +00:00
function _< name_underscored_and_lowercase >
2020-09-05 14:19:12 +00:00
{
< CODE TO RUN >
# variables that can be local should be local
2020-09-23 08:21:37 +00:00
local < LOCAL_VARIABLE_NAME >
2020-09-05 14:19:12 +00:00
}
```
2020-09-09 15:19:48 +00:00
##### Error Tracing
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
A construct to trace error in your scripts looks like this. Remember: Remove `set -x` in the end. This is for debugging purposes only.
2020-09-05 14:19:12 +00:00
``` BASH
2020-10-06 12:45:55 +00:00
set -euEo pipefail
trap '__log_err ${FUNCNAME[0]:-"?"} ${_:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
function __log_err
2020-09-05 14:19:12 +00:00
{
2020-10-06 12:45:55 +00:00
local FUNC_NAME LINE EXIT_CODE
FUNC_NAME="${1} / ${2}"
LINE="${3}"
EXIT_CODE="${4}"
printf "\n– – – \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
" – script = ${SCRIPT,,}.sh" \
" – function = ${FUNC_NAME}" \
" – line = ${LINE}" \
" – exit code = ${EXIT_CODE}"
2020-09-05 14:19:12 +00:00
< CODE TO RUN AFTERWARDS >
}
```
2020-10-06 12:45:55 +00:00
##### Comments, Descriptiveness & An Example
2020-09-05 14:19:12 +00:00
2020-09-09 15:19:48 +00:00
Comments should only describe non-obvious matters. Comments should start lowercase when they aren't sentences. Make the code **self-descriptive** by using meaningful names! Make comments not longer than approximately 80 columns, then wrap the line.
2020-09-05 14:19:12 +00:00
2020-10-06 12:45:55 +00:00
A positive example, which is taken from `start-mailserver.sh` , would be
2020-09-05 14:19:12 +00:00
``` BASH
2020-10-06 12:45:55 +00:00
function _setup_postfix_aliases
2020-09-05 14:19:12 +00:00
{
2020-10-06 12:45:55 +00:00
_notify 'task' 'Setting up Postfix Aliases'
: >/etc/postfix/virtual
: >/etc/postfix/regexp
if [[ -f /tmp/docker-mailserver/postfix-virtual.cf ]]
then
# fixing old virtual user file
if grep -q ",$" /tmp/docker-mailserver/postfix-virtual.cf
then
sed -i -e "s/, /,/g" -e "s/,$//g" /tmp/docker-mailserver/postfix-virtual.cf
fi
cp -f /tmp/docker-mailserver/postfix-virtual.cf /etc/postfix/virtual
# the `to` is important, don't delete it
# shellcheck disable=SC2034
while read -r FROM TO
do
# Setting variables for better readability
UNAME=$(echo "${FROM}" | cut -d @ -f1)
DOMAIN=$(echo "${FROM}" | cut -d @ -f2)
# if they are equal it means the line looks like: "user1 other@domain.tld"
2020-10-21 16:16:32 +00:00
[[ "${UNAME}" != "${DOMAIN}" ]] & & echo "${DOMAIN}" >> /tmp/vhost.tmp
2020-10-06 12:45:55 +00:00
done < < (grep -v "^\s*$\|^\s*\#" /tmp/docker-mailserver/postfix-virtual.cf || true)
else
_notify 'inf' "Warning 'config/postfix-virtual.cf' is not provided. No mail alias/forward created."
fi
...
2020-09-05 14:19:12 +00:00
}
```
2020-09-09 15:19:48 +00:00
### YAML
2020-10-06 12:45:55 +00:00
When formatting YAML files, use [Prettier][prettier], an opinionated formatter. There are many plugins for IDEs around.
2020-09-09 15:19:48 +00:00
[//]: # (Links)
[commit]: https://help.github.com/articles/closing-issues-via-commit-messages/
[gpg]: https://docs.github.com/en/github/authenticating-to-github/generating-a-new-gpg-key
[semver]: https://semver.org/
2020-10-06 12:45:55 +00:00
[regex]: https://regex101.com/r/ikzJpF/7
2020-09-09 15:19:48 +00:00
[prettier]: https://prettier.io