tool for modifying conf/ini files #224

Closed
opened 2021-11-20 15:21:52 +00:00 by ungleich-gitea · 10 comments

Created by: lloyd

I wonder if there is a type for parsing/modifying ini/.conf files?

In 20 minutes of looking I haven't found anything beyond the __key_value type, the __line type, and __block. None of this is a good fit.

A case this specifically this would be useful for is in toggling a value in an .ini file where that value was repeated multiple times in distinct sections:

[foo]
enabled=0

[bar]
enabled=0

I'd like to say something like:

__ini foo.enabled --file /etc/whatever.conf --value 1 --state present
*Created by: lloyd* I wonder if there is a type for parsing/modifying [ini/.conf files](http://en.wikipedia.org/wiki/INI_file)? In 20 minutes of looking I haven't found anything beyond the `__key_value` type, the `__line` type, and `__block`. None of this is a good fit. A case this specifically this would be useful for is in toggling a value in an .ini file where that value was repeated multiple times in distinct sections: ``` [foo] enabled=0 [bar] enabled=0 ``` I'd like to say something like: ``` __ini foo.enabled --file /etc/whatever.conf --value 1 --state present ```
ungleich-gitea added the
Stale
label 2021-11-20 15:21:52 +00:00
Author
Owner

closed

closed
Author
Owner

Created by: toupeira

It would be worth looking into Augeas which provides automated editing of all sorts of configuration files, you could use augtool to implement a generic __augeas type similar to the one available in Puppet.

*Created by: toupeira* It would be worth looking into [Augeas](http://augeas.net/) which provides automated editing of all sorts of configuration files, you could use `augtool` to implement a generic `__augeas` type similar to [the one available in Puppet](http://projects.puppetlabs.com/projects/1/wiki/puppet_augeas).
Author
Owner

Created by: telmich

I suggest nl instead of cat -n (-n is not posix) at first glance. I think there are some issues in your proposal, but it is actually hard to comment without a pull request. Generally speaking I'd check if you need to create the section as well (i.e. if it does not exist).

So in short, I suggest to make a PR as a basis for the discussion - even if we have to rewrite it several times

*Created by: telmich* I suggest nl instead of cat -n (-n is not posix) at first glance. I think there are some issues in your proposal, but it is actually hard to comment without a pull request. Generally speaking I'd check if you need to create the section as well (i.e. if it does not exist). So in short, I suggest to make a PR as a basis for the discussion - even if we have to rewrite it several times
Author
Owner

Created by: lloyd

(if this looks acceptable as a first implementation, I'll write docs and open a pull request)

*Created by: lloyd* (if this looks acceptable as a first implementation, I'll write docs and open a pull request)
Author
Owner

Created by: lloyd

Interesting. I can look into making the implementation a bit more elegant/idiomatic. For now, I've gotten a first version written and am using it in one of my projects.

I really like the idea of using an explorer, performing the manipulation and diff on the client machine, and then transferring a patch! Writing escaped shell in a heredoc is rather cumbersome :).

*Created by: lloyd* Interesting. I can look into making the implementation a bit more elegant/idiomatic. For now, I've gotten a [first version written](https://github.com/mozilla/awsbox/tree/v0.7.0/.cdist/type/__ini) and am using it in [one of my projects](https://github.com/mozilla/awsbox/blob/v0.7.0/.cdist/manifest/init#L7-L8). I really like the idea of using an explorer, performing the manipulation and diff on the client machine, and then transferring a patch! Writing escaped shell in a heredoc is rather cumbersome :).
Author
Owner

Created by: telmich

Lloyd,

a pointer that may help you: awk can do tracking of sections easily like
this:

awk 'BEGIN { inside = 0 } /[sektion]/ { inside=1 } { if(inside)
print($0); } /[!sektion]/ {inside=0}' inifile

No need to use awk - but I think it can easily achieve what you want :-)

Daniel suggested to use an explorer to get the whole ini file, create a
new one, copy over the diff and use patch to apply the changes. We could
do it like that or use the __file type to transfer the whole file.

Cheers,

Nico

Lloyd Hilaiel [Mon, Feb 10, 2014 at 08:14:00AM -0800]:

ok, I must have not understood quite how object_ids work.

I like the syntax you propose, and will try to craft something. Working draft of the implementation looks something like this: https://gist.github.com/lloyd/8918659


Reply to this email directly or view it on GitHub:
https://github.com/telmich/cdist/issues/291#issuecomment-34649294

PGP key: 7ED9 F7D3 6B10 81D7 0EC5 5C09 D7DC C8E4 3187 7DF0

*Created by: telmich* Lloyd, a pointer that may help you: awk can do tracking of sections easily like this: ``` awk 'BEGIN { inside = 0 } /[sektion]/ { inside=1 } { if(inside) print($0); } /[!sektion]/ {inside=0}' inifile ``` No need to use awk - but I think it can easily achieve what you want :-) Daniel suggested to use an explorer to get the whole ini file, create a new one, copy over the diff and use patch to apply the changes. We could do it like that or use the __file type to transfer the whole file. Cheers, Nico Lloyd Hilaiel [Mon, Feb 10, 2014 at 08:14:00AM -0800]: > ok, I must have not understood quite how `object_ids` work. > > I like the syntax you propose, and will try to craft something. Working draft of the implementation looks something like this: https://gist.github.com/lloyd/8918659 > > --- > > Reply to this email directly or view it on GitHub: > https://github.com/telmich/cdist/issues/291#issuecomment-34649294 ## PGP key: 7ED9 F7D3 6B10 81D7 0EC5 5C09 D7DC C8E4 3187 7DF0
Author
Owner

