Browse code

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

Doug Wiegley authored on 2014/12/14 03:56:16
Showing 3 changed files
... ...
@@ -148,6 +148,21 @@ $option = $value
148 148
     $xtrace
149 149
 }
150 150
 
151
+function inidelete {
152
+    local xtrace=$(set +o | grep xtrace)
153
+    set +o xtrace
154
+    local file=$1
155
+    local section=$2
156
+    local option=$3
157
+
158
+    [[ -z $section || -z $option ]] && return
159
+
160
+    # Remove old values
161
+    sed -i -e "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ d; }" "$file"
162
+
163
+    $xtrace
164
+}
165
+
151 166
 # Set an option in an INI file
152 167
 # iniset config-file section option value
153 168
 function iniset {
... ...
@@ -144,6 +144,7 @@ function merge_config_file {
144 144
                     else {
145 145
                         # For multiline, invoke the ini routines in the reverse order
146 146
                         count = cfg_attr_count[section, attr]
147
+                        print "inidelete " configfile " " section " " attr
147 148
                         print "iniset " configfile " " section " " attr " \"" cfg_attr[section, attr, count - 1] "\""
148 149
                         for (l = count -2; l >= 0; l--)
149 150
                             print "iniadd_literal " configfile " " section " " attr " \"" cfg_attr[section, attr, l] "\""
... ...
@@ -34,6 +34,32 @@ empty =
34 34
 [eee]
35 35
 multi = foo1
36 36
 multi = foo2
37
+
38
+# inidelete(a)
39
+[del_separate_options]
40
+a=b
41
+b=c
42
+
43
+# inidelete(a)
44
+[del_same_option]
45
+a=b
46
+a=c
47
+
48
+# inidelete(a)
49
+[del_missing_option]
50
+b=c
51
+
52
+# inidelete(a)
53
+[del_missing_option_multi]
54
+b=c
55
+b=d
56
+
57
+# inidelete(a)
58
+[del_no_options]
59
+
60
+# inidelete(a)
61
+# no section - del_no_section
62
+
37 63
 EOF
38 64
 
39 65
 # Test with missing arguments
... ...
@@ -237,4 +263,33 @@ else
237 237
     echo "iniadd with non-exsting failed: $VAL"
238 238
 fi
239 239
 
240
+# Test inidelete
241
+del_cases="
242
+    del_separate_options
243
+    del_same_option
244
+    del_missing_option
245
+    del_missing_option_multi
246
+    del_no_options
247
+    del_no_section"
248
+
249
+for x in $del_cases; do
250
+    inidelete test.ini $x a
251
+    VAL=$(iniget_multiline test.ini $x a)
252
+    if [ -z "$VAL" ]; then
253
+        echo "OK: inidelete $x"
254
+    else
255
+        echo "inidelete $x failed: $VAL"
256
+    fi
257
+    if [ "$x" = "del_separate_options" -o \
258
+        "$x" = "del_missing_option" -o \
259
+        "$x" = "del_missing_option_multi" ]; then
260
+        VAL=$(iniget_multiline test.ini $x b)
261
+        if [ "$VAL" = "c" -o "$VAL" = "c d" ]; then
262
+            echo "OK: inidelete other_options $x"
263
+        else
264
+            echo "inidelete other_option $x failed: $VAL"
265
+        fi
266
+    fi
267
+done
268
+
240 269
 rm test.ini