stdenv: refactor of --replace-{quiet,warn,fail} logic

This is a small simplification of the control flow surrounding these cases. It should make it more obvious when each case happens, and also explicitly defines the current behaviour of --replace.
This commit is contained in:
Kait Lam 2024-02-11 10:48:37 +10:00 committed by rina
parent 687cda7b77
commit 355ab764b4

View file

@ -816,7 +816,7 @@ fi
# Textual substitution functions.
# only log once, due to max logging limit on hydra
_substituteStream_has_warned_replace_deprecation=""
_substituteStream_has_warned_replace_deprecation=false
substituteStream() {
local var=$1
@ -824,24 +824,18 @@ substituteStream() {
shift 2
while (( "$#" )); do
local is_required=1
local is_quiet=""
local replace_mode="$1"
case "$1" in
--replace-quiet)
is_quiet=1
;&
--replace)
# deprecated 2023-11-22
# this will either get removed, or switch to the behaviour of --replace-fail in the future
if [ -z "$is_quiet" ] && [ -z "$_substituteStream_has_warned_replace_deprecation" ]; then
if ! "$_substituteStream_has_warned_replace_deprecation"; then
echo "substituteStream(): WARNING: '--replace' is deprecated, use --replace-{fail,warn,quiet}. ($description)" >&2
_substituteStream_has_warned_replace_deprecation=1
_substituteStream_has_warned_replace_deprecation=true
fi
replace_mode='--replace-warn'
;&
--replace-warn)
is_required=""
;&
--replace-fail)
--replace-quiet|--replace-warn|--replace-fail)
pattern="$2"
replacement="$3"
shift 3
@ -850,11 +844,9 @@ substituteStream() {
eval "$var"'=${'"$var"'//"$pattern"/"$replacement"}'
if [ "$pattern" != "$replacement" ]; then
if [ "${!var}" == "$savedvar" ]; then
if [ -z "$is_required" ]; then
if [ -z "$is_quiet" ]; then
printf "substituteStream(): WARNING: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2
fi
else
if [ "$replace_mode" == --replace-warn ]; then
printf "substituteStream(): WARNING: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2
elif [ "$replace_mode" == --replace-fail ]; then
printf "substituteStream(): ERROR: pattern %q doesn't match anything in %s\n" "$pattern" "$description" >&2
return 1
fi