Created by: lloyd

ok, I must have not understood quite how object_ids work.

I like the syntax you propose, and will try to craft something. Working draft of the implementation looks something like this: https://gist.github.com/lloyd/8918659

*Created by: lloyd* ok, I must have not understood quite how `object_ids` work. I like the syntax you propose, and will try to craft something. Working draft of the implementation looks something like this: https://gist.github.com/lloyd/8918659
Author
Owner

Created by: telmich

I'd definitely recommend against this syntax, as it enforces (pseudo)
structure where it is not needed and can be ambiguous.

I would also recommend to take the object_id as a filename, but allow
the user to specific the filename using --file as well (for setting
multiple entries in multiple locations). For me, it could look like this:

__ini /etc/foobar.ini --section foo --name bar --value baz --state present

__ini /etc/foobar.ini --section foo --name bar # empty value
__ini /etc/foobar.ini --section foo --state absent # delete whole section

This is just an idea, though.

Cheers,

Nico

Lloyd Hilaiel [Mon, Feb 10, 2014 at 06:54:06AM -0800]:

ok, cool. I wonder if the __object_id should actually include the filename and property. If it did not, it would not be possible to change the same property in two files.

So something like this?

__conf_file /etc/whatever.conf:foo.enabled --value 1 --state present

Reply to this email directly or view it on GitHub:
https://github.com/telmich/cdist/issues/291#issuecomment-34640020

PGP key: 7ED9 F7D3 6B10 81D7 0EC5 5C09 D7DC C8E4 3187 7DF0

*Created by: telmich* I'd definitely recommend against this syntax, as it enforces (pseudo) structure where it is not needed and can be ambiguous. I would also recommend to take the object_id as a filename, but allow the user to specific the filename using --file as well (for setting multiple entries in multiple locations). For me, it could look like this: ``` __ini /etc/foobar.ini --section foo --name bar --value baz --state present __ini /etc/foobar.ini --section foo --name bar # empty value __ini /etc/foobar.ini --section foo --state absent # delete whole section ``` This is just an idea, though. Cheers, Nico Lloyd Hilaiel [Mon, Feb 10, 2014 at 06:54:06AM -0800]: > ok, cool. I wonder if the `__object_id` should actually include the filename and property. If it did not, it would not be possible to change the same property in two files. > > So something like this? > > ``` > __conf_file /etc/whatever.conf:foo.enabled --value 1 --state present > ``` > > --- > > Reply to this email directly or view it on GitHub: > https://github.com/telmich/cdist/issues/291#issuecomment-34640020 ## PGP key: 7ED9 F7D3 6B10 81D7 0EC5 5C09 D7DC C8E4 3187 7DF0
Author
Owner

Created by: lloyd

ok, cool. I wonder if the __object_id should actually include the filename and property. If it did not, it would not be possible to change the same property in two files.

So something like this?

__conf_file /etc/whatever.conf:foo.enabled --value 1 --state present
*Created by: lloyd* ok, cool. I wonder if the `__object_id` should actually include the filename and property. If it did not, it would not be possible to change the same property in two files. So something like this? ``` __conf_file /etc/whatever.conf:foo.enabled --value 1 --state present ```
Author
Owner

Created by: telmich

Not yet, but you are welcome to write one - I think it would be beneficial and could be accepted upstream.

*Created by: telmich* Not yet, but you are welcome to write one - I think it would be beneficial and could be accepted upstream.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: ungleich-public/cdist#224
No description provided.