-
Notifications
You must be signed in to change notification settings - Fork 141
/
markdown_formatter.go
90 lines (76 loc) · 2.42 KB
/
markdown_formatter.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
package formatters
import (
"bytes"
"fmt"
"github.com/zalando/zally/cli/zally/domain"
)
// MarkdownFormatter is used to generate output in markdown format
type MarkdownFormatter struct {
}
// FormatViolations formats the list of the violations
func (f *MarkdownFormatter) FormatViolations(header string, violations []domain.Violation) string {
var buffer bytes.Buffer
if len(violations) > 0 {
fmt.Fprint(&buffer, f.formatHeader(header))
for _, violation := range violations {
fmt.Fprint(&buffer, f.formatViolation(&violation))
}
}
return buffer.String()
}
// FormatViolationsCount generates violation counters in in markdown format
func (f *MarkdownFormatter) FormatViolationsCount(violationsCount *domain.ViolationsCount) string {
var buffer bytes.Buffer
fmt.Fprint(&buffer, f.formatHeader("Summary:"))
fmt.Fprintf(&buffer, "- MUST violations: %d\n", violationsCount.Must)
fmt.Fprintf(&buffer, "- SHOULD violations: %d\n", violationsCount.Should)
fmt.Fprintf(&buffer, "- MAY violations: %d\n", violationsCount.May)
fmt.Fprintf(&buffer, "- HINT violations: %d\n", violationsCount.Hint)
return buffer.String()
}
// FormatRule formats rule description
func (f *MarkdownFormatter) FormatRule(rule *domain.Rule) string {
var buffer bytes.Buffer
fmt.Fprintf(
&buffer,
"- `[%s %s]` [%s](%s)\n",
rule.Code,
rule.Type,
rule.Title,
rule.URL)
return buffer.String()
}
// FormatMessage formats message
func (f *MarkdownFormatter) FormatMessage(message string) string {
if message != "" {
return fmt.Sprintf("\n%s\n\n", message)
}
return ""
}
// FormatServerMessage formats server message
func (f *MarkdownFormatter) FormatServerMessage(message string) string {
if message != "" {
return fmt.Sprintf("\n\n%s%s\n\n\n", f.formatHeader("Server message:"), message)
}
return ""
}
func (f *MarkdownFormatter) formatViolation(violation *domain.Violation) string {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "### `%s` [%s](%s)\n\n", violation.ViolationType, violation.Title, violation.RuleLink)
fmt.Fprintf(&buffer, "> %s\n\n", violation.Decription)
if violation.Pointer != "" {
fmt.Fprintf(&buffer, "- %s\n", violation.Pointer)
} else {
for _, path := range violation.Paths {
fmt.Fprintf(&buffer, "- %s\n", path)
}
}
fmt.Fprintf(&buffer, "\n")
return buffer.String()
}
func (f *MarkdownFormatter) formatHeader(header string) string {
if len(header) == 0 {
return ""
}
return fmt.Sprintf("## %s\n\n", header)
}