Merge "Fix an issue in iniset function"

This commit is contained in:
Zuul 2019-01-17 04:35:27 +00:00 committed by Gerrit Code Review
commit 85e6aaf541
2 changed files with 14 additions and 7 deletions

View File

@ -88,17 +88,22 @@ function iniget_multiline {
} }
# Determinate is the given option present in the INI file # Determinate is the given option present in the INI file
# ini_has_option config-file section option # ini_has_option [-sudo] config-file section option
function ini_has_option { function ini_has_option {
local xtrace local xtrace
xtrace=$(set +o | grep xtrace) xtrace=$(set +o | grep xtrace)
set +o xtrace set +o xtrace
local sudo=""
if [ $1 == "-sudo" ]; then
sudo="sudo "
shift
fi
local file=$1 local file=$1
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=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
$xtrace $xtrace
[ -n "$line" ] [ -n "$line" ]
} }
@ -173,8 +178,10 @@ function iniset {
xtrace=$(set +o | grep xtrace) xtrace=$(set +o | grep xtrace)
set +o xtrace set +o xtrace
local sudo="" local sudo=""
local sudo_option=""
if [ $1 == "-sudo" ]; then if [ $1 == "-sudo" ]; then
sudo="sudo " sudo="sudo "
sudo_option="-sudo "
shift shift
fi fi
local file=$1 local file=$1
@ -187,11 +194,11 @@ function iniset {
return return
fi fi
if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then
# Add section at the end # Add section at the end
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
fi fi
if ! ini_has_option "$file" "$section" "$option"; then if ! ini_has_option $sudo_option "$file" "$section" "$option"; then
# Add it # Add it
$sudo sed -i -e "/^\[$section\]/ a\\ $sudo sed -i -e "/^\[$section\]/ a\\
$option = $value $option = $value
@ -228,7 +235,7 @@ function iniset_multiline {
# the reverse order. Do a reverse here to keep the original order. # the reverse order. Do a reverse here to keep the original order.
values="$v ${values}" values="$v ${values}"
done done
if ! grep -q "^\[$section\]" "$file"; then if ! $sudo grep -q "^\[$section\]" "$file"; then
# Add section at the end # Add section at the end
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null
else else

View File

@ -125,14 +125,14 @@ VAL=$(iniget ${TEST_INI} bbb handlers)
assert_equal "$VAL" "33,44" "inset at EOF" assert_equal "$VAL" "33,44" "inset at EOF"
# test empty option # test empty option
if ini_has_option ${TEST_INI} ddd empty; then if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then
passed "ini_has_option: ddd.empty present" passed "ini_has_option: ddd.empty present"
else else
failed "ini_has_option failed: ddd.empty not found" failed "ini_has_option failed: ddd.empty not found"
fi fi
# test non-empty option # test non-empty option
if ini_has_option ${TEST_INI} bbb handlers; then if ini_has_option ${SUDO_ARG} ${TEST_INI} bbb handlers; then
passed "ini_has_option: bbb.handlers present" passed "ini_has_option: bbb.handlers present"
else else
failed "ini_has_option failed: bbb.handlers not found" failed "ini_has_option failed: bbb.handlers not found"