This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathspiff.go
154 lines (125 loc) · 3.52 KB
/
spiff.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/codegangsta/cli"
"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/cloudfoundry-incubator/spiff/compare"
"github.com/cloudfoundry-incubator/spiff/flow"
"github.com/cloudfoundry-incubator/spiff/yaml"
)
func main() {
app := cli.NewApp()
app.Name = "spiff"
app.Usage = "BOSH deployment manifest toolkit"
app.Version = "1.0.8"
app.Commands = []cli.Command{
{
Name: "merge",
ShortName: "m",
Usage: "merge stub files into a manifest template",
Action: func(c *cli.Context) {
if len(c.Args()) < 1 {
cli.ShowCommandHelp(c, "merge")
os.Exit(1)
}
merge(c.Args()[0], c.Args()[1:])
},
},
{
Name: "diff",
ShortName: "d",
Usage: "structurally compare two YAML files",
Flags: []cli.Flag{
cli.StringFlag{
Name: "separator",
Usage: "separator to print between diffs",
},
},
Action: func(c *cli.Context) {
if len(c.Args()) < 2 {
cli.ShowCommandHelp(c, "diff")
os.Exit(1)
}
diff(c.Args()[0], c.Args()[1], c.String("separator"))
},
},
}
app.Run(os.Args)
}
func merge(templateFilePath string, stubFilePaths []string) {
templateFile, err := ioutil.ReadFile(templateFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading template [%s]:", path.Clean(templateFilePath)), err)
}
templateYAML, err := yaml.Parse(templateFilePath, templateFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing template [%s]:", path.Clean(templateFilePath)), err)
}
stubs := []yaml.Node{}
for _, stubFilePath := range stubFilePaths {
stubFile, err := ioutil.ReadFile(stubFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading stub [%s]:", path.Clean(stubFilePath)), err)
}
stubYAML, err := yaml.Parse(stubFilePath, stubFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing stub [%s]:", path.Clean(stubFilePath)), err)
}
stubs = append(stubs, stubYAML)
}
flowed, err := flow.Cascade(templateYAML, stubs...)
if err != nil {
log.Fatalln("error generating manifest:", err)
}
yaml, err := candiedyaml.Marshal(flowed)
if err != nil {
log.Fatalln("error marshalling manifest:", err)
}
fmt.Println(string(yaml))
}
func diff(aFilePath, bFilePath string, separator string) {
aFile, err := ioutil.ReadFile(aFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading a [%s]:", path.Clean(aFilePath)), err)
}
aYAML, err := yaml.Parse(aFilePath, aFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing a [%s]:", path.Clean(aFilePath)), err)
}
bFile, err := ioutil.ReadFile(bFilePath)
if err != nil {
log.Fatalln(fmt.Sprintf("error reading b [%s]:", path.Clean(bFilePath)), err)
}
bYAML, err := yaml.Parse(bFilePath, bFile)
if err != nil {
log.Fatalln(fmt.Sprintf("error parsing b [%s]:", path.Clean(bFilePath)), err)
}
diffs := compare.Compare(aYAML, bYAML)
if len(diffs) == 0 {
fmt.Println("no differences!")
return
}
for _, diff := range diffs {
fmt.Println("Difference in", strings.Join(diff.Path, "."))
if diff.A != nil {
ayaml, err := candiedyaml.Marshal(diff.A)
if err != nil {
panic(err)
}
fmt.Printf(" %s has:\n \x1b[31m%s\x1b[0m\n", aFilePath, strings.Replace(string(ayaml), "\n", "\n ", -1))
}
if diff.B != nil {
byaml, err := candiedyaml.Marshal(diff.B)
if err != nil {
panic(err)
}
fmt.Printf(" %s has:\n \x1b[32m%s\x1b[0m\n", bFilePath, strings.Replace(string(byaml), "\n", "\n ", -1))
}
fmt.Printf(separator)
}
}