From 1f65fd64ce2f4ed32a706f9bcb28c2ee0cf51e5b Mon Sep 17 00:00:00 2001 From: Doug Wiegley Date: Sat, 13 Dec 2014 11:56:16 -0700 Subject: [PATCH] Clear multi-line sections before adding lines With multiline support for local.conf, the first line is created with iniset, which will set *all* previous lines to the same thing, and then subsequent lines will be added. Modify the multiline support to first clear existing lines from the section. This causes fatal errors with neutron.conf, which defines drivers with a bunch of service_provider= options, and the current code ends up with the first driver defined in local.conf being present twice. Change-Id: If132a94e53545d9134859aa508da7b9819ede2f8 --- functions-common | 15 +++++++++++++ lib/config | 1 + tests/test_ini.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/functions-common b/functions-common index 94ab34763e..352102e24a 100644 --- a/functions-common +++ b/functions-common @@ -148,6 +148,21 @@ $option = $value $xtrace } +function inidelete { + local xtrace=$(set +o | grep xtrace) + set +o xtrace + local file=$1 + local section=$2 + local option=$3 + + [[ -z $section || -z $option ]] && return + + # Remove old values + sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file" + + $xtrace +} + # Set an option in an INI file # iniset config-file section option value function iniset { diff --git a/lib/config b/lib/config index c0756bf3fe..31c6fa6e57 100644 --- a/lib/config +++ b/lib/config @@ -144,6 +144,7 @@ function merge_config_file { else { # For multiline, invoke the ini routines in the reverse order count = cfg_attr_count[section, attr] + print "inidelete " configfile " " section " " attr print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, count - 1] "\"" for (l = count -2; l >= 0; l--) print "iniadd_literal " configfile " " section " " attr " \"" cfg_attr[section, attr, l] "\"" diff --git a/tests/test_ini.sh b/tests/test_ini.sh index 598cd578f6..106cc9507f 100755 --- a/tests/test_ini.sh +++ b/tests/test_ini.sh @@ -34,6 +34,32 @@ empty = [eee] multi = foo1 multi = foo2 + +# inidelete(a) +[del_separate_options] +a=b +b=c + +# inidelete(a) +[del_same_option] +a=b +a=c + +# inidelete(a) +[del_missing_option] +b=c + +# inidelete(a) +[del_missing_option_multi] +b=c +b=d + +# inidelete(a) +[del_no_options] + +# inidelete(a) +# no section - del_no_section + EOF # Test with missing arguments @@ -237,4 +263,33 @@ else echo "iniadd with non-exsting failed: $VAL" fi +# Test inidelete +del_cases=" + del_separate_options + del_same_option + del_missing_option + del_missing_option_multi + del_no_options + del_no_section" + +for x in $del_cases; do + inidelete test.ini $x a + VAL=$(iniget_multiline test.ini $x a) + if [ -z "$VAL" ]; then + echo "OK: inidelete $x" + else + echo "inidelete $x failed: $VAL" + fi + if [ "$x" = "del_separate_options" -o \ + "$x" = "del_missing_option" -o \ + "$x" = "del_missing_option_multi" ]; then + VAL=$(iniget_multiline test.ini $x b) + if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then + echo "OK: inidelete other_options $x" + else + echo "inidelete other_option $x failed: $VAL" + fi + fi +done + rm test.ini