Skip to content

Commit

Permalink
fix: Make execute-template --init not read .chezmoitemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 18, 2023
1 parent 07ef06e commit d85b5e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
@@ -1,8 +1,10 @@
# `.chezmoi.$FORMAT.tmpl`

If a file called `.chezmoi.$FORMAT.tmpl` exists then `chezmoi init` will use it
to create an initial config file. `$FORMAT` must be one of the supported
config file formats, e.g. `json`, `jsonc`, `toml`, or `yaml`.
to create an initial config file. `$FORMAT` must be one of the supported config
file formats, e.g. `json`, `jsonc`, `toml`, or `yaml`. Templates defined in
`.chezmoitemplates` are not available because the template is executed before
the source state is read.

!!! example

Expand Down
15 changes: 13 additions & 2 deletions internal/chezmoi/sourcestate.go
Expand Up @@ -131,6 +131,7 @@ type SourceState struct {
defaultTemplateDataFunc func() map[string]any
templateDataOnly bool
readTemplateData bool
readTemplates bool
defaultTemplateData map[string]any
userTemplateData map[string]any
priorityTemplateData map[string]any
Expand Down Expand Up @@ -222,6 +223,13 @@ func WithReadTemplateData(readTemplateData bool) SourceStateOption {
}
}

// WithReadTemplates sets whether to read .chezmoitemplates directories.
func WithReadTemplates(readTemplates bool) SourceStateOption {
return func(s *SourceState) {
s.readTemplates = readTemplates
}
}

// WithSourceDir sets the source directory.
func WithSourceDir(sourceDirAbsPath AbsPath) SourceStateOption {
return func(s *SourceState) {
Expand Down Expand Up @@ -286,6 +294,7 @@ func NewSourceState(options ...SourceStateOption) *SourceState {
httpClient: http.DefaultClient,
logger: &log.Logger,
readTemplateData: true,
readTemplates: true,
priorityTemplateData: make(map[string]any),
userTemplateData: make(map[string]any),
templateOptions: DefaultTemplateOptions,
Expand Down Expand Up @@ -945,8 +954,10 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
}
return s.addTemplateData(sourceAbsPath)
case fileInfo.Name() == TemplatesDirName:
if err := s.addTemplatesDir(ctx, sourceAbsPath); err != nil {
return err
if s.readTemplates {
if err := s.addTemplatesDir(ctx, sourceAbsPath); err != nil {
return err
}
}
return fs.SkipDir
case s.templateDataOnly:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/executetemplatecmd.go
Expand Up @@ -99,6 +99,7 @@ func (c *Config) newExecuteTemplateCmd() *cobra.Command {
func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error {
options := []chezmoi.SourceStateOption{
chezmoi.WithTemplateDataOnly(true),
chezmoi.WithReadTemplates(!c.executeTemplate.init),
}
if c.executeTemplate.init {
options = append(options, chezmoi.WithReadTemplateData(false))
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/testdata/scripts/issue3418.txtar
@@ -0,0 +1,14 @@
# test that chezmoi execute-template --init does read .chezmoitemplates
stdin $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
exec chezmoi execute-template
stdout 'contents of template'

# test that chezmoi execute-template --init does not read .chezmoitemplates
stdin $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
! exec chezmoi execute-template --init
! stdout 'contents of template'

-- home/user/.local/share/chezmoi/.chezmoi.toml.tmpl --
{{ template "template" }}
-- home/user/.local/share/chezmoi/.chezmoitemplates/template --
# contents of template

0 comments on commit d85b5e4

Please sign in to comment.