Merge "Clear multi-line sections before adding lines"

This commit is contained in:
Jenkins 2015-01-07 22:32:15 +00:00 committed by Gerrit Code Review
commit c7acc12ad2
3 changed files with 71 additions and 0 deletions

View File

@ -147,6 +147,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 {

View File

@ -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] "\""

View File

@ -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