forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
64 lines (57 loc) · 1.47 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
vfs "github.com/twpayne/go-vfs"
)
type dumpCmdConfig struct {
format string
recursive bool
}
var dumpCmd = &cobra.Command{
Use: "dump [targets...]",
Short: "Write a dump of the target state to stdout",
RunE: makeRunE(config.runDumpCmd),
}
func init() {
rootCmd.AddCommand(dumpCmd)
persistentFlags := dumpCmd.PersistentFlags()
persistentFlags.StringVarP(&config.dump.format, "format", "f", "json", "format (JSON, TOML, or YAML)")
persistentFlags.BoolVarP(&config.dump.recursive, "recursive", "r", true, "recursive")
}
func (c *Config) runDumpCmd(fs vfs.FS, args []string) error {
format, ok := formatMap[strings.ToLower(c.dump.format)]
if !ok {
return fmt.Errorf("%s: unknown format", c.dump.format)
}
ts, err := c.getTargetState(fs)
if err != nil {
return err
}
var concreteValue interface{}
if len(args) == 0 {
concreteValue, err = ts.ConcreteValue(c.dump.recursive)
if err != nil {
return err
}
} else {
entries, err := c.getEntries(ts, args)
if err != nil {
return err
}
var concreteValues []interface{}
for _, entry := range entries {
entryConcreteValue, err := entry.ConcreteValue(ts.DestDir, ts.TargetIgnore.Match, ts.SourceDir, c.dump.recursive)
if err != nil {
return err
}
if entryConcreteValue != nil {
concreteValues = append(concreteValues, entryConcreteValue)
}
}
concreteValue = concreteValues
}
return format(os.Stdout, concreteValue)
}