Closed joshproehl closed 1 year ago
Have you tried using a modify_
script or template?
konsolerc
's format is a variant of INI, if I recall correctly, so you might be able to use fromIni
and toIni
.
You may also be able to use kwriteconfig
.
That did the job perfectly, thank you. Took me a little messing around to get the necessary syntax, but works great. My apologies, I don't know how I managed not to locate the "Manage part, but not all, of a file" segment of the docs.
For anyone finding this in the future, the full solution was to create file 'private_dot_config/private_modify_konsolerc' in my chezmoi repo containing the following code:
{{- /* chezmoi:modify-template */ -}}
{{ fromIni .chezmoi.stdin | setValueAtPath "Desktop Entry.DefaultProfile" "Solarized with hack-nerd.profile" | toIni | trim }}
I should easily be able to adapt this technique to other (non-INI) files that need the single-line treatment as well. Thanks!
What exactly are you trying to do?
I would like to find and modify a single line in a file, while leaving the rest of the file in its untouched state.
What have you tried so far?
Templates. Scripts.
Where else have you checked for solutions?
Additional context
What I'm trying to do is mimic the functionality of Ansible's line in file functionality. The use case is that I have a config file which is managed by an OS package, and I would like to ensure that a single setting in that file is changed, without having to maintain an entire copy of the config file and keep it updated in my chezmoi repo.
For example, I would like to find any line matching
/^DefaultProfile=/
in .config/konsolerc. 1) If the line exists, replace the line withDefaultProfile=My Custom Profile
2) If the line does not exist, insert a multi-line string into the file:Using chezmoi to manage this file in its entirety is a non-starter as the file contains a number of details that are highly specific to the individual machine and are regularly changed by the application if the machine's state changes. Only this one line matters.
The best way that I have found to do this is with some obtuse CLI scripts, but would prefer to rely only on chezmoi and not a combination of CLI tools that may or may not exist on any given distro.
Is there a way to do this with chezmoi that I have not found? If not, is this a feature request that would fall in chezmoi's scope? Ultimately it would be nice to be able to match/replace any regex so an entire block of a file could be replaced, but single-line replacement would suffice.