Merge "Fix iniset and his friends"

This commit is contained in:
Jenkins 2013-01-11 01:18:27 +00:00 committed by Gerrit Code Review
commit 9f67bd692c
2 changed files with 61 additions and 10 deletions

View File

@ -459,7 +459,7 @@ function inicomment() {
local file=$1 local file=$1
local section=$2 local section=$2
local option=$3 local option=$3
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
} }
# Uncomment an option in an INI file # Uncomment an option in an INI file
@ -468,7 +468,7 @@ function iniuncomment() {
local file=$1 local file=$1
local section=$2 local section=$2
local option=$3 local option=$3
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
} }
@ -479,10 +479,20 @@ function iniget() {
local section=$2 local section=$2
local option=$3 local option=$3
local line local line
line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file) line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
echo ${line#*=} echo ${line#*=}
} }
# Determinate is the given option present in the INI file
# ini_has_option config-file section option
function ini_has_option() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
[ -n "$line" ]
}
# Set an option in an INI file # Set an option in an INI file
# iniset config-file section option value # iniset config-file section option value
@ -491,18 +501,18 @@ function iniset() {
local section=$2 local section=$2
local option=$3 local option=$3
local value=$4 local value=$4
if ! grep -q "^\[ *$section *\]" $file; then if ! grep -q "^\[$section\]" "$file"; then
# Add section at the end # Add section at the end
echo -e "\n[$section]" >>$file echo -e "\n[$section]" >>"$file"
fi fi
if [[ -z "$(iniget $file $section $option)" ]]; then if ! ini_has_option "$file" "$section" "$option"; then
# Add it # Add it
sed -i -e "/^\[ *$section *\]/ a\\ sed -i -e "/^\[$section\]/ a\\
$option = $value $option = $value
" $file " "$file"
else else
# Replace it # Replace it
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
fi fi
} }

View File

@ -57,6 +57,9 @@ handlers=ee,ff
[ ccc ] [ ccc ]
spaces = yes spaces = yes
[ddd]
empty =
EOF EOF
# Test with spaces # Test with spaces
@ -79,13 +82,22 @@ fi
# Test with spaces in section header # Test with spaces in section header
VAL=$(iniget test.ini ccc spaces) VAL=$(iniget test.ini " ccc " spaces)
if [[ "$VAL" == "yes" ]]; then if [[ "$VAL" == "yes" ]]; then
echo "OK: $VAL" echo "OK: $VAL"
else else
echo "iniget failed: $VAL" echo "iniget failed: $VAL"
fi fi
iniset test.ini "b b" opt_ion 42
VAL=$(iniget test.ini "b b" opt_ion)
if [[ "$VAL" == "42" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test without spaces, end of file # Test without spaces, end of file
VAL=$(iniget test.ini bbb handlers) VAL=$(iniget test.ini bbb handlers)
@ -104,6 +116,29 @@ else
echo "iniget failed: $VAL" echo "iniget failed: $VAL"
fi fi
# test empty option
if ini_has_option test.ini ddd empty; then
echo "OK: ddd.empty present"
else
echo "ini_has_option failed: ddd.empty not found"
fi
# test non-empty option
if ini_has_option test.ini bbb handlers; then
echo "OK: bbb.handlers present"
else
echo "ini_has_option failed: bbb.handlers not found"
fi
# test changing empty option
iniset test.ini ddd empty "42"
VAL=$(iniget test.ini ddd empty)
if [[ "$VAL" == "42" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test section not exist # Test section not exist
@ -132,6 +167,12 @@ else
echo "iniget failed: $VAL" echo "iniget failed: $VAL"
fi fi
if ! ini_has_option test.ini aaa debug; then
echo "OK aaa.debug not present"
else
echo "ini_has_option failed: aaa.debug"
fi
iniset test.ini aaa debug "999" iniset test.ini aaa debug "999"
VAL=$(iniget test.ini aaa debug) VAL=$(iniget test.ini aaa debug)