1
1
package cmd
2
2
3
3
import (
4
+ "cmp"
4
5
"fmt"
5
6
6
7
"github.com/spf13/cobra"
@@ -10,6 +11,7 @@ import (
10
11
11
12
type managedCmdConfig struct {
12
13
filter * chezmoi.EntryTypeFilter
14
+ format * choiceFlag
13
15
pathStyle * choiceFlag
14
16
tree bool
15
17
}
@@ -29,6 +31,7 @@ func (c *Config) newManagedCmd() *cobra.Command {
29
31
}
30
32
31
33
managedCmd .Flags ().VarP (c .managed .filter .Exclude , "exclude" , "x" , "Exclude entry types" )
34
+ managedCmd .Flags ().VarP (c .managed .format , "format" , "f" , "Format" )
32
35
managedCmd .Flags ().VarP (c .managed .filter .Include , "include" , "i" , "Include entry types" )
33
36
managedCmd .Flags ().VarP (c .managed .pathStyle , "path-style" , "p" , "Path style" )
34
37
must (managedCmd .RegisterFlagCompletionFunc ("path-style" , c .managed .pathStyle .FlagCompletionFunc ()))
@@ -51,7 +54,13 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *c
51
54
}
52
55
}
53
56
54
- var paths []fmt.Stringer
57
+ type entryPaths struct {
58
+ targetRelPath chezmoi.RelPath
59
+ Absolute chezmoi.AbsPath `json:"absolute" yaml:"absolute"`
60
+ SourceAbsolute chezmoi.AbsPath `json:"sourceAbsolute" yaml:"sourceAbsolute"`
61
+ SourceRelative chezmoi.SourceRelPath `json:"sourceRelative" yaml:"sourceRelative"`
62
+ }
63
+ var allEntryPaths []* entryPaths
55
64
_ = sourceState .ForEach (
56
65
func (targetRelPath chezmoi.RelPath , sourceStateEntry chezmoi.SourceStateEntry ) error {
57
66
if ! c .managed .filter .IncludeSourceStateEntry (sourceStateEntry ) {
@@ -80,25 +89,42 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string, sourceState *c
80
89
}
81
90
}
82
91
83
- var path fmt.Stringer
84
- switch pathStyle := c .managed .pathStyle .String (); pathStyle {
85
- case pathStyleAbsolute :
86
- path = c .DestDirAbsPath .Join (targetRelPath )
87
- case pathStyleRelative :
88
- path = targetRelPath
89
- case pathStyleSourceAbsolute :
90
- path = c .SourceDirAbsPath .Join (sourceStateEntry .SourceRelPath ().RelPath ())
91
- case pathStyleSourceRelative :
92
- path = sourceStateEntry .SourceRelPath ().RelPath ()
93
- default :
94
- return fmt .Errorf ("%s: invalid path style" , pathStyle )
92
+ entryPaths := & entryPaths {
93
+ targetRelPath : targetRelPath ,
94
+ Absolute : c .DestDirAbsPath .Join (targetRelPath ),
95
+ SourceAbsolute : c .SourceDirAbsPath .Join (sourceStateEntry .SourceRelPath ().RelPath ()),
96
+ SourceRelative : sourceStateEntry .SourceRelPath (),
95
97
}
96
- paths = append (paths , path )
98
+ allEntryPaths = append (allEntryPaths , entryPaths )
97
99
return nil
98
100
},
99
101
)
100
102
101
- return c .writePaths (stringersToStrings (paths ), writePathsOptions {
102
- tree : c .managed .tree ,
103
- })
103
+ switch pathStyle := c .managed .pathStyle .String (); pathStyle {
104
+ case pathStyleAbsolute , pathStyleRelative , pathStyleSourceAbsolute , pathStyleSourceRelative :
105
+ paths := make ([]string , len (allEntryPaths ))
106
+ for i , structuredPath := range allEntryPaths {
107
+ switch c .managed .pathStyle .String () {
108
+ case pathStyleAbsolute :
109
+ paths [i ] = structuredPath .Absolute .String ()
110
+ case pathStyleRelative :
111
+ paths [i ] = structuredPath .targetRelPath .String ()
112
+ case pathStyleSourceAbsolute :
113
+ paths [i ] = structuredPath .SourceAbsolute .String ()
114
+ case pathStyleSourceRelative :
115
+ paths [i ] = structuredPath .SourceRelative .String ()
116
+ }
117
+ }
118
+ return c .writePaths (paths , writePathsOptions {
119
+ tree : c .managed .tree ,
120
+ })
121
+ case pathStyleAll :
122
+ allEntryPathsMap := make (map [string ]* entryPaths , len (allEntryPaths ))
123
+ for _ , entryPaths := range allEntryPaths {
124
+ allEntryPathsMap [entryPaths .targetRelPath .String ()] = entryPaths
125
+ }
126
+ return c .marshal (cmp .Or (c .managed .format .String (), c .Format .String ()), allEntryPathsMap )
127
+ default :
128
+ return fmt .Errorf ("%s: invalid path style" , pathStyle )
129
+ }
104
130
}
0 commit comments