forked from nilslice/protolock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.go
39 lines (34 loc) · 879 Bytes
/
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
package protolock
import (
"fmt"
"io"
"sort"
)
// HandleReport checks a report for warnigs and writes warnings to an io.Writer.
// The returned int (an exit code) is 1 if warnings are encountered.
func HandleReport(report *Report, w io.Writer, err error) (int, error) {
if len(report.Warnings) > 0 {
// sort the warnings so they are grouped by file location
orderByPathAndMessage(report.Warnings)
for _, warning := range report.Warnings {
fmt.Fprintf(
w,
"CONFLICT: %s [%s]\n",
warning.Message, warning.Filepath,
)
}
return 1, err
}
return 0, err
}
func orderByPathAndMessage(warnings []Warning) {
sort.Slice(warnings, func(i, j int) bool {
if warnings[i].Filepath < warnings[j].Filepath {
return true
}
if warnings[i].Filepath > warnings[j].Filepath {
return false
}
return warnings[i].Message < warnings[j].Message
})
}