twpayne / chezmoi

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

Does templating work differently with scripts? #1196

Closed sheeley closed 3 years ago

sheeley commented 3 years ago

What exactly are you trying to do?

I'm trying to create a templated script that injects secrets before running. For some reason, it appears that chezmoi's normal data variables aren't available.

What have you tried so far?

I've created a templated script (licenses.sh.tmpl - also tried with run_once_ prefix) with this content:

 #!/usr/bin/env bash

defaults write com.surteesstudios.Bartender "license2" -string '{{- .bartender2License -}}'
defaults write com.surteesstudios.Bartender "license4" -string '{{- .bartender4License -}}'

And I've run these commands:

➜  chezmoi diff
chezmoi: template: bin/plists/licenses.sh.tmpl:3:68: executing "bin/plists/licenses.sh.tmpl" at <.bartender2License>: map has no entry for key "bartender2License"

12:20:24  | chezmoi  main ●: 112ms
✗  chezmoi data
{
  "bartender2license": "foo",
  ...
}

12:20:39  | chezmoi  main ●: 108ms
➜  chezmoi execute-template '{{ .bartender2license }}'
foo⏎

Where else have you checked for solutions?

Output of any commands you've tried with --verbose

$ chezmoi diff --verbose
chezmoi: template: bin/plists/licenses.sh.tmpl:3:68: executing "bin/plists/licenses.sh.tmpl" at <.bartender2License>: map has no entry for key "bartender2License"

Output of chezmoi doctor

$ chezmoi doctor
RESULT   CHECK                MESSAGE
ok       version              v2.0.11, commit b254970c28cb2744487621d4b730b3849e18ff03, built at 2021-04-26T22:45:54Z, built by goreleaser
ok       os-arch              darwin/amd64
ok       config-file          /Users/sheeley/.config/chezmoi/chezmoi.toml is a file
ok       source-dir           /Users/sheeley/.local/share/chezmoi is a directory
ok       suspicious-entries   no suspicious entries found in /Users/sheeley/.local/share/chezmoi
ok       dest-dir             /Users/sheeley is a directory
ok       shell                found /usr/local/bin/fish
ok       editor               found /usr/local/bin/nvim
ok       git-cli              found /usr/local/bin/git, version 2.31.1
ok       merge-cli            found /usr/local/bin/vimdiff
ok       age-cli              age not found in $PATH
ok       gnupg-cli            gpg not found in $PATH
ok       1password-cli        found /usr/local/bin/op, version 1.9.1
info     bitwarden-cli        bw not found in $PATH
info     gopass-cli           gopass not found in $PATH
info     keepassxc-cli        keepassxc-cli not found in $PATH
info     keepassxc-db         not set
info     lastpass-cli         lpass not found in $PATH
info     pass-cli             pass not found in $PATH
info     vault-cli            vault not found in $PATH
info     secret-cli           not set

Additional context

Add any other context about the problem here.

sheeley commented 3 years ago

Agh. I need a linter or something. Capitalization issue, sorry.

twpayne commented 3 years ago

Thanks for opening the issue! Yeah, this is a capitalization issue. chezmoi uses viper under the hood, which silently converts all variable names to lowercase (https://github.com/twpayne/chezmoi/issues/463).

sheeley commented 3 years ago

Yeah, it took me ages to notice that. I'm wondering if it would make sense to have a treatment like git does.

git: 'ram' is not a git command. See 'git --help'.

The most similar commands are
    am
    rm

Something like adding this error handling

    if err != nil && strings.Contains(err.Error(), "map has no entry for key") {
        key := getKeyFromErr(err) // - in this case, bartender2License
        // do some levenshtein magic to get similar keys ^
        fmt.Println(similarKeys)
        panic(err)
    }

Would you be interested in that functionality?