]> www.infradead.org Git - users/sagi/blktests.git/commitdiff
common/rc: introduce _check_conflict_and_set_default()
authorShin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Sat, 4 May 2024 08:14:38 +0000 (17:14 +0900)
committerShin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Thu, 9 May 2024 07:37:06 +0000 (16:37 +0900)
Following commits are going to rename some config option parameters from
lowercase letters to uppercase. The old lowercase options will be
deprecated but still be kept usable to not cause confusions. When these
changes are made, it will be required to check that both new and old
parameters are not set at once and ensure they do not have two different
values.

To simplify the code to check the two parameters, introduce the helper
_check_conflict_and_set_default(). If the both two parameters are
set, it errors out. If the old option is set, it propagates the old
option value to the new option. Also, when neither of them is set, it
sets the default value to the new option.

Reviewed-by: Daniel Wagner <dwagner@suse.de>
Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
common/rc

index fa61cc69e62e5b8b4a9d525fe6830cdcbe67f169..6a9a26c21d24fbd2c845897edd59548f3581ff32 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -515,3 +515,30 @@ _set_combined_conditions()
        done
        COND_DESC="$_cond_desc"
 }
+
+# Check both old and new parameters are not configured. If the old parameter is
+# set, propagate to the new parameter. If neither is set, set the default value
+# to the new parameter.
+_check_conflict_and_set_default()
+{
+       local new_name="$1"
+       local old_name="$2"
+       local default_val="$3"
+       local new_name_checked="$new_name"_checked
+
+       if [[ -n ${!new_name_checked} ]]; then
+               return
+       fi
+
+       if [[ -n ${!old_name} ]]; then
+               if [[ -n ${!new_name} ]]; then
+                       echo "Both ${old_name} and ${new_name} are specified"
+                       exit 1
+               fi
+               eval "${new_name}=\"${!old_name}\""
+       elif [[ -z ${!new_name} ]]; then
+               eval "${new_name}=\"${default_val}\""
+       fi
+
+       eval "${new_name_checked}=true"
+}