-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
104 lines (89 loc) · 2.79 KB
/
output.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
// Copyright (C) 2024 Eric Cornelissen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/ericcornelissen/ades"
)
type jsonOutput struct {
Violations []jsonViolation `json:"problems"`
}
type jsonViolation struct {
Target string `json:"target"`
File string `json:"file"`
Job string `json:"job"`
Step string `json:"step"`
Problem string `json:"problem"`
}
func printJson(rawViolations map[string]map[string][]ades.Violation) string {
violations := make([]jsonViolation, 0)
for target, targetViolations := range rawViolations {
for file, fileViolations := range targetViolations {
for _, fileViolation := range fileViolations {
violations = append(violations, jsonViolation{
Target: target,
File: file,
Job: fileViolation.JobId,
Step: fileViolation.StepId,
Problem: fileViolation.Problem,
})
}
}
}
sort.Slice(violations, func(i, j int) bool {
return violations[i].File < violations[j].File
})
jsonBytes, _ := json.Marshal(jsonOutput{Violations: violations})
return string(jsonBytes)
}
func printViolations(violations map[string][]ades.Violation, suggestions bool) string {
clean := true
var sb strings.Builder
for file, fileViolations := range violations {
if cnt := len(fileViolations); cnt > 0 {
clean = false
sb.WriteString(fmt.Sprintf("Detected %d violation(s) in %q:", cnt, file))
sb.WriteRune('\n')
for _, violation := range fileViolations {
sb.WriteString(printViolation(&violation, suggestions))
sb.WriteRune('\n')
}
}
}
if clean {
return "Ok\n"
} else {
return sb.String()
}
}
func printViolation(violation *ades.Violation, suggestions bool) string {
var sb strings.Builder
if violation.JobId == "" {
sb.WriteString(fmt.Sprintf(" step %q has %q", violation.StepId, violation.Problem))
} else {
sb.WriteString(fmt.Sprintf(" job %q, step %q has %q", violation.JobId, violation.StepId, violation.Problem))
}
if suggestions {
suggestion, _ := ades.Suggestion(violation)
sb.WriteString(", suggestion:\n")
sb.WriteString(suggestion)
} else {
sb.WriteString(fmt.Sprintf(" (%s)", violation.RuleId))
}
return sb.String()
}