diff --git a/internal/cmd/config.go b/internal/cmd/config.go index afdfcfbc791..3d07ffa12bd 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -194,6 +194,7 @@ type Config struct { chattr chattrCmdConfig dump dumpCmdConfig executeTemplate executeTemplateCmdConfig + ignored ignoredCmdConfig _import importCmdConfig init initCmdConfig managed managedCmdConfig diff --git a/internal/cmd/ignoredcmd.go b/internal/cmd/ignoredcmd.go index b46f5c18dae..4842d2f869e 100644 --- a/internal/cmd/ignoredcmd.go +++ b/internal/cmd/ignoredcmd.go @@ -1,13 +1,15 @@ package cmd import ( - "strings" - "github.com/spf13/cobra" "github.com/twpayne/chezmoi/v2/internal/chezmoi" ) +type ignoredCmdConfig struct { + tree bool +} + func (c *Config) newIgnoredCmd() *cobra.Command { ignoredCmd := &cobra.Command{ Use: "ignored", @@ -19,18 +21,18 @@ func (c *Config) newIgnoredCmd() *cobra.Command { Annotations: newAnnotations(), } + ignoredCmd.Flags().BoolVarP(&c.ignored.tree, "tree", "t", c.ignored.tree, "Print paths as a tree") + return ignoredCmd } func (c *Config) runIgnoredCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error { - builder := strings.Builder{} - for _, relPath := range sourceState.Ignored() { - if _, err := builder.WriteString(relPath.String()); err != nil { - return err - } - if err := builder.WriteByte('\n'); err != nil { - return err - } + relPaths := sourceState.Ignored() + paths := make([]string, 0, len(relPaths)) + for _, relPath := range relPaths { + paths = append(paths, relPath.String()) } - return c.writeOutputString(builder.String()) + return c.writePaths(paths, writePathsOptions{ + tree: c.ignored.tree, + }) } diff --git a/internal/cmd/testdata/scripts/ignore.txtar b/internal/cmd/testdata/scripts/ignore.txtar index 9ad157f1a54..22b6d61b8a0 100644 --- a/internal/cmd/testdata/scripts/ignore.txtar +++ b/internal/cmd/testdata/scripts/ignore.txtar @@ -11,6 +11,10 @@ exists $HOME/.file exec chezmoi ignored cmp stdout golden/ignored +# test that chezmoi ignored --tree lists the ignored entries in a tree-like format +exec chezmoi ignored --tree +cmp stdout golden/ignored-tree + chhome home2/user # test that chezmoi manage lists all managed files @@ -48,6 +52,9 @@ cmp stdout golden/managed-ignore-star-star-slash-star-dot-txt -- golden/ignored -- .dir README.md +-- golden/ignored-tree -- +.dir +README.md -- golden/managed-all -- .dir .dir/file.txt