Closed 0xjams closed 2 months ago
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 me@home.org
) instead of prompting.
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"
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.
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.
I just tested this with interesting results.
email = "Email address"
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=me@mail.com < ~/.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 = "me@me.com"%
kali@kali:~/.local/share/chezmoi$ chezmoi execute-template --verbose --init --promptString email=me@mail.com < ~/.local/share/chezmoi/.chezmoi.toml.tmpl
[data]
email = "me@me.com"%
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 address=me@mail.com' < ~/.local/share/chezmoi/.chezmoi.toml.tmpl
Note --promptString 'Email address=me@mail.com'
instead of --promptString email=me@mail.com
.
Thank you so much. I completely misunderstood the flag. I thought that this --promptString email=me@mail.com
was supposed to override the value of the email
variable.
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.
The expected result should be that the template should have been evaluated to
me@home.org
What have you tried so far?
Where else have you checked for solutions?
Output of
chezmoi doctor
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.