-
Notifications
You must be signed in to change notification settings - Fork 53
/
console.go
112 lines (97 loc) · 2.47 KB
/
console.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
package reporter
import (
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"github.com/cloudflare/pint/internal/checks"
"github.com/cloudflare/pint/internal/output"
)
func NewConsoleReporter(output io.Writer) ConsoleReporter {
return ConsoleReporter{output: output}
}
type ConsoleReporter struct {
output io.Writer
}
func (cr ConsoleReporter) Submit(summary Summary) error {
reports := summary.Reports
reps := reports[:]
sort.Slice(reps, func(i, j int) bool {
if reports[i].Path < reports[j].Path {
return true
}
if reports[i].Path > reports[j].Path {
return false
}
if reports[i].Problem.Lines[0] < reports[j].Problem.Lines[0] {
return true
}
if reports[i].Problem.Lines[0] > reports[j].Problem.Lines[0] {
return false
}
if reports[i].Problem.Reporter < reports[j].Problem.Reporter {
return true
}
if reports[i].Problem.Reporter > reports[j].Problem.Reporter {
return false
}
return reports[i].Problem.Text < reports[j].Problem.Text
})
perFile := map[string][]string{}
for _, report := range reps {
if _, ok := perFile[report.Path]; !ok {
perFile[report.Path] = []string{}
}
content, err := readFile(report.Path)
if err != nil {
return err
}
msg := []string{}
firstLine, lastLine := report.Problem.LineRange()
msg = append(msg, output.MakeBlue("%s:%s: ", report.Path, printLineRange(firstLine, lastLine)))
switch report.Problem.Severity {
case checks.Bug, checks.Fatal:
msg = append(msg, output.MakeRed(report.Problem.Text))
case checks.Warning:
msg = append(msg, output.MakeYellow(report.Problem.Text))
default:
msg = append(msg, output.MakeGray(report.Problem.Text))
}
msg = append(msg, output.MakeMagneta(" (%s)\n", report.Problem.Reporter))
for _, c := range strings.Split(content, "\n")[firstLine-1 : lastLine] {
msg = append(msg, output.MakeWhite("%s\n", c))
}
perFile[report.Path] = append(perFile[report.Path], strings.Join(msg, ""))
}
paths := []string{}
for path := range perFile {
paths = append(paths, path)
}
sort.Strings(paths)
for _, path := range paths {
msgs := perFile[path]
for _, msg := range msgs {
fmt.Fprintln(cr.output, msg)
}
}
return nil
}
func readFile(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
content, err := io.ReadAll(f)
if err != nil {
return "", err
}
return string(content), nil
}
func printLineRange(s, e int) string {
if s == e {
return strconv.Itoa(s)
}
return fmt.Sprintf("%d-%d", s, e)
}