twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

`fromIni` failed to parse hex color code inside chezmoi template #2972

Closed yusuftaufiq closed 1 year ago

yusuftaufiq commented 1 year ago

Describe the bug

I want to use the chezmoi template to modify and merge a .gitconfig that uses the .ini file format. When I'm using the fromIni function when the .gitconfig value contains a hex color code, it looks like the parsed value is incorrect.

To reproduce

  1. Create a .gitconfig file inside the .chezmoitemplates folder with the following contents:
    [delta]
        file-style = "#81A1C1" bold
  2. Then, create a modify_dot_gitconfig file inside the chezmoi source directory with the following code:
    {{- /* chezmoi:modify-template */ -}}
    {{- $baseGitConfig := includeTemplate ".gitconfig" | fromIni -}}
    {{- $baseGitConfig | toIni -}}
  3. Finally when printing .gitconfig using:
    chezmoi cat ~/.gitconfig

    I get the following output:

    [delta]
    file-style = "\""

Expected behavior

Instead of getting file-style = "\"", I expected to get file-style = "#81A1C1" bold.

Output of command with the --verbose flag

$ chezmoi --verbose cat ~/.gitconfig
[delta]
file-style = "\""

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE ok version v2.33.4, commit b9603835e602a317d4cca08a903add9f8713e88e, built at 2023-05-02T14:54:35Z, built by goreleaser ok latest-version v2.33.4 ok os-arch linux/amd64 (Pop!_OS 22.04 LTS) ok uname Linux pop-os 6.2.6-76060206-generic #202303130630~1680814622~22.04~3850312 SMP PREEMPT_DYNAMIC Thu A x86_64 x86_64 x86_64 GNU/Linux ok go-version go1.20.3 (gc) ok executable ~/bin/chezmoi ok upgrade-method replace-executable ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2023-05-02T10:49:55+07:00 warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) warning suspicious-entries ~/.local/share/chezmoi/dot_config/chezmoi/chezmoi.toml warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok umask 002 ok cd-command found /usr/bin/zsh ok cd-args /usr/bin/zsh ok diff-command found /usr/bin/delta ok edit-command found /usr/bin/codium ok edit-args codium --wait ok git-command found /usr/bin/git, version 2.34.1 ok merge-command found /home/linuxbrew/.linuxbrew/bin/vimdiff ok shell-command found /usr/bin/zsh ok shell-args /usr/bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.27 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info dashlane-command dcli not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info rbw-command rbw not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

Actually inside modify_dot_gitconfig, I used the code above to combine the .gitconfig from the chezmoi template with the current .gitconfig from stdin, here is the full code:

{{-- /* chezmoi:modify-template */ --}}
{{- $baseGitConfig := includeTemplate ".gitconfig" | fromIni -}}
{{- $currentGitConfig := fromIni .chezmoi.stdin -}}

{{- range $groupKey, $groupValue := $baseGitConfig -}}
    {{- range $key, $value := $groupValue -}}
        {{- $joinedKey := printf "%s.%s" $groupKey $key -}}
        {{- $currentGitConfig := $currentGitConfig | setValueAtPath $joinedKey $value -}}
    {{- end -}}
{{- end -}}

{{- $currentGitConfig | toIni | trim -}}

Also in the real world, my .gitconfig would contain something like this too

twpayne commented 1 year ago
[delta]
    file-style = "#81A1C1" bold

This is not a valid .ini file, which is why fromIni is complaining.

yusuftaufiq commented 1 year ago

This is not a valid .ini file, which is why fromIni is complaining.

Thanks for your quick response, can you tell me about a valid .ini file from the example above? Because I tried another configuration like this:

[delta]
file-style = '#81A1C1'

But I seem to get the same result as I described in this issue.

twpayne commented 1 year ago

There is no official .ini file specification. Different programs use different variations of the format. chezmoi's fromIni template function uses the gopkg.in/ini.v1 package.

Note also that you're talking about modifying git's configuration, and git doesn't use the .ini format. git uses its own undocumented config file format. You can use git config to make changes.

halostatue commented 1 year ago

I use delta, but I haven’t used the colour theming like that. However, instead of doing modify_dot_gitconfig.tmpl with a modification script, why not just go all the way with dot_gitconfig.tmpl and drop {{ include "delta.gitconfig" }} at the bottom? See include for the exact details?

yusuftaufiq commented 1 year ago

I use delta, but I haven’t used the colour theming like that. However, instead of doing modify_dot_gitconfig.tmpl with a modification script, why not just go all the way with dot_gitconfig.tmpl and drop {{ include "delta.gitconfig" }} at the bottom? See include for the exact details?

Thanks for your suggestion but I'd rather merge .gitconfig than replace it entirely as I have several different configurations like code editors on multiple OSes. So this is why I used modify_dot_gitconfig before.

I thought I'd take @twpayne's suggestion to use git config to make changes and I'd use it with the chezmoi script instead.

halostatue commented 1 year ago

I use delta, but I haven’t used the colour theming like that. However, instead of doing modify_dot_gitconfig.tmpl with a modification script, why not just go all the way with dot_gitconfig.tmpl and drop {{ include "delta.gitconfig" }} at the bottom? See include for the exact details? Thanks for your suggestion but I'd rather merge .gitconfig than replace it entirely as I have several different configurations like code editors on multiple OSes. So this is why I used modify_dot_gitconfig before.

I pretty much only use macOS, but I have built dynamic support for different diff tools using full templates. The same could be done for code editors, etc.

yusuftaufiq commented 1 year ago

I pretty much only use macOS, but I have built dynamic support for different diff tools using full templates. The same could be done for code editors, etc.

Ah, thanks! I seem to have missed that point, I'll try to check it out.