crudini
crudini copied to clipboard
can this utility comment out keys?
Can I say comment or uncomment "some_key" from X file?
That is not supported at present, though would be useful and is already mentioned in the TODO file
That is not supported at present, though would be useful and is already mentioned in the TODO file
Gotcha, I read the TODO, I will work on this. I have been meaning to create something like crudini for a while I manage *nix provisioning configs and I've gotten tired of doing it with plain bash. It takes way too much fiddling with sed/awk to manipulate ini files. Just so we're clear.
use case 1 set/remove comment character # or ; from:
[a-section]
# a-key = foo
to:
[a-section]
a-key = foo
use case 2 change value under comment from:
[a-section]
# a-key = foo
to:
[a-section]
# a-key = bar
use case 3 new comment from:
[a-section]
# a-key = foo
to:
[a-section]
# a-key = foo
# b-key = bar
I guess one could also comment out sections, but most often than not I am working with already made configs with sections.
Related issue is issue #22
I'm having a similar issue when modifying php.ini files:
[global]
...
; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0
It would be great to be able to uncomment the above configuration line. Currently crudini puts the configuration in the bottom of the file. However, the bottom of the file has an include statement like:
include=/etc/php/8.3/fpm/pool.d/*.conf
Because of this include statement, the namespace changes from [global] to [www] and any configuration changes that will come after the include statement above will not be recognized by the PHP ini parser.
@nicodemuz on my admin stuff I just ended up using sed and made a custom bash function. I have a bunch of custom libraries in bash with utility functions, I will admit I am not a fan a bash but you can't beat the simplicity once you learn it, I would say with less than a dozen one liners using sed/awk and other linux utils you could replace crudini all together to manage config files of all kinds.
With this function you can comment and uncomment any line in place:
comments.sh
#!/bin/bash
comment_uncomment() {
flag=$1
comment_char=$2
string_to_match=$3
file_path=$4
if [ "$flag" == "comment" ]; then
sed -i "/^[[:space:]]*[^$comment_char]/ s/$string_to_match/$comment_char&/" "$file_path"
echo "Lines containing '$string_to_match' commented in $file_path"
elif [ "$flag" == "uncomment" ]; then
sed -i -E "s/^($comment_char *?)(.*$string_to_match.*)$/\2/g" "$file_path"
echo "Lines containing '$string_to_match' uncommented in $file_path"
else
echo "Invalid flag. Please use 'comment' or 'uncomment'."
fi
}
# Usage: comment_uncomment flag comment_char string_to_match file_path
# Example usage:
# comment_uncomment comment ";" "Default Unit: seconds" "example.txt"
# comment_uncomment uncomment "#" "partial match" "example.txt"
comment_uncomment "$1" "$2" "$3" "$4"
If you save file as comments.sh and chmod to be executable you can call it as follows:
./comments.sh uncomment ";" "Default Value" /path/config.php
In addition you can save your sh file in path and you could call it from anywhere, also uncomment and comment flags could be shortened to your liking. Hope this help you.
@nicodemuz Interesting. Your issue sounds like it would be most generally handled with two options.
- to uncomment parameters if existing
- to add new parameters to the start of a section
@nicodemuz Interesting. Your issue sounds like it would be most generally handled with two options.
- to uncomment parameters if existing
- to add new parameters to the start of a section
For my use case, I would personally prefer the first option, as then the related php ini comment would be above the configuration.