Hi, I'd like to start the discussion about the implementation of the code as PR, if the following changes fit in the original mindset. With the addition of the comment feature, it may be necessary to update existing comments.
Editing with complex and storing regular expressions may be error-prone and may not cover all situations. I have therefore found in various scenarios that it makes more sense to clean up and recreate an entry in an existing ini file than to edit in existing files.
Especially the background that an update of a key/value pair is always placed at the beginning of a section means that comments no longer have to be placed in front of an entry. An comment entry orphaned in this way will only be reordered when an update is made.
For this reason, the following adjustment empties all lines with the matching comment and key and rewrites them at the beginning of the section. this is executed in every situation, unless only a value is to be read.
What do you think of the procedure - too drastic or appropriate? Unfortunately it is not nice that the ini file is "in motion" with every change...
...
# 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 [[ ! "${section}" ]]; then
# if no section is given, propagate the default section
section=${section_default}
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) re-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}"
...
(editing part of key/values removed)
The feature is already locally developed and extensively tested. A PR can be provided at any time.
Hi, I'd like to start the discussion about the implementation of the code as PR, if the following changes fit in the original mindset. With the addition of the comment feature, it may be necessary to update existing comments.
Editing with complex and storing regular expressions may be error-prone and may not cover all situations. I have therefore found in various scenarios that it makes more sense to clean up and recreate an entry in an existing ini file than to edit in existing files.
Especially the background that an update of a key/value pair is always placed at the beginning of a section means that comments no longer have to be placed in front of an entry. An comment entry orphaned in this way will only be reordered when an update is made.
For this reason, the following adjustment empties all lines with the matching comment and key and rewrites them at the beginning of the section. this is executed in every situation, unless only a value is to be read.
What do you think of the procedure - too drastic or appropriate? Unfortunately it is not nice that the ini file is "in motion" with every change...
...
...
... (editing part of key/values removed)
The feature is already locally developed and extensively tested. A PR can be provided at any time.
Cheerio