This repository was archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
155 lines (131 loc) · 3.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"github.com/golangci/golangci-lint/pkg/result"
)
const (
envGitHubWorkspace = "GITHUB_WORKSPACE"
envConfig = "INPUT_CONFIG"
envBasePath = "INPUT_BASEPATH"
)
type report struct {
Issues []result.Issue `json:"Issues"`
Report struct {
Error string `json:"Error"`
} `json:"Report"`
}
var typecheckingErrRegexp = regexp.MustCompile(`^typechecking\serror:\s(.+\.go):(\d+):(\d+):\s(.+)$`)
func (r report) parseError() []annotation {
anns := make([]annotation, 0)
m := typecheckingErrRegexp.FindStringSubmatch(r.Report.Error)
if len(m) == 0 {
return anns
}
line, err := strconv.Atoi(m[2])
if err != nil {
// not fail
return anns
}
col, err := strconv.Atoi(m[3])
if err != nil {
// not fail
return anns
}
anns = append(anns, annotation{
file: m[1],
line: line,
col: col,
text: m[4],
})
return anns
}
type annotation struct {
file string
line int
col int
text string
}
func (a annotation) Output() string {
return fmt.Sprintf("::error file=%s,line=%d,col=%d::%s", a.file, a.line, a.col, a.text)
}
type config struct {
workspace string
config string
basePath string
}
func loadConfig() config {
return config{
workspace: os.Getenv(envGitHubWorkspace),
config: os.Getenv(envConfig),
basePath: os.Getenv(envBasePath),
}
}
func execGolangCILint(cfg config) (int, []annotation, error) {
args := []string{"run"}
if cfg.config != "" {
args = append(args, "--config", cfg.config)
}
args = append(args, "--out-format", "json")
baseDir := filepath.Join(cfg.workspace, cfg.basePath)
cmd := exec.Command("golangci-lint", args...)
cmd.Dir = baseDir
r, err := cmd.StdoutPipe()
if err != nil {
return -1, nil, fmt.Errorf("cannot get stdout: %w", err)
}
defer r.Close()
if err := cmd.Start(); err != nil {
return -1, nil, fmt.Errorf("cannot execute lint: %w", err)
}
var rep report
if err := json.NewDecoder(r).Decode(&rep); err != nil {
return -1, nil, fmt.Errorf("cannot parse result: %w", err)
}
// NOTE: Not need error checking.
// When `cmd.Wait()` failed, `cmd` has already completed.
//nolint:errcheck
cmd.Wait()
exitCode := cmd.ProcessState.ExitCode()
if errAnns := rep.parseError(); len(errAnns) > 0 {
return exitCode, errAnns, nil
}
anns := createAnotations(cfg, rep.Issues)
return exitCode, anns, nil
}
func createAnotations(cfg config, issues []result.Issue) []annotation {
ann := make([]annotation, len(issues))
for i := range issues {
pos := issues[i].Pos
file := filepath.Join(cfg.basePath, pos.Filename)
ann[i] = annotation{
file: file,
line: pos.Line,
col: pos.Column,
text: fmt.Sprintf("[%s] %s", issues[i].FromLinter, issues[i].Text),
}
}
return ann
}
func reportFailures(cfg config, anns []annotation) {
for _, ann := range anns {
fmt.Println(ann.Output())
}
}
func main() {
cfg := loadConfig()
exitStatus, issues, err := execGolangCILint(cfg)
if err != nil {
log.Fatalf("failed to execute golangci-lint: %v", err)
}
if len(issues) > 0 {
reportFailures(cfg, issues)
}
os.Exit(exitStatus)
}