Skip to content

Commit

Permalink
feat: Add --tree flag to managed command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 24, 2024
1 parent 278e2be commit 91823e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
15 changes: 5 additions & 10 deletions internal/cmd/managedcmd.go
@@ -1,10 +1,6 @@
package cmd

import (
"fmt"
"sort"
"strings"

"github.com/spf13/cobra"

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
Expand All @@ -13,6 +9,7 @@ import (
type managedCmdConfig struct {
filter *chezmoi.EntryTypeFilter
pathStyle chezmoi.PathStyle
tree bool
}

func (c *Config) newManagedCmd() *cobra.Command {
Expand All @@ -30,6 +27,7 @@ func (c *Config) newManagedCmd() *cobra.Command {
managedCmd.Flags().VarP(c.managed.filter.Exclude, "exclude", "x", "Exclude entry types")
managedCmd.Flags().VarP(c.managed.filter.Include, "include", "i", "Include entry types")
managedCmd.Flags().VarP(&c.managed.pathStyle, "path-style", "p", "Path style")
managedCmd.Flags().BoolVarP(&c.managed.tree, "tree", "t", c.managed.tree, "Print paths as a tree")

return managedCmd
}
Expand Down Expand Up @@ -93,10 +91,7 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *c
},
)

sort.Strings(paths)
builder := strings.Builder{}
for _, path := range paths {
fmt.Fprintln(&builder, path)
}
return c.writeOutputString(builder.String())
return c.writePaths(paths, writePathsOptions{
tree: c.managed.tree,
})
}
20 changes: 20 additions & 0 deletions internal/cmd/testdata/scripts/managedtree.txtar
@@ -0,0 +1,20 @@
mksourcedir

# test that chezmoi managed --tree produces tree-like output
exec chezmoi managed --tree
cmp stdout golden/stdout

-- golden/stdout --
.create
.dir
file
subdir
file
.empty
.executable
.file
.private
.readonly
.remove
.symlink
.template

0 comments on commit 91823e3

Please sign in to comment.