Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can this utility comment out keys? #97

Open
TheFern2 opened this issue Aug 23, 2023 · 7 comments
Open

can this utility comment out keys? #97

TheFern2 opened this issue Aug 23, 2023 · 7 comments

Comments

@TheFern2
Copy link

Can I say comment or uncomment "some_key" from X file?

@pixelb
Copy link
Owner

pixelb commented Aug 23, 2023

That is not supported at present, though would be useful and is already mentioned in the TODO file

@TheFern2
Copy link
Author

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.

@pixelb
Copy link
Owner

pixelb commented Sep 29, 2023

Related issue is issue #22

@nicodemuz
Copy link

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.

@TheFern2
Copy link
Author

TheFern2 commented Jan 4, 2024

@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.

@pixelb
Copy link
Owner

pixelb commented Jan 4, 2024

@nicodemuz Interesting.
Your issue sounds like it would be most generally handled with two options.

  1. to uncomment parameters if existing
  2. to add new parameters to the start of a section

@nicodemuz
Copy link

@nicodemuz Interesting. Your issue sounds like it would be most generally handled with two options.

  1. to uncomment parameters if existing
  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants