Add feature edit/update comments (#132)

Remove obsolete lines of code
Update reference file
Update test scenario
This commit is contained in:
R. Fuehrer 2020-04-22 09:59:24 +02:00 committed by GitHub
parent 56eab89caa
commit 1005ceeec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 37 deletions

View File

@ -53,10 +53,13 @@ function ini_val() {
section="${section_default}" section="${section_default}"
fi fi
# get current value (if exists)
current=$(sed -En "/^\[/{h;d;};G;s/^${key}([[:blank:]]*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1') current=$(sed -En "/^\[/{h;d;};G;s/^${key}([[:blank:]]*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1')
# get current comment (if exists)
current_comment=$(sed -En "/^\[${section}\]/,/^\[.*\]/ s|^(${comment_delim}\[${key}\])(.*)|\2|p" "${file}"|awk '{$1=$1};1')
if ! grep -q "\[${section}\]" "${file}"; then if ! grep -q "\[${section}\]" "${file}"; then
# create section if not exists (empty line to seperate new section) # create section if not exists (empty line to seperate new section for better readability)
echo >> "${file}" echo >> "${file}"
echo "[${section}]" >> "${file}" echo "[${section}]" >> "${file}"
fi fi
@ -66,19 +69,32 @@ function ini_val() {
echo "${current}" echo "${current}"
else else
# set a value # set a value
if [[ ! "${current}" ]]; then
# doesn't exist yet, add
if [[ ! "${section}" ]]; then if [[ ! "${section}" ]]; then
# if no section is given, propagate the default section # if no section is given, propagate the default section
section=${section_default} section=${section_default}
fi fi
if [[ ! "${comment}" ]]; then
# if no comment given, keep old comment
comment="${current_comment}"
fi
# maintenance area
# a) remove comment if new given / respect section
sed -i.bak "/^\[${section}\]/,/^\[.*\]/ s|^\(${comment_delim}\[${key}\] \).*$||" "${file}"
# b) remove old key / respect section
sed -i.bak "/^\[${section}\]/,/^\[.*\]/ s|^\(${key}=\).*$||" "${file}"
# c) remove all empty lines in ini file
sed -i.bak '/^[[:space:]]*$/d' "${file}"
# d) insert line break before every section for better readability
sed -i.bak $'s/^\\[/\\\n\\[/g' "${file}"
# add to section # add to section
if [[ ! "${comment}" ]]; then if [[ ! "${comment}" ]]; then
# add new key/value without comment # add new key/value _without_ comment
RET="/\\[${section}\\]/a\\ RET="/\\[${section}\\]/a\\
${key}${delim}${val}" ${key}${delim}${val}"
else else
# add new key/value with preceeding comment # add new key/value _with_ preceeding comment
RET="/\\[${section}\\]/a\\ RET="/\\[${section}\\]/a\\
${comment_delim}[${key}] ${comment}\\ ${comment_delim}[${key}] ${comment}\\
${key}${delim}${val}" ${key}${delim}${val}"
@ -86,12 +102,6 @@ ${key}${delim}${val}"
sed -i.bak -e "${RET}" "${file}" sed -i.bak -e "${RET}" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666 # this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak" rm -f "${file}.bak"
else
# replace existing (modified to replace only keys in given section)
sed -i.bak -e "/^\[${section}\]/,/^\[.*\]/ s|^\(${key}[ \t]*${delim}[ \t]*\).*$|\1${val}|" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak"
fi
fi fi
} }

View File

@ -4,33 +4,39 @@ exists
nginx, nodejs nginx, nodejs
--> command: Replace three values in-place and show result --> command: Replace three values in-place and show result
[default] [default]
orphan = no more orphan=no more
[connection] [connection]
host = 192.168.0.1 host=192.168.0.1
[software] [software]
packages = vim packages=vim
[comment] [comment]
;[command] this key is commented ;[command] got this new comment
command=commented command=works like a chame
;[new_command] last addition will be moved downwards again after next command
new_command=commented too
--> function: Read 3 values --> function: Read 3 values
exists exists
127.0.0.1 127.0.0.1
nginx, nodejs nginx, nodejs
--> function: Replace three values in-place and show result --> function: Replace three values in-place and show result
[default] [default]
orphan = no more orphan=no more
[connection] [connection]
host = 192.168.0.1 host=192.168.0.1
[software] [software]
packages = vim packages=vim
[comment] [comment]
;[command] this key is commented ;[command] got this new comment
command=commented command=works like a chame
;[new_command] last addition will be moved downwards again after next command
new_command=commented too

View File

@ -1,8 +1,8 @@
[default] [default]
orphan = exists orphan=exists
[connection] [connection]
host = 127.0.0.1 host=127.0.0.1
[software] [software]
packages = nginx, nodejs packages=nginx, nodejs

View File

@ -23,7 +23,10 @@ echo "--> command: Replace three values in-place and show result"
bash "${__root}/src/ini_val.sh" ./dummy.ini orphan "no more" bash "${__root}/src/ini_val.sh" ./dummy.ini orphan "no more"
bash "${__root}/src/ini_val.sh" ./dummy.ini connection.host "192.168.0.1" bash "${__root}/src/ini_val.sh" ./dummy.ini connection.host "192.168.0.1"
bash "${__root}/src/ini_val.sh" ./dummy.ini software.packages "vim" bash "${__root}/src/ini_val.sh" ./dummy.ini software.packages "vim"
bash "${__root}/src/ini_val.sh" ./dummy.ini comment.command "commented" "this key is commented" bash "${__root}/src/ini_val.sh" ./dummy.ini comment.command "commented" "this key is commented"
bash "${__root}/src/ini_val.sh" ./dummy.ini comment.new_command "commented too" "last addition will be moved downwards again after next command"
bash "${__root}/src/ini_val.sh" ./dummy.ini comment.command "works like a chame" "got this new comment"
cat dummy.ini cat dummy.ini
rm -f dummy.ini rm -f dummy.ini
@ -43,6 +46,9 @@ echo "--> function: Replace three values in-place and show result"
ini_val ./dummy.ini orphan "no more" ini_val ./dummy.ini orphan "no more"
ini_val ./dummy.ini connection.host "192.168.0.1" ini_val ./dummy.ini connection.host "192.168.0.1"
ini_val ./dummy.ini software.packages "vim" ini_val ./dummy.ini software.packages "vim"
ini_val ./dummy.ini comment.command "commented" "this key is commented"
ini_val ./dummy.ini comment.command "commented" "this key is commited"
ini_val ./dummy.ini comment.new_command "commented too" "last addition will be moved downwards again after next command"
ini_val ./dummy.ini comment.command "works like a chame" "got this new comment"
cat dummy.ini cat dummy.ini
rm -f dummy.ini rm -f dummy.ini