From 971a83ea3af7c9251a7ce56fee084b3095ab1365 Mon Sep 17 00:00:00 2001 From: "R. Fuehrer" Date: Tue, 31 Dec 2019 15:16:08 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 2 +- README.md | 1 + src/ini_val.sh | 40 +++++++++++++++++++++++----------- test/fixture/ini_val.stdio | 2 ++ test/scenario/ini_val/data.ini | 1 + 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f23178..62549a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 613f774..d2d45c0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ini_val.sh b/src/ini_val.sh index b530530..1fbf24b 100755 --- a/src/ini_val.sh +++ b/src/ini_val.sh @@ -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> "${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 diff --git a/test/fixture/ini_val.stdio b/test/fixture/ini_val.stdio index 5034a09..1b97ec3 100644 --- a/test/fixture/ini_val.stdio +++ b/test/fixture/ini_val.stdio @@ -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] diff --git a/test/scenario/ini_val/data.ini b/test/scenario/ini_val/data.ini index d55cef4..785937b 100644 --- a/test/scenario/ini_val/data.ini +++ b/test/scenario/ini_val/data.ini @@ -1,3 +1,4 @@ +[default] orphan = exists [connection]