/
dump.go
68 lines (60 loc) · 1.61 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
65
66
67
68
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
type dumpCmdConfig struct {
format string
recursive bool
}
var dumpCmd = &cobra.Command{
Use: "dump [targets...]",
Short: "Write a dump of the target state to stdout",
Long: mustGetLongHelp("dump"),
Example: getExample("dump"),
PreRunE: config.ensureNoError,
RunE: 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")
markRemainingZshCompPositionalArgumentsAsFiles(dumpCmd, 1)
}
func (c *Config) runDumpCmd(cmd *cobra.Command, 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(nil)
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.TargetIgnore.Match, ts.SourceDir, os.FileMode(c.Umask), c.dump.recursive)
if err != nil {
return err
}
if entryConcreteValue != nil {
concreteValues = append(concreteValues, entryConcreteValue)
}
}
concreteValue = concreteValues
}
return format(c.Stdout, concreteValue)
}