twpayne / chezmoi

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

How to escape templating #2234

Closed romariorobby closed 2 years ago

romariorobby commented 2 years ago

What exactly are you trying to do?

I tried to create a template for lf. in the lfrc config use {{ }} to create custom function, as result it gives templating error for chezmoi. is there a way to get around this? here's example

//
{{- if (eq .chezmoi.os "linux") }}
cmd tar ${{
    set -f
    mkdir $1
    cp -r $fx $1
    tar czf $1.tar.gz $1
    rm -rf $1
}}
{{- end }}

What have you tried so far?

Where else have you checked for solutions?

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

$ chezmoi --verbose execute-template < x.tmpl
chezmoi: template: stdin:43: bad number syntax: "-f" in action started at stdin:42

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE warning version v2.19.0, built at 2022-07-19T08:35:34Z ok latest-version v2.19.0 ok os-arch linux/amd64 (Arch Linux) ok uname Linux arch 5.18.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 02 Jul 2022 21:03:06 +0000 x86_64 GNU/Linux ok go-version go1.18.4 (gc) ok executable /usr/bin/chezmoi ok config-file ~/.config/chezmoi/chezmoi.toml, last modified 2022-07-27T14:11:07+07:00 warning source-dir ~/.local/share/chezmoi is a git working tree (dirty) warning suspicious-entries ~/.local/share/chezmoi/home/.chezmoiignore-bak warning working-tree ~/.local/share/chezmoi is a git working tree (dirty) ok dest-dir ~ is a directory ok shell-command found /usr/bin/zsh ok shell-args /usr/bin/zsh ok cd-command found /usr/bin/zsh ok cd-args /usr/bin/zsh ok edit-command found /usr/bin/nvim ok edit-args /usr/bin/nvim info diff-command not set ok umask 022 ok git-command found /usr/bin/git, version 2.37.1 warning merge-command vimdiff not found in $PATH info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.2.36 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info gopass-command gopass not found in $PATH info keepassxc-command keepassxc-cli not found in $PATH info keeper-command keeper not found in $PATH info keepassxc-db not set info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

lfrc config Add any other context about the problem here.

twpayne commented 2 years ago

To get {{ in the output, use {{ "{{" }} in the template.

Hope this helps.

romariorobby commented 2 years ago

Sorry, i didn't quite understand. where should i put the double quotes? only on clashes character? or whole block? I tried these but still give me templating error. do you mind if you give me example from example i gave above? Thanks

twpayne commented 2 years ago

You'll need to do something like

//
{{- if (eq .chezmoi.os "linux") }}
cmd tar ${{ "{{" }}
    set -f
    mkdir $1
    cp -r $fx $1
    tar czf $1.tar.gz $1
    rm -rf $1
{{ "}}" }}
{{- end }}
romariorobby commented 2 years ago

Ahhh I see, silly me. Thank you!