forked from morphy2k/revive-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (79 loc) · 1.63 KB
/
main.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
// The Revive action binary
package main
import (
"encoding/json"
"flag"
"fmt"
"go/token"
"os"
"sync"
)
var version = "unknown"
type failure struct {
Failure string
RuleName string
Category string
Position position
Confidence float64
Severity string
}
type position struct {
Start token.Position
End token.Position
}
type statistics struct {
Total, Warnings, Errors int
}
func (f statistics) String() string {
return fmt.Sprintf("%d failures (%d warnings, %d errors)",
f.Total, f.Warnings, f.Errors)
}
func getFailures(ch chan *failure) {
dec := json.NewDecoder(os.Stdin)
for dec.More() {
f := &failure{}
if err := dec.Decode(f); err != nil {
fmt.Fprintln(os.Stderr, "Error while decoding stdin:", err)
os.Exit(1)
}
ch <- f
}
close(ch)
}
func printFailure(f *failure, wg *sync.WaitGroup) {
s := fmt.Sprintf("file=%s,line=%d,col=%d::%s\n",
f.Position.Start.Filename, f.Position.Start.Line, f.Position.Start.Column, f.Failure)
if f.Severity == "warning" {
fmt.Printf("::warning %s", s)
} else {
fmt.Printf("::error %s", s)
}
wg.Done()
}
func main() {
printVersion := flag.Bool("version", false, "Print version")
flag.Parse()
if *printVersion {
fmt.Println(version)
os.Exit(0)
}
stats := &statistics{}
ch := make(chan *failure)
go getFailures(ch)
wg := &sync.WaitGroup{}
fmt.Println("::group::Failures")
for f := range ch {
wg.Add(1)
stats.Total++
switch f.Severity {
case "warning":
stats.Warnings++
case "error":
stats.Errors++
}
go printFailure(f, wg)
}
wg.Wait()
fmt.Println("::endgroup::")
fmt.Println("Successful run with", stats.String())
}