mirror of
https://github.com/kvz/bash3boilerplate.git
synced 2025-04-25 13:29:40 +00:00
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:
parent
14dc823cca
commit
971a83ea3a
@ -14,7 +14,7 @@ Released: TBA.
|
|||||||
[Diff](https://github.com/kvz/bash3boilerplate/compare/v2.4.1...master).
|
[Diff](https://github.com/kvz/bash3boilerplate/compare/v2.4.1...master).
|
||||||
|
|
||||||
- [x] Capture correct error_code in err_report (#124, @eval)
|
- [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
|
## v2.4.1
|
||||||
|
|
||||||
|
@ -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)
|
- [A. G. Madi](https://github.com/warpengineer)
|
||||||
- [Lukas Stockner](mailto:oss@genesiscloud.com)
|
- [Lukas Stockner](mailto:oss@genesiscloud.com)
|
||||||
- [Gert Goet](https://github.com/eval)
|
- [Gert Goet](https://github.com/eval)
|
||||||
|
- [@rfuehrer](https://github.com/rfuehrer)
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
@ -33,16 +33,30 @@ function ini_val() {
|
|||||||
local delim=" = "
|
local delim=" = "
|
||||||
local section=""
|
local section=""
|
||||||
local key=""
|
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
|
# Split on . for section. However, section is optional
|
||||||
IFS='.' read -r section key <<< "${sectionkey}"
|
IFS='.' read -r section key <<< "${sectionkey}"
|
||||||
if [[ ! "${key}" ]]; then
|
if [[ ! "${key}" ]]; then
|
||||||
key="${section}"
|
key="${section}"
|
||||||
section=""
|
# default section if not given
|
||||||
|
section="${section_default}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local current
|
current=$(sed -En "/^\[/{h;d;};G;s/^${key}(.*)${delim}(.*)\n\[${section}\]$/\2/p" "${file}"|awk '{$1=$1};1')
|
||||||
current=$(awk -F "${delim}" "/^${key}${delim}/ {for (i=2; i<NF; i++) printf \$i \" \"; print \$NF}" "${file}")
|
|
||||||
|
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
|
if [[ ! "${val}" ]]; then
|
||||||
# get a value
|
# get a value
|
||||||
@ -51,19 +65,19 @@ function ini_val() {
|
|||||||
# set a value
|
# set a value
|
||||||
if [[ ! "${current}" ]]; then
|
if [[ ! "${current}" ]]; then
|
||||||
# doesn't exist yet, add
|
# doesn't exist yet, add
|
||||||
|
|
||||||
if [[ ! "${section}" ]]; then
|
if [[ ! "${section}" ]]; then
|
||||||
# no section was given, add to bottom of file
|
# if no section is given, propagate the default section
|
||||||
echo "${key}${delim}${val}" >> "${file}"
|
section=${section_default}
|
||||||
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"
|
|
||||||
fi
|
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
|
else
|
||||||
# replace existing
|
# replace existing (modified to replace only keys in given section)
|
||||||
sed -i.bak -e "/^${key}${delim}/s/${delim}.*/${delim}${val}/" "${file}"
|
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
|
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
|
||||||
rm -f "${file}.bak"
|
rm -f "${file}.bak"
|
||||||
fi
|
fi
|
||||||
|
@ -3,6 +3,7 @@ exists
|
|||||||
127.0.0.1
|
127.0.0.1
|
||||||
nginx, nodejs
|
nginx, nodejs
|
||||||
--> command: Replace three values in-place and show result
|
--> command: Replace three values in-place and show result
|
||||||
|
[default]
|
||||||
orphan = no more
|
orphan = no more
|
||||||
|
|
||||||
[connection]
|
[connection]
|
||||||
@ -15,6 +16,7 @@ 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]
|
||||||
orphan = no more
|
orphan = no more
|
||||||
|
|
||||||
[connection]
|
[connection]
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
[default]
|
||||||
orphan = exists
|
orphan = exists
|
||||||
|
|
||||||
[connection]
|
[connection]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user