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

Invalid key in config when using dash #2198

Closed
mcexit opened this issue Jul 4, 2022 · 3 comments · Fixed by #2202
Closed

Invalid key in config when using dash #2198

mcexit opened this issue Jul 4, 2022 · 3 comments · Fixed by #2202
Labels
enhancement New feature or request

Comments

@mcexit
Copy link
Contributor

mcexit commented Jul 4, 2022

Describe the bug

Whenever trying to use a key with a dash in `~/.config/chezmoi/chezmoi.toml, it gives an error. According to the TOML v1.0.0 spec1 this should be valid.

To reproduce

Add a key with a dash/hypen in the chezmoi config.toml

Expected behavior

Allow key with dash/hyphen.

Output of command with the --verbose flag

chezmoi: invalid config: /home/user/.config/chezmoi/chezmoi.toml: key-name: invalid key

Output of chezmoi doctor

chezmoi: warning: /home/user/.config/chezmoi/chezmoi.toml: key-name: invalid key
RESULT    CHECK                MESSAGE
warning   version              v2.18.1, built at 2022-06-23T20:04:43Z
ok        latest-version       v2.18.1
ok        os-arch              linux/amd64 (Arch Linux)
ok        uname                Linux host 5.18.9-zen1-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Sat, 02 Jul 2022 21:03:08 +0000 x86_64 GNU/Linux
ok        go-version           go1.18.3 (gc)
ok        executable           /usr/bin/chezmoi
ok        config-file          ~/.config/chezmoi/chezmoi.toml, last modified 2022-07-04T15:44:59-07:00
warning   source-dir           ~/.local/share/chezmoi is a git working tree (dirty)
ok        suspicious-entries   no suspicious entries
warning   working-tree         ~/.local/share/chezmoi is a git working tree (dirty)
ok        dest-dir             ~ is a directory
ok        shell-command        found /bin/bash
ok        shell-args           /bin/bash
ok        cd-command           found /bin/bash
ok        cd-args              /bin/bash
ok        edit-command         found /usr/bin/vim
ok        edit-args            /usr/bin/vim
info      diff-command         not set
ok        umask                022
ok        git-command          found /usr/bin/git, version 2.37.0
ok        merge-command        found /usr/bin/vimdiff
ok        age-command          found /usr/bin/age, version 1.0.0
ok        gpg-command          found /usr/bin/gpg, version 2.2.35
info      pinentry-command     not set
info      1password-command    op not found in $PATH
info      bitwarden-command    bw not found in $PATH
ok        gopass-command       found /usr/bin/gopass, version 1.14.3
ok        keepassxc-command    found /usr/bin/keepassxc-cli, version 2.7.1
info      keeper-command       keeper not found in $PATH
info      keepassxc-db         not set
info      lastpass-command     lpass not found in $PATH
ok        pass-command         found /usr/bin/pass, version 1.7.4
info      vault-command        vault not found in $PATH
info      secret-command       not set

Additional context

Unsure if this is an issue with the module or version of module being used. If so, then I can report upstream if desired.

Footnotes

  1. https://toml.io/en/v1.0.0#keys

@twpayne
Copy link
Owner

twpayne commented Jul 6, 2022

This is a deliberate extra check added by chezmoi. Keys with dashes in them cannot be used unquoted in templates. See #1438.

@mcexit
Copy link
Contributor Author

mcexit commented Jul 8, 2022

@twpayne It isn't even getting that far to let me quote it. It appears to be doing the check prior to templating. I did find out that it appears to work with the .chezmoidata.toml in the source state, however the goal here is to utilize the config file outside of the source state and add an unpredictable number of key-value pairs. I am experimenting with using chezmoi.toml to add config blocks to dotfiles, some use INI format, TOML, etc.

I'm curious on the design decision to use different rules from .chezmoidata.toml in the source state and chezmoi.toml in the config directory. .chezmoidata.toml appears to offer all the features I'm looking for, including maintaining capitalization. Maybe the best route is to figure out how to automatically copy the user config into the source state and then add it to .gitignore. This is less than ideal, but should be doable.

I've worked around this to some extent by just converting the casing using Sprig template functions in the templates, but there are some this won't work due to multiple uppercase letters in key names (e.g. SSLType). It is fairly easy though to convert example_key to example-key or exampleKey or ExampleKey. Ideally a user would be able to reference standard key names though from the apps config docs rather than having to convert them.

Regardless thank you for this amazing project and your time!

A separate request that I may open would be to integrate gomplate12 or additional functions to convert to TOML23 and potentially INI4 format as well.

Footnotes

  1. https://github.com/hairyhenderson/gomplate

  2. https://docs.gomplate.ca/functions/data/#data-totoml 2

  3. https://gohugo.io/functions/transform.unmarshal/

  4. https://github.com/go-ini/ini

@twpayne
Copy link
Owner

twpayne commented Jul 8, 2022

Thank you for the detailed follow-up and for your kind words :)

I'm curious on the design decision to use different rules from .chezmoidata.toml in the source state and chezmoi.toml in the config directory.

This was an oversight. I initially added the check to disallow dashes in keys as a way to reduce the support burden on me when users hit the problem: instead of getting confused with invalid template variable names, they would instead get an early warning. However, I didn't apply the same checks to .chezmoidata.toml. #2202 removes the check, and makes chezmoi consistent in its parsing (with the exception of spf13/cobra downcasing keys in the config file, of course).

.chezmoidata.toml appears to offer all the features I'm looking for, including maintaining capitalization. Maybe the best route is to figure out how to automatically copy the user config into the source state and then add it to .gitignore. This is less than ideal, but should be doable.

Yeah, case preservation (#463) is a long-standing bug in a dependency that is sadly quite hard to fix. I would question whether preserving case is so important that a major work-around is preferable to losing case.

A separate request that I may open would be to integrate gomplate12 or additional functions to convert to TOML23 and potentially INI4 format as well.

Please do open separate issues for these so we can track them.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2022
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

Successfully merging a pull request may close this issue.

2 participants