Skip to content

Commit ce7d72f

Browse files
committed
BUILD/MEDIUM: linter: add sequential running of linters
1 parent f26aa67 commit ce7d72f

File tree

5 files changed

+177
-53
lines changed

5 files changed

+177
-53
lines changed

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ golangci_lint:
8585
tags:
8686
- go
8787
script:
88-
- make lint
88+
- make lint-seq
8989
lint-commit-msg:
9090
stage: lint
9191
needs: []

Makefile

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

30+
.PHONY: lint-seq
31+
lint-seq:
32+
cd bin;GOLANGCI_LINT_VERSION=${GOLANGCI_LINT_VERSION} sh lint-check.sh
33+
go run cmd/linters/*
34+
3035
.PHONY: yaml-lint
3136
yaml-lint:
3237
docker run --rm -v $(pwd):/data cytopia/yamllint .

cmd/linters/main.go

Lines changed: 118 additions & 0 deletions
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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ require (
1414
github.com/jessevdk/go-flags v1.4.0
1515
github.com/pires/go-proxyproto v0.7.0
1616
github.com/prometheus/client_golang v1.18.0
17-
github.com/stretchr/testify v1.8.4
17+
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.29.1
2122
k8s.io/apiextensions-apiserver v0.29.1
2223
k8s.io/apimachinery v0.29.1
@@ -33,22 +34,22 @@ require (
3334
github.com/beorn7/perks v1.0.1 // indirect
3435
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3536
github.com/davecgh/go-spew v1.1.1 // indirect
36-
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
37+
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
3738
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
3839
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
3940
github.com/go-logr/logr v1.4.1 // indirect
4041
github.com/go-openapi/analysis v0.22.2 // indirect
4142
github.com/go-openapi/errors v0.21.0 // indirect
42-
github.com/go-openapi/jsonpointer v0.20.2 // indirect
43-
github.com/go-openapi/jsonreference v0.20.4 // indirect
43+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
44+
github.com/go-openapi/jsonreference v0.21.0 // indirect
4445
github.com/go-openapi/loads v0.21.5 // indirect
4546
github.com/go-openapi/spec v0.20.14 // indirect
4647
github.com/go-openapi/strfmt v0.22.0 // indirect
47-
github.com/go-openapi/swag v0.22.9 // indirect
48+
github.com/go-openapi/swag v0.23.0 // indirect
4849
github.com/go-openapi/validate v0.23.0 // indirect
4950
github.com/gofrs/flock v0.8.1 // indirect
5051
github.com/gogo/protobuf v1.3.2 // indirect
51-
github.com/golang/protobuf v1.5.3 // indirect
52+
github.com/golang/protobuf v1.5.4 // indirect
5253
github.com/google/gnostic-models v0.6.8 // indirect
5354
github.com/google/go-cmp v0.6.0 // indirect
5455
github.com/google/gofuzz v1.2.0 // indirect
@@ -76,20 +77,19 @@ require (
7677
go.mongodb.org/mongo-driver v1.13.1 // indirect
7778
go.uber.org/zap v1.26.0 // indirect
7879
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
79-
golang.org/x/net v0.21.0 // indirect
80+
golang.org/x/net v0.27.0 // indirect
8081
golang.org/x/oauth2 v0.17.0 // indirect
81-
golang.org/x/sys v0.17.0 // indirect
82-
golang.org/x/term v0.17.0 // indirect
83-
golang.org/x/text v0.14.0 // indirect
82+
golang.org/x/sys v0.22.0 // indirect
83+
golang.org/x/term v0.22.0 // indirect
84+
golang.org/x/text v0.16.0 // indirect
8485
golang.org/x/time v0.5.0 // indirect
8586
google.golang.org/appengine v1.6.8 // indirect
86-
google.golang.org/protobuf v1.32.0 // indirect
87+
google.golang.org/protobuf v1.34.2 // indirect
8788
gopkg.in/inf.v0 v0.9.1 // indirect
8889
gopkg.in/yaml.v2 v2.4.0 // indirect
89-
gopkg.in/yaml.v3 v3.0.1 // indirect
90-
k8s.io/klog/v2 v2.120.1 // indirect
91-
k8s.io/kube-openapi v0.0.0-20240209001042-7a0d5b415232 // indirect
92-
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
90+
k8s.io/klog/v2 v2.130.1 // indirect
91+
k8s.io/kube-openapi v0.0.0-20240726031636-6f6746feab9c // indirect
92+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
9393
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
9494
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
9595
)

0 commit comments

Comments
 (0)