-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathreport.go
More file actions
327 lines (292 loc) · 9.34 KB
/
report.go
File metadata and controls
327 lines (292 loc) · 9.34 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package diff
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"reflect"
"regexp"
"text/template"
"github.com/aryann/difflib"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
"github.com/mgutz/ansi"
)
// Report to store report data and format
type Report struct {
format ReportFormat
Entries []ReportEntry
mode string
}
// ReportEntry to store changes between releases
type ReportEntry struct {
Key string
SuppressedKinds []string
Kind string
Context int
Diffs []difflib.DiffRecord
ChangeType string
Structured *StructuredEntry
}
// ReportFormat to the context to make a changes report
type ReportFormat struct {
output func(r *Report, to io.Writer)
changestyles map[string]ChangeStyle
}
// ChangeStyle for styling the report
type ChangeStyle struct {
color string
message string
}
// ReportTemplateSpec for common template spec
type ReportTemplateSpec struct {
Namespace string
Name string
Kind string
API string
Change string
}
// setupReportFormat: process output argument.
func (r *Report) setupReportFormat(format string) {
r.mode = format
switch format {
case "simple":
setupSimpleReport(r)
case "template":
setupTemplateReport(r)
case "json":
setupJSONReport(r)
case "structured":
setupStructuredReport(r)
case "dyff":
setupDyffReport(r)
default:
setupDiffReport(r)
}
}
func setupDyffReport(r *Report) {
r.format.output = printDyffReport
}
func printDyffReport(r *Report, to io.Writer) {
currentFile, _ := os.CreateTemp("", "existing-values")
defer func() {
_ = os.Remove(currentFile.Name())
}()
newFile, _ := os.CreateTemp("", "new-values")
defer func() {
_ = os.Remove(newFile.Name())
}()
context := -1
for _, entry := range r.Entries {
context = entry.Context
_, _ = currentFile.WriteString("---\n")
_, _ = newFile.WriteString("---\n")
for _, record := range entry.Diffs {
switch record.Delta {
case difflib.Common:
_, _ = currentFile.WriteString(record.Payload + "\n")
_, _ = newFile.WriteString(record.Payload + "\n")
case difflib.LeftOnly:
_, _ = currentFile.WriteString(record.Payload + "\n")
case difflib.RightOnly:
_, _ = newFile.WriteString(record.Payload + "\n")
}
}
}
_ = currentFile.Close()
_ = newFile.Close()
currentInputFile, newInputFile, _ := ytbx.LoadFiles(currentFile.Name(), newFile.Name())
report, _ := dyff.CompareInputFiles(currentInputFile, newInputFile)
reportWriter := &dyff.HumanReport{
Report: report,
OmitHeader: true,
MinorChangeThreshold: 0.1,
}
if context != -1 {
reportWriter.MultilineContextLines = context
}
_ = reportWriter.WriteReport(to)
}
// addEntry: stores diff changes.
func (r *Report) addEntry(key string, suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, changeType string, structured *StructuredEntry) {
entry := ReportEntry{
key,
suppressedKinds,
kind,
context,
diffs,
changeType,
structured,
}
r.Entries = append(r.Entries, entry)
}
// print: prints entries added to the report.
func (r *Report) print(to io.Writer) {
r.format.output(r, to)
}
// clean: needed for testing
func (r *Report) clean() {
r.Entries = nil
}
// setup report for default output: diff
func setupDiffReport(r *Report) {
r.format.output = printDiffReport
r.format.changestyles = make(map[string]ChangeStyle)
r.format.changestyles["ADD"] = ChangeStyle{color: "green", message: "has been added:"}
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "has been removed:"}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "has changed:"}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "changed ownership:"}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
}
// print report for default output: diff
func printDiffReport(r *Report, to io.Writer) {
for _, entry := range r.Entries {
_, _ = fmt.Fprintf(
to,
ansi.Color("%s %s", r.format.changestyles[entry.ChangeType].color)+"\n",
entry.Key,
r.format.changestyles[entry.ChangeType].message,
)
printDiffRecords(entry.SuppressedKinds, entry.Kind, entry.Context, entry.Diffs, to)
}
}
// setup report for simple output.
func setupSimpleReport(r *Report) {
r.format.output = printSimpleReport
r.format.changestyles = make(map[string]ChangeStyle)
r.format.changestyles["ADD"] = ChangeStyle{color: "green", message: "to be added."}
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "to be removed."}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "to be changed."}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "to change ownership."}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
}
// print report for simple output
func printSimpleReport(r *Report, to io.Writer) {
summary := map[string]int{
"ADD": 0,
"REMOVE": 0,
"MODIFY": 0,
"OWNERSHIP": 0,
"MODIFY_SUPPRESSED": 0,
}
for _, entry := range r.Entries {
_, _ = fmt.Fprintf(to, ansi.Color("%s %s", r.format.changestyles[entry.ChangeType].color)+"\n",
entry.Key,
r.format.changestyles[entry.ChangeType].message,
)
summary[entry.ChangeType]++
}
_, _ = fmt.Fprintf(to, "Plan: %d to add, %d to change, %d to destroy, %d to change ownership.\n", summary["ADD"], summary["MODIFY"], summary["REMOVE"], summary["OWNERSHIP"])
}
func newTemplate(name string) *template.Template {
// Prepare template functions
funcsMap := template.FuncMap{
"last": func(x int, a interface{}) bool {
return x == reflect.ValueOf(a).Len()-1
},
}
return template.New(name).Funcs(funcsMap)
}
// setup report for json output
func setupJSONReport(r *Report) {
t, err := newTemplate("entries").Parse(defaultTemplateReport)
if err != nil {
log.Fatalf("Error loading default template: %v", err)
}
r.format.output = templateReportPrinter(t)
r.format.changestyles = make(map[string]ChangeStyle)
r.format.changestyles["ADD"] = ChangeStyle{color: "green", message: ""}
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: ""}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: ""}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: ""}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: ""}
}
// setup report for template output
func setupTemplateReport(r *Report) {
var tpl *template.Template
{
tplFile, present := os.LookupEnv("HELM_DIFF_TPL")
if present {
t, err := newTemplate(filepath.Base(tplFile)).ParseFiles(tplFile)
if err != nil {
fmt.Println(err)
log.Fatalf("Error loading custom template")
}
tpl = t
} else {
// Render
t, err := newTemplate("entries").Parse(defaultTemplateReport)
if err != nil {
log.Fatalf("Error loading default template")
}
tpl = t
}
}
r.format.output = templateReportPrinter(tpl)
r.format.changestyles = make(map[string]ChangeStyle)
r.format.changestyles["ADD"] = ChangeStyle{color: "green", message: ""}
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: ""}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: ""}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: ""}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: ""}
}
func setupStructuredReport(r *Report) {
r.format.output = printStructuredReport
}
func printStructuredReport(r *Report, to io.Writer) {
entries := make([]StructuredEntry, 0, len(r.Entries))
for _, entry := range r.Entries {
if entry.Structured != nil {
structuredCopy := *entry.Structured
if structuredCopy.ChangeType == "" {
structuredCopy.ChangeType = entry.ChangeType
}
entries = append(entries, structuredCopy)
continue
}
entries = append(entries, StructuredEntry{
Name: entry.Key,
ChangeType: entry.ChangeType,
})
}
encoder := json.NewEncoder(to)
encoder.SetIndent("", " ")
if err := encoder.Encode(entries); err != nil {
log.Printf("Error encoding structured diff output: %v\n", err)
}
}
// report with template output will only have access to ReportTemplateSpec.
// This function reverts parsedMetadata.String()
func (t *ReportTemplateSpec) loadFromKey(key string) error {
pattern := regexp.MustCompile(`(?P<namespace>[a-z0-9-]+), (?P<name>[a-z0-9.-]+), (?P<kind>\w+) \((?P<api>[^)]+)\)`)
matches := pattern.FindStringSubmatch(key)
if len(matches) > 1 {
t.Namespace = matches[1]
t.Name = matches[2]
t.Kind = matches[3]
t.API = matches[4]
return nil
}
return errors.New("key string didn't match regexp")
}
// load and print report for template output
func templateReportPrinter(t *template.Template) func(r *Report, to io.Writer) {
return func(r *Report, to io.Writer) {
var templateDataArray []ReportTemplateSpec
for _, entry := range r.Entries {
templateData := ReportTemplateSpec{}
err := templateData.loadFromKey(entry.Key)
if err != nil {
log.Println("error processing report entry")
} else {
templateData.Change = entry.ChangeType
templateDataArray = append(templateDataArray, templateData)
}
}
_ = t.Execute(to, templateDataArray)
_, _ = to.Write([]byte("\n"))
}
}