2021-09-05 22:07:02 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-01-07 23:25:09 +00:00
|
|
|
# Wrapper for 'sed -i': fail if file was not modified by sed and container was not restarted.
|
2023-01-11 00:31:21 +00:00
|
|
|
# Error output is suppressed, when container is restarted to avoid harmless error messages.
|
2022-01-07 23:25:09 +00:00
|
|
|
# Use "--strict" as first parameter, to fail regardless of the container state (fresh or restarted).
|
|
|
|
|
|
|
|
# When to use sedfile?
|
|
|
|
# Is a file change optional? --> use regular 'sed -i'
|
|
|
|
# Is a file change expected? --> use 'sedfile --strict -i'
|
|
|
|
# Is a file change only on the first container run expected? --> use 'sedfile -i'
|
2021-09-05 22:07:02 +00:00
|
|
|
|
|
|
|
set -ueo pipefail
|
|
|
|
|
2022-05-05 10:58:00 +00:00
|
|
|
# shellcheck source=../scripts/helpers/log.sh
|
|
|
|
source /usr/local/bin/helpers/log.sh
|
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
function __usage { echo "Usage: ${0} -i <replace/delete operation> <file>" ; }
|
|
|
|
|
|
|
|
HASHTOOL='sha1sum'
|
2022-01-07 23:25:09 +00:00
|
|
|
SKIP_ERROR=0
|
2021-09-05 22:07:02 +00:00
|
|
|
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${#} -lt 3 ]]; then
|
2022-04-02 13:52:30 +00:00
|
|
|
_log 'error' 'At least three parameters must be given'
|
2022-03-26 08:30:09 +00:00
|
|
|
__usage
|
2021-09-05 22:07:02 +00:00
|
|
|
exit 1
|
|
|
|
fi >&2
|
|
|
|
|
2022-03-26 08:30:09 +00:00
|
|
|
[[ -f /CONTAINER_START ]] && SKIP_ERROR=1 # hide error if container was restarted
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${1} == '--strict' ]]; then # show error every time
|
2022-01-07 23:25:09 +00:00
|
|
|
SKIP_ERROR=0
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2021-09-05 22:07:02 +00:00
|
|
|
# get last argument
|
|
|
|
FILE=${*: -1}
|
|
|
|
|
|
|
|
OLD=$(${HASHTOOL} "${FILE}")
|
2022-03-26 08:30:09 +00:00
|
|
|
sed "${@}"
|
2021-09-05 22:07:02 +00:00
|
|
|
NEW=$(${HASHTOOL} "${FILE}")
|
|
|
|
|
|
|
|
# fail if file was not modified
|
2023-05-24 07:06:59 +00:00
|
|
|
if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]]; then
|
2022-04-02 13:52:30 +00:00
|
|
|
_log 'error' "No difference after call to 'sed' in 'sedfile' (sed ${*})" >&2
|
2021-09-05 22:07:02 +00:00
|
|
|
exit 1
|
2022-03-26 08:30:09 +00:00
|
|
|
fi
|
2022-01-07 23:25:09 +00:00
|
|
|
|
2021-09-05 22:07:02 +00:00
|
|
|
exit 0
|