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

promptString is not yielding the expected result #3834

Closed
2 tasks done
0xjams opened this issue Jun 26, 2024 · 7 comments
Closed
2 tasks done

promptString is not yielding the expected result #3834

0xjams opened this issue Jun 26, 2024 · 7 comments
Labels
support Support request

Comments

@0xjams
Copy link

0xjams commented Jun 26, 2024

What exactly are you trying to do?

Using the example case of defining a variable during the init step. I'm currently testing by executing the template.

$ cat .chezmoi.toml.tmpl
{{- $email := promptStringOnce . "email" "Email address" -}}

[data]
    email = {{ $email | quote }}  
$ chezmoi --verbose execute-template --init --promptString [email protected] < ~/.local/share/chezmoi/.chezmoi.toml.tmpl

[data]
    email = "Email address"        

The expected result should be that the template should have been evaluated to [email protected]

What have you tried so far?

  • I've tried using promptStringOnce and promptString in the template file
  • I've tried sending the command line parameter promptString between quotes
  • I've tried using the verbose flag
  • I also thought that I was defining the template function's parameters in the wrong order, but they are OK.

Where else have you checked for solutions?

Output of chezmoi doctor

$ chezmoi doctor
RESULT    CHECK                       MESSAGE
ok        version                     v2.49.1, commit b4b55659c69fb13a502903c16dcdd566b988eece, built at 2024-06-23T12:28:09Z, built by goreleaser
ok        latest-version              v2.49.1
ok        os-arch                     linux/amd64 (Kali GNU/Linux 2024.2)
ok        uname                       Linux kali 6.8.11-amd64 #1 SMP PREEMPT_DYNAMIC Kali 6.8.11-1kali2 (2024-05-30) x86_64 GNU/Linux
ok        go-version                  go1.22.4 (gc)
ok        executable                  /usr/local/bin/chezmoi
ok        upgrade-method              sudo-upgrade-package
ok        config-file                 no config file found
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        umask                       002
ok        cd-command                  found /usr/bin/zsh
ok        cd-args                     /usr/bin/zsh
info      diff-command                not set
ok        edit-command                found /usr/bin/vi
ok        edit-args                   /usr/bin/vi
ok        git-command                 found /usr/bin/git, version 2.43.0
ok        merge-command               found /usr/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.43
info      pinentry-command            not set
info      1password-command           op not found in $PATH
info      bitwarden-command           bw not found in $PATH
info      bitwarden-secrets-command   bws not found in $PATH
info      dashlane-command            dcli not found in $PATH
info      doppler-command             doppler 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      vlt-command                 vlt not found in $PATH
info      secret-command              not set

Additional context

My use case is slightly different, but I used the default example as a sanity check. I still haven't gotten the expected result.

@0xjams 0xjams added the support Support request label Jun 26, 2024
@twpayne
Copy link
Owner

twpayne commented Jun 27, 2024

promptStringOnce only prompts for a string if it is not already set.

Do you already have data.email set in your configuration? If so, promptStringOnce will return the existing value (which might not be [email protected]) instead of prompting.

@0xjams
Copy link
Author

0xjams commented Jun 27, 2024

The example of my post was extracted from the documentation in the Create a config file on a new machine automatically section. Am I wrong to assume that after executing the execute-template subcommand with the promptStringOnce flag, the value should have been overridden and it should not have used the default value of the function call?

[data]
    email = "Email address"      

@twpayne
Copy link
Owner

twpayne commented Jun 27, 2024

Am I wrong to assume that after executing the execute-template subcommand with the promptStringOnce flag, the value should have been overridden and it should not have used the default value of the function call?

Yes.

promptString always asks for a string. promptStringOnce asks for a string if there is no existing value. If you already have a config file, then there is probably already an existing value.

Breaking the arguments to promptStringOnce down:

promptStringOnce map path prompt [default]

map and path tell chezmoi where to find the existing value.

prompt is the prompt for the user if there is no existing value.

default is the default value if the user hits enter in response to the prompt.

In the case of

promptStringOnce . "email" "Email address"

map is . and path is email which means that chezmoi is looking up the .email template variable. If you already have a config file with data.email set then the .email template variable is already set.

@0xjams
Copy link
Author

0xjams commented Jun 27, 2024

My bad. The problem was that I never actually ran chezmoi init after creating that template, I was only running chezmoi execute-template --init, I guess that execution defined the value, that's probably why promptStringOnce was not firing up.

Thank you so much! Sorry for the trouble.

@0xjams
Copy link
Author

0xjams commented Jun 27, 2024

I just tested this with interesting results.

  • First, I made sure that the ~/.config/chezmoi/chezmoi.toml file does not exist
  • I ran the execute-template function, but the template still returned the default value: email = "Email address"
  • I ran chezmoi init, and promptStringOnce ran, I set the value as [email protected].
  • After executing the template again, I got the value in chezmoi.toml as expected.
  • However, when that file did not exist, I never got the value from the command parameter, only the default one from the template.
kali@kali:~/.local/share/chezmoi$  rm ~/.config/chezmoi/chezmoi.toml
kali@kali:~/.local/share/chezmoi$  rm ~/.config/chezmoi/chezmoi.toml
rm: cannot remove '/home/kali/.config/chezmoi/chezmoi.toml': No such file or directory
❌ kali@kali:~/.local/share/chezmoi$  chezmoi execute-template --verbose --init --promptString [email protected] < ~/.local/share/chezmoi/.chezmoi.toml.tmpl

[data]
    email = "Email address"%                                                                           
kali@kali:~/.local/share/chezmoi$  chezmoi init
# Here the promptStringOnce ran, and I set the value
kali@kali:~/.local/share/chezmoi$  cat /home/kali/.config/chezmoi/chezmoi.toml

[data]
    email = "[email protected]"%                                                                               
kali@kali:~/.local/share/chezmoi$  chezmoi execute-template --verbose --init --promptString [email protected] < ~/.local/share/chezmoi/.chezmoi.toml.tmpl

[data]
    email = "[email protected]"% 

@twpayne
Copy link
Owner

twpayne commented Jul 1, 2024

The value passed to the --promptString flag is the prompt, not the key. i.e. I think you want:

$ chezmoi execute-template --verbose --init --promptString 'Email [email protected]' < ~/.local/share/chezmoi/.chezmoi.toml.tmpl

Note --promptString 'Email [email protected]' instead of --promptString [email protected].

@0xjams
Copy link
Author

0xjams commented Jul 2, 2024

Thank you so much. I completely misunderstood the flag. I thought that this --promptString [email protected] was supposed to override the value of the email variable.

@0xjams 0xjams closed this as completed Jul 2, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support Support request
Projects
None yet
Development

No branches or pull requests

2 participants