Skip to content

Commit

Permalink
fix: Only read source state if needed in source-path command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 13, 2022
1 parent ef9e0ec commit 83f01eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/cmd/sourcepathcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strings"

"github.com/spf13/cobra"

"github.com/twpayne/chezmoi/v2/pkg/chezmoi"
)

func (c *Config) newSourcePathCmd() *cobra.Command {
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/cmd/testdata/scripts/issue2354.txtar
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 83f01eb

Please sign in to comment.