-
Notifications
You must be signed in to change notification settings - Fork 141
/
result_printer.go
54 lines (45 loc) · 1.7 KB
/
result_printer.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
package utils
import (
"fmt"
"io"
"github.com/zalando/zally/cli/zally/domain"
"github.com/zalando/zally/cli/zally/utils/formatters"
)
// ResultPrinter helps to print results to the CLI
type ResultPrinter struct {
buffer io.Writer
formatter formatters.Formatter
}
// NewResultPrinter creates an instance of ResultPrinter
func NewResultPrinter(buffer io.Writer, formatter formatters.Formatter) ResultPrinter {
var resultPrinter ResultPrinter
resultPrinter.buffer = buffer
resultPrinter.formatter = formatter
return resultPrinter
}
// PrintViolations creates string representation of Violation
func (r *ResultPrinter) PrintViolations(violations *domain.Violations) {
if len(violations.Violations) > 0 {
fmt.Fprint(r.buffer, r.formatter.FormatViolations("MUST", violations.Must()))
fmt.Fprint(r.buffer, r.formatter.FormatViolations("SHOULD", violations.Should()))
fmt.Fprint(r.buffer, r.formatter.FormatViolations("MAY", violations.May()))
fmt.Fprint(r.buffer, r.formatter.FormatViolations("HINT", violations.Hint()))
fmt.Fprint(r.buffer, r.formatter.FormatViolationsCount(&violations.ViolationsCount))
}
fmt.Fprint(r.buffer, r.formatter.FormatServerMessage(violations.Message))
if violations.Message == "" && len(violations.Violations) == 0 {
fmt.Fprint(r.buffer, r.formatter.FormatMessage("Congratulations! No violations found."))
}
}
// PrintRules prints a list of supported rules
func (r *ResultPrinter) PrintRules(rules *domain.Rules) {
r.printRules(rules.Must())
r.printRules(rules.Should())
r.printRules(rules.May())
r.printRules(rules.Hint())
}
func (r *ResultPrinter) printRules(rules []domain.Rule) {
for _, rule := range rules {
fmt.Fprint(r.buffer, r.formatter.FormatRule(&rule))
}
}