-
Notifications
You must be signed in to change notification settings - Fork 90
/
diff.go
206 lines (178 loc) · 5.77 KB
/
diff.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package kustomize
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/marccampbell/yaml-toolbox/pkg/splitter"
"github.com/pkg/errors"
"github.com/sergi/go-diff/diffmatchpatch"
)
type Diff struct {
FilesChanged int `json:"filesChanged"`
LinesAdded int `json:"linesAdded"`
LinesRemoved int `json:"linesRemoved"`
}
func diffContent(baseContent string, updatedContent string) (int, int, error) {
dmp := diffmatchpatch.New()
charsA, charsB, lines := dmp.DiffLinesToChars(baseContent, updatedContent)
diffs := dmp.DiffMain(charsA, charsB, false)
diffs = dmp.DiffCharsToLines(diffs, lines)
additions := 0
deletions := 0
for _, diff := range diffs {
scanner := bufio.NewScanner(strings.NewReader(diff.Text))
for scanner.Scan() {
if diff.Type == diffmatchpatch.DiffDelete {
deletions++
} else if diff.Type == diffmatchpatch.DiffInsert {
additions++
}
}
}
return additions, deletions, nil
}
// DiffAppVersionsForDownstream will generate a diff of the rendered yaml between two different
// archivedirs
func DiffAppVersionsForDownstream(downstreamName string, archive string, diffBasePath string, kustomizeVersion string) (*Diff, error) {
// kustomize build both of these archives before diffing
archiveOutput, err := exec.Command(GetKustomizePath(kustomizeVersion), "build", filepath.Join(archive, "overlays", "downstreams", downstreamName)).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("kustomize stderr: %q", string(ee.Stderr))
}
return nil, errors.Wrap(err, "failed to run kustomize on archive dir")
}
baseOutput, err := exec.Command(GetKustomizePath(kustomizeVersion), "build", filepath.Join(diffBasePath, "overlays", "downstreams", downstreamName)).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("kustomize stderr: %q", string(ee.Stderr))
}
return nil, errors.Wrap(err, "failed to run kustomize on base dir")
}
archiveFiles, err := splitter.SplitYAML(archiveOutput)
if err != nil {
return nil, errors.Wrap(err, "failed to split archive yaml")
}
baseFiles, err := splitter.SplitYAML(baseOutput)
if err != nil {
return nil, errors.Wrap(err, "failed to split base yaml")
}
diff := Diff{}
for archiveFilename, archiveContents := range archiveFiles {
baseContents, ok := baseFiles[archiveFilename]
if !ok {
// this file was added
scanner := bufio.NewScanner(bytes.NewReader(archiveContents))
for scanner.Scan() {
diff.LinesAdded++
}
diff.FilesChanged++
continue
}
linesAdded, linesRemoved, err := diffContent(string(baseContents), string(archiveContents))
if err != nil {
return nil, errors.Wrapf(err, "failed to diff base and archive contents %s", archiveFilename)
}
diff.LinesAdded += linesAdded
diff.LinesRemoved += linesRemoved
if linesAdded > 0 || linesRemoved > 0 {
diff.FilesChanged++
}
}
for baseFilename, baseContents := range baseFiles {
_, ok := archiveFiles[baseFilename]
if !ok {
// this file was removed
scanner := bufio.NewScanner(bytes.NewReader(baseContents))
for scanner.Scan() {
diff.LinesRemoved++
}
diff.FilesChanged++
}
}
archiveDir := filepath.Join(archive, "overlays", "downstreams", downstreamName)
archiveChartFiles, err := getKustomizedFiles(archiveDir, kustomizeVersion)
if err != nil {
return nil, errors.Wrap(err, "failed to kustomize archive charts dir")
}
baseDir := filepath.Join(diffBasePath, "overlays", "downstreams", downstreamName)
baseChartFiles, err := getKustomizedFiles(baseDir, kustomizeVersion)
if err != nil {
return nil, errors.Wrap(err, "failed to kustomize base charts dir")
}
for archiveFilename, archiveContents := range archiveChartFiles {
baseContents, ok := baseChartFiles[archiveFilename]
if !ok {
// this file was added
scanner := bufio.NewScanner(bytes.NewReader(archiveContents))
for scanner.Scan() {
diff.LinesAdded++
}
diff.FilesChanged++
continue
}
linesAdded, linesRemoved, err := diffContent(string(baseContents), string(archiveContents))
if err != nil {
return nil, errors.Wrapf(err, "failed to diff base and archive chart contents %s", archiveFilename)
}
diff.LinesAdded += linesAdded
diff.LinesRemoved += linesRemoved
if linesAdded > 0 || linesRemoved > 0 {
diff.FilesChanged++
}
}
for baseFilename, baseContents := range baseChartFiles {
_, ok := archiveChartFiles[baseFilename]
if !ok {
// this file was removed
scanner := bufio.NewScanner(bytes.NewReader(baseContents))
for scanner.Scan() {
diff.LinesRemoved++
}
diff.FilesChanged++
}
}
return &diff, nil
}
func getKustomizedFiles(kustomizeTarget string, version string) (map[string][]byte, error) {
kustomizedFilesList := map[string][]byte{}
archiveChartDir := filepath.Join(kustomizeTarget, "charts")
_, err := os.Stat(archiveChartDir)
if err != nil {
if os.IsNotExist(err) {
return kustomizedFilesList, nil
}
return kustomizedFilesList, err
}
err = filepath.Walk(archiveChartDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == "kustomization.yaml" {
archiveOutput, err := exec.Command(GetKustomizePath(version), "build", filepath.Dir(path)).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("kustomize %s: %q", path, string(ee.Stderr))
}
return errors.Wrapf(err, "failed to kustomize %s", path)
}
archiveFiles, err := splitter.SplitYAML(archiveOutput)
if err != nil {
return errors.Wrapf(err, "failed to split yaml result for %s", path)
}
for filename, d := range archiveFiles {
kustomizedFilesList[filename] = d
}
}
return nil
})
if err != nil {
return kustomizedFilesList, err
}
return kustomizedFilesList, nil
}