Skip to content

Commit dfac68b

Browse files
committed
BUILD/MEDIUM: linter: add sequential running of linters
1 parent 7e5cdf9 commit dfac68b

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

.aspell.yml

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ allowed:
2424
- frontends
2525
- tcp
2626
- crd
27+
- linter
28+
- linters

.gitlab-ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ golangci_lint:
8686
tags:
8787
- go
8888
script:
89-
- make lint
89+
- make lint-seq
9090
commit-policy:
9191
stage: lint
9292
needs: []

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ lint:
2828
cd bin;GOLANGCI_LINT_VERSION=${GOLANGCI_LINT_VERSION} sh lint-check.sh
2929
bin/golangci-lint run --timeout 20m --color always --max-issues-per-linter 0 --max-same-issues 0
3030

31+
.PHONY: lint-seq
32+
lint-seq:
33+
cd bin;GOLANGCI_LINT_VERSION=${GOLANGCI_LINT_VERSION} sh lint-check.sh
34+
go run cmd/linters/*
35+
3136
.PHONY: check-commit
3237
check-commit:
3338
cd bin;CHECK_COMMIT=${CHECK_COMMIT} sh check-commit.sh

cmd/linters/main.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2019 HAProxy Technologies LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package main
15+
16+
import (
17+
"errors"
18+
"fmt"
19+
"log"
20+
"os"
21+
"os/exec"
22+
"os/signal"
23+
"strings"
24+
"syscall"
25+
"time"
26+
27+
"gopkg.in/yaml.v3"
28+
)
29+
30+
func main() {
31+
cmd := exec.Command("bin/golangci-lint", "linters")
32+
result, err := cmd.CombinedOutput()
33+
if err != nil {
34+
log.Panic(err)
35+
}
36+
if _, err := os.Stat(".golangci.yml"); err == nil {
37+
data, err := os.ReadFile(".golangci.yml")
38+
if err != nil {
39+
log.Panic(err)
40+
}
41+
var config map[string]interface{}
42+
err = yaml.Unmarshal(data, &config)
43+
if err != nil {
44+
log.Panic(err)
45+
}
46+
delete(config, "linters")
47+
48+
err = os.Rename(".golangci.yml", ".golangci.yml.tmp")
49+
if err != nil {
50+
log.Panic(err)
51+
}
52+
53+
yamlData, err := yaml.Marshal(config)
54+
if err != nil {
55+
log.Panic(err)
56+
}
57+
err = os.WriteFile(".golangci.yml", yamlData, 0o600)
58+
if err != nil {
59+
log.Panic(err)
60+
}
61+
}
62+
signalCh := make(chan os.Signal, 1)
63+
signal.Notify(signalCh, syscall.SIGTERM, os.Interrupt)
64+
65+
go func() {
66+
<-signalCh
67+
fmt.Println("ctrl-c received, terminating linters...") //nolint:forbidigo
68+
os.Remove(".golangci.yml")
69+
err = os.Rename(".golangci.yml.tmp", ".golangci.yml")
70+
if err != nil {
71+
log.Panic(err)
72+
}
73+
os.Exit(1)
74+
}()
75+
76+
// fmt.Println(string(result))
77+
lines := strings.Split(string(result), "\n")
78+
exitCode := 0
79+
for _, line := range lines {
80+
if line == "" {
81+
break
82+
}
83+
if strings.HasPrefix(line, "Disabled by your configuration linters") {
84+
break
85+
}
86+
if strings.HasPrefix(line, "Enabled by your configuration linters:") {
87+
continue
88+
}
89+
parts := strings.Split(line, ":")
90+
fmt.Print(parts[0]) //nolint:forbidigo
91+
timeStart := time.Now()
92+
args := []string{"--timeout", "20m", "--max-issues-per-linter", "0", "--max-same-issues", "0", "run", "-E"}
93+
94+
cmd := exec.Command("bin/golangci-lint", append(args, parts[0])...) //nolint:gosec
95+
result, err := cmd.CombinedOutput()
96+
duration := time.Since(timeStart)
97+
fmt.Printf(" %.1fs %s\n", duration.Seconds(), string(result)) //nolint:forbidigo
98+
if err != nil {
99+
var exitError *exec.ExitError
100+
if errors.As(err, &exitError) {
101+
if exitError.Exited() {
102+
if exitError.ExitCode() != 0 {
103+
exitCode = 1
104+
}
105+
}
106+
} else {
107+
fmt.Println(err) //nolint:forbidigo
108+
}
109+
}
110+
}
111+
112+
os.Remove(".golangci.yml")
113+
err = os.Rename(".golangci.yml.tmp", ".golangci.yml")
114+
if err != nil {
115+
log.Panic(err)
116+
}
117+
os.Exit(exitCode)
118+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/stretchr/testify v1.9.0
1818
github.com/valyala/fasthttp v1.50.0
1919
go.uber.org/automaxprocs v1.5.3
20+
gopkg.in/yaml.v3 v3.0.1
2021
k8s.io/api v0.31.1
2122
k8s.io/apiextensions-apiserver v0.31.1
2223
k8s.io/apimachinery v0.31.1
@@ -87,7 +88,6 @@ require (
8788
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
8889
gopkg.in/inf.v0 v0.9.1 // indirect
8990
gopkg.in/yaml.v2 v2.4.0 // indirect
90-
gopkg.in/yaml.v3 v3.0.1 // indirect
9191
k8s.io/klog/v2 v2.130.1 // indirect
9292
k8s.io/kube-openapi v0.0.0-20240726031636-6f6746feab9c // indirect
9393
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect

0 commit comments

Comments
 (0)