mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2024-12-18 14:26:22 +00:00
Add feature edit/update comments (#132)
Remove obsolete lines of code Update reference file Update test scenario
This commit is contained in:
parent
56eab89caa
commit
1005ceeec8
@ -53,10 +53,13 @@ function ini_val() {
|
||||
section="${section_default}"
|
||||
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')
|
||||
# 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
|
||||
# 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 "[${section}]" >> "${file}"
|
||||
fi
|
||||
@ -66,32 +69,39 @@ function ini_val() {
|
||||
echo "${current}"
|
||||
else
|
||||
# set a value
|
||||
if [[ ! "${current}" ]]; then
|
||||
# doesn't exist yet, add
|
||||
if [[ ! "${section}" ]]; then
|
||||
# if no section is given, propagate the default section
|
||||
section=${section_default}
|
||||
fi
|
||||
# add to section
|
||||
if [[ ! "${comment}" ]]; then
|
||||
# add new key/value without comment
|
||||
RET="/\\[${section}\\]/a\\
|
||||
if [[ ! "${section}" ]]; then
|
||||
# if no section is given, propagate the default section
|
||||
section=${section_default}
|
||||
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
|
||||
if [[ ! "${comment}" ]]; then
|
||||
# add new key/value _without_ comment
|
||||
RET="/\\[${section}\\]/a\\
|
||||
${key}${delim}${val}"
|
||||
else
|
||||
# add new key/value with preceeding comment
|
||||
RET="/\\[${section}\\]/a\\
|
||||
else
|
||||
# add new key/value _with_ preceeding comment
|
||||
RET="/\\[${section}\\]/a\\
|
||||
${comment_delim}[${key}] ${comment}\\
|
||||
${key}${delim}${val}"
|
||||
fi
|
||||
sed -i.bak -e "${RET}" "${file}"
|
||||
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
|
||||
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
|
||||
sed -i.bak -e "${RET}" "${file}"
|
||||
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
|
||||
rm -f "${file}.bak"
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -4,33 +4,39 @@ exists
|
||||
nginx, nodejs
|
||||
|
||||
--> command: Replace three values in-place and show result
|
||||
|
||||
[default]
|
||||
orphan = no more
|
||||
orphan=no more
|
||||
|
||||
[connection]
|
||||
host = 192.168.0.1
|
||||
host=192.168.0.1
|
||||
|
||||
[software]
|
||||
packages = vim
|
||||
packages=vim
|
||||
|
||||
[comment]
|
||||
;[command] this key is commented
|
||||
command=commented
|
||||
;[command] got this new comment
|
||||
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
|
||||
exists
|
||||
127.0.0.1
|
||||
nginx, nodejs
|
||||
|
||||
--> function: Replace three values in-place and show result
|
||||
|
||||
[default]
|
||||
orphan = no more
|
||||
orphan=no more
|
||||
|
||||
[connection]
|
||||
host = 192.168.0.1
|
||||
host=192.168.0.1
|
||||
|
||||
[software]
|
||||
packages = vim
|
||||
packages=vim
|
||||
|
||||
[comment]
|
||||
;[command] this key is commented
|
||||
command=commented
|
||||
;[command] got this new comment
|
||||
command=works like a chame
|
||||
;[new_command] last addition will be moved downwards again after next command
|
||||
new_command=commented too
|
||||
|
@ -1,8 +1,8 @@
|
||||
[default]
|
||||
orphan = exists
|
||||
orphan=exists
|
||||
|
||||
[connection]
|
||||
host = 127.0.0.1
|
||||
host=127.0.0.1
|
||||
|
||||
[software]
|
||||
packages = nginx, nodejs
|
||||
packages=nginx, nodejs
|
||||
|
@ -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 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 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
|
||||
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 connection.host "192.168.0.1"
|
||||
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
|
||||
rm -f dummy.ini
|
||||
|
Loading…
Reference in New Issue
Block a user