forked from databus23/helm-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
276 lines (245 loc) · 7.79 KB
/
report.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
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
package diff
import (
"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
}
// ReportEntry to store changes between releases
type ReportEntry struct {
key string
suppressedKinds []string
kind string
context int
diffs []difflib.DiffRecord
changeType string
}
// 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) {
switch format {
case "simple":
setupSimpleReport(r)
case "template":
setupTemplateReport(r)
case "json":
setupJSONReport(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())
}()
for _, entry := range r.entries {
_, _ = 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,
}
_ = reportWriter.WriteReport(to)
}
// addEntry: stores diff changes.
func (r *Report) addEntry(key string, suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, changeType string) {
entry := ReportEntry{
key,
suppressedKinds,
kind,
context,
diffs,
changeType,
}
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:"}
}
// 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", "yellow")+"\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."}
}
// print report for simple output
func printSimpleReport(r *Report, to io.Writer) {
var summary = map[string]int{
"ADD": 0,
"REMOVE": 0,
"MODIFY": 0,
"OWNERSHIP": 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
var 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: ""}
}
// 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: ""}
}
// 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"))
}
}