forked from 1Password/shell-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_report.go
126 lines (103 loc) · 2.8 KB
/
validation_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
package plugintest
import (
"fmt"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/fatih/color"
)
func PrintValidationReport(plugin schema.Plugin) {
reports := plugin.DeepValidate()
printer := &ValidationReportPrinter{
Reports: reports,
Format: PrintFormat{}.ValidationReportFormat(),
}
printer.Print()
}
func PrintReportIfErrors(plugin schema.Plugin) (hasErrors bool) {
pluginReports := plugin.DeepValidate()
for _, report := range pluginReports {
if report.HasErrors() {
printer := &ValidationReportPrinter{
Format: PrintFormat{}.ValidationReportFormat(),
Reports: pluginReports,
}
printer.Print()
return true
}
}
return false
}
type PrintFormat struct {
Heading *color.Color
Warning *color.Color
Error *color.Color
Success *color.Color
}
func (pf PrintFormat) ValidationReportFormat() PrintFormat {
heading := color.New(color.FgCyan, color.Bold)
warning := color.New(color.FgYellow)
err := color.New(color.FgRed)
success := color.New(color.FgGreen)
return PrintFormat{
Heading: heading,
Warning: warning,
Error: err,
Success: success,
}
}
type ValidationReportPrinter struct {
Reports []schema.ValidationReport
Format PrintFormat
}
func (p *ValidationReportPrinter) Print() {
if p.Reports == nil || len(p.Reports) == 0 {
color.Cyan("No reports to print")
return
}
for _, report := range p.Reports {
p.PrintReport(report)
}
}
func (p *ValidationReportPrinter) PrintReport(report schema.ValidationReport) {
p.printHeading(report.Heading)
p.printChecks(report.Checks)
}
// sortChecks in the order ["success", "warning", "error"]
func (p *ValidationReportPrinter) sortChecks(checks []schema.ValidationCheck) []schema.ValidationCheck {
var successChecks []schema.ValidationCheck
var warningChecks []schema.ValidationCheck
var errorChecks []schema.ValidationCheck
for _, c := range checks {
if c.Assertion {
successChecks = append(successChecks, c)
continue
}
if c.Severity == schema.ValidationSeverityWarning {
warningChecks = append(warningChecks, c)
continue
}
errorChecks = append(errorChecks, c)
}
result := append(successChecks, warningChecks...)
result = append(result, errorChecks...)
return result
}
func (p *ValidationReportPrinter) printChecks(checks []schema.ValidationCheck) {
for _, c := range p.sortChecks(checks) {
p.printCheck(c)
}
fmt.Println()
}
func (p *ValidationReportPrinter) printHeading(heading string) {
p.Format.Heading.Printf("# %s\n\n", heading)
}
func (p *ValidationReportPrinter) printCheck(check schema.ValidationCheck) {
if check.Assertion {
p.Format.Success.Printf("✔ %s\n", check.Description)
return
}
if check.Severity == schema.ValidationSeverityWarning {
p.Format.Warning.Printf("⚠ %s\n", check.Description)
return
}
p.Format.Error.Printf("✘ %s\n", check.Description)
}