Skip to content

Commit

Permalink
feat: Add --tree flag to ignored command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 24, 2024
1 parent 8e3f44c commit 278e2be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions internal/cmd/config.go
Expand Up @@ -194,6 +194,7 @@ type Config struct {
chattr chattrCmdConfig
dump dumpCmdConfig
executeTemplate executeTemplateCmdConfig
ignored ignoredCmdConfig
_import importCmdConfig
init initCmdConfig
managed managedCmdConfig
Expand Down
24 changes: 13 additions & 11 deletions 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",
Expand All @@ -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,
})
}
7 changes: 7 additions & 0 deletions internal/cmd/testdata/scripts/ignore.txtar
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 278e2be

Please sign in to comment.