#!/bin/bash # Wrapper for 'sed -i': fail if file was not modified by sed and container was not restarted. # Error output is surpressed, when container is restarted to avoid harmless error messages. # 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' if [[ -e /usr/local/bin/helpers/log.sh ]] then # shellcheck source=../scripts/helpers/log.sh source /usr/local/bin/helpers/log.sh else # when running BATS (unit tests), this file is not located # inside a container; as a consequence, we need to source # from a different location source target/scripts/helpers/log.sh fi set -ueo pipefail function __usage { echo "Usage: ${0} -i " ; } HASHTOOL='sha1sum' SKIP_ERROR=0 if [[ ${#} -lt 3 ]] then _log 'error' 'At least three parameters must be given' __usage exit 1 fi >&2 [[ -f /CONTAINER_START ]] && SKIP_ERROR=1 # hide error if container was restarted if [[ ${1} == '--strict' ]] # show error every time then SKIP_ERROR=0 shift fi # get last argument FILE=${*: -1} OLD=$(${HASHTOOL} "${FILE}") sed "${@}" NEW=$(${HASHTOOL} "${FILE}") # fail if file was not modified if [[ ${OLD} == "${NEW}" ]] && [[ ${SKIP_ERROR} -eq 0 ]] then _log 'error' "No difference after call to 'sed' in 'sedfile' (sed ${*})" >&2 exit 1 fi exit 0