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

chezmoi modify command #2826

Closed
felipecrs opened this issue Mar 6, 2023 · 3 comments
Closed

chezmoi modify command #2826

felipecrs opened this issue Mar 6, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@felipecrs
Copy link
Contributor

felipecrs commented Mar 6, 2023

Is your feature request related to a problem? Please describe.

Since I started using chezmoi modify template I never looked back. The solution is pretty clean, fast, and platform independent. But I believe it could be leverage for more than just managing some files in the sourceDir. For example, see this:

Where I'm using a modify_settings.json file to modify the VS Code settings for Windows from within chezmoi installed in WSL.

Describe the solution you'd like

I believe it would be awesome if we had a command like chezmoi modify <template> [<files>], which would modify <files> with <template> being a modify template and would respect some other chezmoi flags like --verbose to output a diff or also --force to ask/not ask for confirmation.

Describe alternatives you've considered

result=$(cat <file> | chezmoi execute-template --with-stdin <(cat modify_template_file))
tee <file> <<<"$result"

Additional context

N/A

@felipecrs felipecrs added the enhancement New feature or request label Mar 6, 2023
@twpayne
Copy link
Owner

twpayne commented Mar 6, 2023

Thanks, this is an interesting idea, but...

  • Modify templates are a clever hack for modifying arbitrary text files and have very practical value in the context of chezmoi, but they are very much a hack. If you were asked "design a system to make changes to a small part of a file while keeping the rest intact" then you almost certainly wouldn't chose text/template as a starting language and add a couple of fromJson | setValueAtPath | toJson-type functions to it. I mean, maybe you would have those functions, but you wouldn't choose text/template as your base language.
  • The chezmoi modify command would turn chezmoi from being a dotfile manager into a dotfile manager and also an interpreter for a poorly-specified, ad-hoc implementation of Common Lisp. Fundamentally, there are better grammars for modifying files (e.g. see Run modify scripts in parallel #2814 (comment), also sed, jq, and similar).

In summary, I don't think chezmoi should become an interpreter for a clever-hack language. Instead, I think it would be better for chezmoi to provide something like https://github.com/itchyny/gojq as a template function.

@felipecrs
Copy link
Contributor Author

I totally understand. Adding such command would expose chezmoi to being more than just a dotfiles manager, and I understand your concerns about the consequences of doing so.

For my use case, I'd still be using it in the dotfiles context though, but I'm fine with the alternative (it's only sad that I can't achieve a similar diff style output as the other files being modified by my dotfiles).

Instead, I think it would be better for chezmoi to provide something like https://github.com/itchyny/gojq as a template function.

That's interesting and may be useful for someone (currently not for me, I think).

So, thanks for the discussion. I'll be closing this issue as not planned then, but feel free to reopen if there is still something you believe can be extracted from this.

@felipecrs felipecrs closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2023
@halostatue
Copy link
Collaborator

One note on gojq: if used, chezmoi should probably override its reading of the ~/.jq library if it exists. I tried using gojq for a project recently and because I have functions present in my ~/.jq that the author of gojq refuses to implement (I disagree with the reasoning, but accept it). I could get nothing of use from gojq without doing HOME= gojq ….

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants