From 83f01eb563a3ed1d9596a90f0d3f975d110090ca Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 13 Sep 2022 23:32:42 +0200 Subject: [PATCH] fix: Only read source state if needed in source-path command --- pkg/cmd/sourcepathcmd.go | 11 +++++++---- pkg/cmd/testdata/scripts/issue2354.txtar | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 pkg/cmd/testdata/scripts/issue2354.txtar diff --git a/pkg/cmd/sourcepathcmd.go b/pkg/cmd/sourcepathcmd.go index 3edc594594f..4ed13e35abb 100644 --- a/pkg/cmd/sourcepathcmd.go +++ b/pkg/cmd/sourcepathcmd.go @@ -5,8 +5,6 @@ import ( "strings" "github.com/spf13/cobra" - - "github.com/twpayne/chezmoi/v2/pkg/chezmoi" ) func (c *Config) newSourcePathCmd() *cobra.Command { @@ -16,17 +14,22 @@ func (c *Config) newSourcePathCmd() *cobra.Command { Long: mustLongHelp("source-path"), Example: example("source-path"), ValidArgsFunction: c.targetValidArgs, - RunE: c.makeRunEWithSourceState(c.runSourcePathCmd), + RunE: c.runSourcePathCmd, } return sourcePathCmd } -func (c *Config) runSourcePathCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error { +func (c *Config) runSourcePathCmd(cmd *cobra.Command, args []string) error { if len(args) == 0 { return c.writeOutputString(c.SourceDirAbsPath.String() + "\n") } + sourceState, err := c.newSourceState(cmd.Context()) + if err != nil { + return err + } + sourceAbsPaths, err := c.sourceAbsPaths(sourceState, args) if err != nil { return err diff --git a/pkg/cmd/testdata/scripts/issue2354.txtar b/pkg/cmd/testdata/scripts/issue2354.txtar new file mode 100644 index 00000000000..c9ae77c44d5 --- /dev/null +++ b/pkg/cmd/testdata/scripts/issue2354.txtar @@ -0,0 +1,6 @@ +# test that chezmoi source-path does not read the source state if not needed +exec chezmoi source-path +stdout ${CHEZMOISOURCEDIR@R} + +-- home/user/.local/share/chezmoi/.chezmoiexternal.json -- +invalid JSON file