diff --git a/type/__mail_alias/files/update_aliases.awk b/type/__mail_alias/files/update_aliases.awk
new file mode 100644
index 0000000..87ea202
--- /dev/null
+++ b/type/__mail_alias/files/update_aliases.awk
@@ -0,0 +1,98 @@
+#!/usr/bin/awk -f
+#
+# 2020 Dennis Camera (dennis.camera@ssrq-sds-fds.ch)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with cdist. If not, see .
+#
+
+function getvalue(path) {
+ # Reads the first line of the file located at path and returns it.
+ getline < path
+ close(path)
+ return $0
+}
+
+function sepafter(f, def, _) {
+ # finds the separator between field $f and $(f+1)
+ _ = substr($0, length($f)+1, index(substr($0, length($f)+1), $(f+1))-1)
+ return _ ? _ : def
+}
+
+function write_aliases() {
+ if (aliases_written) return
+
+ # print aliases line
+ printf "%s%s", ENVIRON["__object_id"], sepafter(1, ": ")
+ while ((getline < aliases_should_file) > 0) {
+ if (aliases_written) printf ", "
+ printf "%s", $0
+ aliases_written = 1
+ }
+ printf "\n"
+ close(aliases_should_file)
+}
+
+BEGIN {
+ FS = ":[ \t]*"
+
+ parameter_dir = ENVIRON["__object"] "/parameter/"
+
+ mode = (getvalue(parameter_dir "state") != "absent")
+ aliases_should_file = (parameter_dir "/alias")
+}
+
+/^[ \t]*\#/ {
+ # comment line (leave alone)
+ select = 0; cont = 0 # comments terminate alias lists and continuations
+ print
+ next
+}
+
+{
+ # is this line a continuation line?
+ # (the prev. line ended in a backslash or the line starts with whitespace)
+ is_cont = /^[ \t]/ || cont
+
+ # detect if the line is a line to be continued (ends with a backslash)
+ cont = ($0 ~ /\\$/)
+ # if it is, we drop the backslash from the line.
+ if (cont) sub(/[ \t]*\\$/, "", $0)
+}
+
+is_cont {
+ # we ignore the line as it has been rewritten previously or is not
+ # interesting
+ next
+}
+
+$1 == ENVIRON["__object_id"] {
+ # "target" user -> rewrite aliases list
+ select = 1
+ if (mode) write_aliases()
+ next
+}
+
+{
+ # other user
+ select = 0
+ print
+}
+
+END {
+ # if the last line was an alias, the separator will be reused (looks better)
+ if (mode && !aliases_written)
+ write_aliases()
+}
diff --git a/type/__mail_alias/gencode-remote b/type/__mail_alias/gencode-remote
index 9f4af1b..e5bc2b7 100755
--- a/type/__mail_alias/gencode-remote
+++ b/type/__mail_alias/gencode-remote
@@ -18,6 +18,16 @@
# along with cdist. If not, see .
#
+drop_awk_comments() { sed '/^[[:blank:]]*#.*$/d;/^$/d' "$@"; }
+
+aliases_file=$(cat "${__object}/explorer/aliases_file")
+
+test -n "${aliases_file}" || {
+ echo 'Could not determine aliases file path.' >&2
+ exit 1
+}
+
+
state_should=$(cat "${__object}/parameter/state")
case $state_should
@@ -35,119 +45,36 @@ in
else
echo "add aliases" >>"$__messages_out"
fi
-
- mode=1
;;
(absent)
# nothing to do if no aliases found.
test -s "${__object}/explorer/aliases" || exit 0
echo "delete aliases" >>"$__messages_out"
-
- mode=0
;;
(*)
- printf 'Invalid --state: %s.\n' "$state_should" >&2
+ printf 'Invalid --state: %s.\n' "${state_should}" >&2
printf 'Acceptable values are: present, absent.\n' >&2
exit 1
esac
-aliases_file=$(cat "${__object}/explorer/aliases_file")
+cat <&2
- exit 1
-}
-
-# "export" variables to remote
-printf 'mode=%u\n' "${mode}"
-printf "aliases_file='%s'\n" "${aliases_file}"
-
-cat <<'EOF'
-test -f "${aliases_file}" || touch "${aliases_file}"
-
-awk -F ':[ \t]*' -v mode=$mode '
-function sepafter(f, def, _) {
- # finds the separator between field $f and $(f+1)
- _ = substr($0, length($f)+1, index(substr($0, length($f)+1), $(f+1))-1)
- return _ ? _ : def
-}
-
-function write_aliases() {
- if (aliases_written) return
-
- # print aliases line
- printf "%s%s", ENVIRON["__object_id"], sepafter(1, ": ")
- while ((getline < aliases_should_file) > 0) {
- if (aliases_written) printf ", "
- printf "%s", $0
- aliases_written = 1
- }
- printf "\n"
- close(aliases_should_file)
-}
-
-BEGIN {
- aliases_should_file = (ENVIRON["__object"] "/parameter/alias")
-}
-
-/^[ \t]*\#/ {
- # comment line (leave alone)
- select = 0; cont = 0 # comments terminate alias lists and continuations
- print
- next
-}
-
-{
- # is this line a continuation line?
- # (the prev. line ended in a backslash or the line starts with whitespace)
- is_cont = /^[ \t]/ || cont
-
- # detect if the line is a line to be continued (ends with a backslash)
- cont = ($0 ~ /\\$/)
- # if it is, we drop the backslash from the line.
- if (cont) sub(/[ \t]*\\$/, "", $0)
-}
-
-is_cont {
- # we ignore the line as it has been rewritten previously or is not
- # interesting
- next
-}
-
-$1 == ENVIRON["__object_id"] {
- # "target" user -> rewrite aliases list
- select = 1
- if (mode) write_aliases()
- next
-}
-
-{
- # other user
- select = 0
- print
-}
-
-END {
- # if the last line was an alias, the separator will be reused (looks better)
- if (mode && !aliases_written)
- write_aliases()
-}
-' <"${aliases_file}" >"${aliases_file}.tmp" || {
- rm -f "${aliases_file}.tmp"
+awk '$(drop_awk_comments "${__type}/files/update_aliases.awk")' <'${aliases_file}' >'${aliases_file}.tmp' \
+|| {
+ rm -f '${aliases_file}.tmp'
echo 'Generating new aliases file failed!' >&2
exit 1
}
-if ! cmp -s "${aliases_file}" "${aliases_file}.tmp"
+if ! cmp -s '${aliases_file}' '${aliases_file}.tmp'
then
- # aliases file was modified, replace and run `newaliases`.
- mv "${aliases_file}.tmp" "${aliases_file}"
+ # aliases file was modified, replace:
+ cat '${aliases_file}.tmp' >'${aliases_file}'
- # run newaliases if present
+ # then, run newaliases if present ("missing" on Alpine Linux because of typo)
command -v newaliases >/dev/null 2>&1 && newaliases || true
-else
- # no modifications were made, delete the temp file.
- rm "${aliases_file}.tmp"
fi
+rm -f '${aliases_file}.tmp'
EOF