/
stdout_reporter.go
163 lines (148 loc) · 4.9 KB
/
stdout_reporter.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
package reporter
import (
"fmt"
"strings"
"github.com/fatih/color"
)
type StdoutReporter struct{}
// Print implements the Reporter interface by outputting
// the report content to stdout
func (sr StdoutReporter) Print(reports []Report) error {
var results string
var successCount = 0
var failureCount = 0
for _, report := range reports {
if !report.IsValid {
color.Set(color.FgRed)
tmp := fmt.Sprintln(" × " + report.FilePath)
paddedString := sr.padErrorString(report.ValidationError.Error())
tmp += fmt.Sprintf(" error: %v\n", paddedString)
fmt.Printf(tmp)
color.Unset()
results += tmp
failureCount = failureCount + 1
} else {
tmp := fmt.Sprintln(" ✓ " + report.FilePath)
color.Green(tmp)
results += tmp
successCount = successCount + 1
}
}
fmt.Printf("Summary: %d succeeded, %d failed\n", successCount, failureCount)
return nil
}
// There is repeated code in the following two functions. Trying to consolidate
// the code into one function is difficult because of the output format
func PrintSingleGroupStdout(groupReport map[string][]Report) error {
var successCount = 0
var failureCount = 0
var totalSuccessCount = 0
var totalFailureCount = 0
sr := StdoutReporter{}
for group, reports := range groupReport {
fmt.Printf("%s\n", group)
successCount = 0
failureCount = 0
for _, report := range reports {
if !report.IsValid {
color.Set(color.FgRed)
fmt.Println(" × " + report.FilePath)
paddedString := sr.padErrorString(report.ValidationError.Error())
fmt.Printf(" error: %v\n", paddedString)
color.Unset()
failureCount = failureCount + 1
totalFailureCount = totalFailureCount + 1
} else {
color.Green(" ✓ " + report.FilePath)
successCount = successCount + 1
totalSuccessCount = totalSuccessCount + 1
}
}
fmt.Printf("Summary: %d succeeded, %d failed\n\n", successCount, failureCount)
}
fmt.Printf("Total Summary: %d succeeded, %d failed\n", totalSuccessCount, totalFailureCount)
return nil
}
// Prints the report for when two groups are passed in the groupby flag
func PrintDoubleGroupStdout(groupReport map[string]map[string][]Report) error {
var successCount = 0
var failureCount = 0
var totalSuccessCount = 0
var totalFailureCount = 0
sr := StdoutReporter{}
for group, reports := range groupReport {
fmt.Printf("%s\n", group)
for group2, reports2 := range reports {
fmt.Printf(" %s\n", group2)
successCount = 0
failureCount = 0
for _, report := range reports2 {
if !report.IsValid {
color.Set(color.FgRed)
fmt.Println(" × " + report.FilePath)
paddedString := sr.padErrorString(report.ValidationError.Error())
fmt.Printf(" error: %v\n", paddedString)
color.Unset()
failureCount = failureCount + 1
totalFailureCount = totalFailureCount + 1
} else {
color.Green(" ✓ " + report.FilePath)
successCount = successCount + 1
totalSuccessCount = totalSuccessCount + 1
}
}
fmt.Printf(" Summary: %d succeeded, %d failed\n\n", successCount, failureCount)
}
}
fmt.Printf("Total Summary: %d succeeded, %d failed\n", totalSuccessCount, totalFailureCount)
return nil
}
// Prints the report for when three groups are passed in the groupby flag
func PrintTripleGroupStdout(groupReport map[string]map[string]map[string][]Report) error {
var successCount = 0
var failureCount = 0
var totalSuccessCount = 0
var totalFailureCount = 0
sr := StdoutReporter{}
for groupOne, header := range groupReport {
fmt.Printf("%s\n", groupOne)
for groupTwo, subheader := range header {
fmt.Printf(" %s\n", groupTwo)
for groupThree, reports := range subheader {
fmt.Printf(" %s\n", groupThree)
successCount = 0
failureCount = 0
for _, report := range reports {
if !report.IsValid {
color.Set(color.FgRed)
fmt.Println(" × " + report.FilePath)
paddedString := sr.padErrorString(report.ValidationError.Error())
fmt.Printf(" error: %v\n", paddedString)
color.Unset()
failureCount = failureCount + 1
totalFailureCount = totalFailureCount + 1
} else {
color.Green(" ✓ " + report.FilePath)
successCount = successCount + 1
totalSuccessCount = totalSuccessCount + 1
}
}
fmt.Printf(" Summary: %d succeeded, %d failed\n\n", successCount, failureCount)
}
}
}
fmt.Printf("Total Summary: %d succeeded, %d failed\n", totalSuccessCount, totalFailureCount)
return nil
}
// padErrorString adds padding to every newline in the error
// string, except the first line and removes any trailing newlines
// or spaces
func (sr StdoutReporter) padErrorString(errS string) string {
errS = strings.TrimSpace(errS)
lines := strings.Split(errS, "\n")
for idx := 1; idx < len(lines); idx++ {
lines[idx] = " " + lines[idx]
}
paddedErr := strings.Join(lines, "\n")
return paddedErr
}