-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_folder.go
107 lines (89 loc) · 2.75 KB
/
merge_folder.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cmd
import (
"fmt"
"github.com/davidalpert/go-printers/v1"
"github.com/davidalpert/go-yoss/internal/app"
"github.com/davidalpert/go-yoss/internal/cfgset"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"os"
"path"
"strings"
)
type MergeFolderOptions struct {
*printers.PrinterOptions
cfgset.MergeOptions
OutDir string
}
func NewMergeFolderOptions(ioStreams printers.IOStreams) *MergeFolderOptions {
return &MergeFolderOptions{
PrinterOptions: printers.NewPrinterOptions().WithStreams(ioStreams).WithDefaultOutput("yaml"),
}
}
func NewCmdMergeFolder(ioStreams printers.IOStreams) *cobra.Command {
o := NewMergeFolderOptions(ioStreams)
var cmd = &cobra.Command{
Use: "folder <source_folder>",
Short: "merge two config files together",
Aliases: []string{"f", "fs"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(cmd, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
return o.Run()
},
}
o.PrinterOptions.AddPrinterFlags(cmd.Flags())
cmd.Flags().BoolVarP(&o.Debug, "debug", "d", false, "enable debug output")
cmd.Flags().StringVar(&o.OutDir, "out-dir", "out", "folder to place output")
return cmd
}
// Complete the options
func (o *MergeFolderOptions) Complete(cmd *cobra.Command, args []string) error {
o.SourceFolder = args[0]
return nil
}
// Validate the options
func (o *MergeFolderOptions) Validate() error {
if strings.EqualFold(o.SourceFolder, o.OutDir) {
return fmt.Errorf("cannot merge into the source folder '%s'; please specify a different --out-dir", o.SourceFolder)
}
return o.PrinterOptions.Validate()
}
// Run the command
func (o *MergeFolderOptions) Run() error {
result, err := cfgset.Merge(o.MergeOptions)
if err != nil {
return err
}
if err = app.Fs.MkdirAll(o.OutDir, os.ModePerm); err != nil {
return fmt.Errorf("making %#v: %#v", o.OutDir, err)
}
outputExt := o.FormatCategory()
if outputExt == "text" {
outputExt = "txt"
}
for _, appResult := range result {
appOutDir := path.Join(o.OutDir, appResult.AppDir)
if err = app.Fs.MkdirAll(appOutDir, os.ModePerm); err != nil {
return fmt.Errorf("making %#v: %#v", appOutDir, err)
}
for slug, mergeResult := range appResult.MergeBySlug {
outFile := path.Join(appOutDir, fmt.Sprintf("%s.%s", slug, outputExt))
b, err := yaml.Marshal(mergeResult)
if err != nil {
return fmt.Errorf("marshalling %#v to %#v: %#v", mergeResult, outFile, err)
}
if err = afero.WriteFile(app.Fs, outFile, b, os.ModePerm); err != nil {
return fmt.Errorf("writing %#v: %#v", outFile, err)
}
// TODO: collect errors into an error result rather than failing out on the first one and write to STDERR
}
}
return nil
}