Enhanced ini file handling (#128)

* Add ini file creation if not exists
Add section creation if not exists
Add default section if section not given
Fix read key value only from given (or default) section

* Fix to support platform specific sed

* Fixed typo in comment

* Fix test scenario to reflect changes to the (introduced) default section

* Fix expectations for scenario test

* Fix style guide compatibility
This commit is contained in:
R. Fuehrer 2019-12-31 15:16:08 +01:00 committed by Kevin van Zonneveld
parent 14dc823cca
commit 971a83ea3a
5 changed files with 32 additions and 14 deletions

View File

@ -14,7 +14,7 @@ Released: TBA.
[Diff](https://github.com/kvz/bash3boilerplate/compare/v2.4.1...master).
- [x] Capture correct error_code in err_report (#124, @eval)
- [ ]
- [x] Enhanced ini file handling: create new file, create new sections, handle default section, read key from given section (@rfuehrer)
## v2.4.1

View File

@ -168,6 +168,7 @@ We are looking for endorsements! Are you also using b3bp? [Let us know](https://
- [A. G. Madi](https://github.com/warpengineer)
- [Lukas Stockner](mailto:oss@genesiscloud.com)
- [Gert Goet](https://github.com/eval)
- [@rfuehrer](https://github.com/rfuehrer)
## License

View File

@ -33,16 +33,30 @@ function ini_val() {
local delim=" = "
local section=""
local key=""
local current=""
# add default section
local section_default="default"
if [[ ! -f "${file}" ]]; then
# touch file if not exists
touch ${file}
fi
# Split on . for section. However, section is optional
IFS='.' read -r section key <<< "${sectionkey}"
if [[ ! "${key}" ]]; then
key="${section}"
section=""
# default section if not given
section="${section_default}"
fi
local current
current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
current=$(sed -En "/^\[/{h;d;};G;s/^${key}(.*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1')
if ! grep -q "\[${section}\]" ${file}; then
# create section if not exists (empty line to seperate new section)
echo >> "${file}"
echo "[${section}]" >> "${file}"
fi
if [[ ! "${val}" ]]; then
# get a value
@ -51,19 +65,19 @@ function ini_val() {
# set a value
if [[ ! "${current}" ]]; then
# doesn't exist yet, add
if [[ ! "${section}" ]]; then
# no section was given, add to bottom of file
echo "${key}${delim}${val}" >> "${file}"
else
# add to section
sed -i.bak -e "/\\[${section}\\]/a ${key}${delim}${val}" "${file}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${file}.bak"
# if no section is given, propagate the default section
section=${section_default}
fi
# add to section
RET="/\\[${section}\\]/a\\
${key}${delim}${val}"
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
sed -i.bak -e "/^${key}${delim}/s/${delim}.*/${delim}${val}/" "${file}"
# 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

View File

@ -3,6 +3,7 @@ exists
127.0.0.1
nginx, nodejs
--> command: Replace three values in-place and show result
[default]
orphan = no more
[connection]
@ -15,6 +16,7 @@ exists
127.0.0.1
nginx, nodejs
--> function: Replace three values in-place and show result
[default]
orphan = no more
[connection]

View File

@ -1,3 +1,4 @@
[default]
orphan = exists
[connection]