Skip to content

Commit

Permalink
feat: Add --tree flag to unmanaged command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 24, 2024
1 parent 91823e3 commit 2e04dcf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
12 changes: 12 additions & 0 deletions internal/cmd/testdata/scripts/unmanagedtree.txtar
@@ -0,0 +1,12 @@
# test that chezmoi unmanaged --tree produces tree-like output
exec chezmoi unmanaged --tree
cmp stdout golden/stdout

-- golden/stdout --
.dir
file
subdir
.local
-- home/user/.dir/file --
-- home/user/.dir/subdir/file --
-- home/user/.local/share/chezmoi/dot_dir/.keep --
26 changes: 14 additions & 12 deletions internal/cmd/unmanagedcmd.go
@@ -1,10 +1,8 @@
package cmd

import (
"fmt"
"io/fs"
"sort"
"strings"

"github.com/spf13/cobra"

Expand All @@ -14,6 +12,7 @@ import (

type unmanagedCmdConfig struct {
pathStyle chezmoi.PathStyle
tree bool
}

func (c *Config) newUnmanagedCmd() *cobra.Command {
Expand All @@ -28,6 +27,7 @@ func (c *Config) newUnmanagedCmd() *cobra.Command {
}

unmanagedCmd.Flags().VarP(&c.unmanaged.pathStyle, "path-style", "p", "Path style")
unmanagedCmd.Flags().BoolVarP(&c.unmanaged.tree, "tree", "t", c.unmanaged.tree, "Print paths as a tree")

return unmanagedCmd
}
Expand Down Expand Up @@ -93,16 +93,18 @@ func (c *Config) runUnmanagedCmd(cmd *cobra.Command, args []string, sourceState
}
}

builder := strings.Builder{}
sortedRelPaths := chezmoi.RelPaths(unmanagedRelPaths.Elements())
sort.Sort(sortedRelPaths)
for _, relPath := range sortedRelPaths {
switch c.unmanaged.pathStyle {
case chezmoi.PathStyleAbsolute:
fmt.Fprintln(&builder, c.DestDirAbsPath.Join(relPath))
case chezmoi.PathStyleRelative:
fmt.Fprintln(&builder, relPath)
paths := make([]string, 0, len(unmanagedRelPaths.Elements()))
for relPath := range unmanagedRelPaths {
var path string
if c.unmanaged.pathStyle == chezmoi.PathStyleAbsolute {
path = c.DestDirAbsPath.Join(relPath).String()
} else {
path = relPath.String()
}
paths = append(paths, path)
}
return c.writeOutputString(builder.String())

return c.writePaths(paths, writePathsOptions{
tree: c.unmanaged.tree,
})
}

0 comments on commit 2e04dcf

Please sign in to comment.