Skip to content

Commit

Permalink
BUILD/MEDIUM: linter: add sequential running of linters
Browse files Browse the repository at this point in the history
  • Loading branch information
oktalz committed Sep 26, 2024
1 parent f26aa67 commit ce7d72f
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ golangci_lint:
tags:
- go
script:
- make lint
- make lint-seq
lint-commit-msg:
stage: lint
needs: []
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ lint:
cd bin;GOLANGCI_LINT_VERSION=${GOLANGCI_LINT_VERSION} sh lint-check.sh
bin/golangci-lint run --timeout 20m --color always --max-issues-per-linter 0 --max-same-issues 0

.PHONY: lint-seq
lint-seq:
cd bin;GOLANGCI_LINT_VERSION=${GOLANGCI_LINT_VERSION} sh lint-check.sh
go run cmd/linters/*

.PHONY: yaml-lint
yaml-lint:
docker run --rm -v $(pwd):/data cytopia/yamllint .
Expand Down
118 changes: 118 additions & 0 deletions cmd/linters/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2019 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"

"gopkg.in/yaml.v3"
)

func main() {
cmd := exec.Command("bin/golangci-lint", "linters")
result, err := cmd.CombinedOutput()
if err != nil {
log.Panic(err)
}
if _, err := os.Stat(".golangci.yml"); err == nil {
data, err := os.ReadFile(".golangci.yml")
if err != nil {
log.Panic(err)
}
var config map[string]interface{}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Panic(err)
}
delete(config, "linters")

err = os.Rename(".golangci.yml", ".golangci.yml.tmp")
if err != nil {
log.Panic(err)
}

yamlData, err := yaml.Marshal(config)
if err != nil {
log.Panic(err)
}
err = os.WriteFile(".golangci.yml", yamlData, 0o600)
if err != nil {
log.Panic(err)
}
}
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGTERM, os.Interrupt)

go func() {
<-signalCh
fmt.Println("ctrl-c received, terminating linters...") //nolint:forbidigo
os.Remove(".golangci.yml")
err = os.Rename(".golangci.yml.tmp", ".golangci.yml")
if err != nil {
log.Panic(err)
}
os.Exit(1)
}()

// fmt.Println(string(result))
lines := strings.Split(string(result), "\n")
exitCode := 0
for _, line := range lines {
if line == "" {
break
}
if strings.HasPrefix(line, "Disabled by your configuration linters") {
break
}
if strings.HasPrefix(line, "Enabled by your configuration linters:") {
continue
}
parts := strings.Split(line, ":")
fmt.Print(parts[0]) //nolint:forbidigo
timeStart := time.Now()
args := []string{"--timeout", "20m", "--max-issues-per-linter", "0", "--max-same-issues", "0", "run", "-E"}

cmd := exec.Command("bin/golangci-lint", append(args, parts[0])...) //nolint:gosec
result, err := cmd.CombinedOutput()
duration := time.Since(timeStart)
fmt.Printf(" %.1fs %s\n", duration.Seconds(), string(result)) //nolint:forbidigo
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
if exitError.Exited() {
if exitError.ExitCode() != 0 {
exitCode = 1
}
}
} else {
fmt.Println(err) //nolint:forbidigo
}
}
}

os.Remove(".golangci.yml")
err = os.Rename(".golangci.yml.tmp", ".golangci.yml")
if err != nil {
log.Panic(err)
}
os.Exit(exitCode)
}
30 changes: 15 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ require (
github.com/jessevdk/go-flags v1.4.0
github.com/pires/go-proxyproto v0.7.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/valyala/fasthttp v1.50.0
go.uber.org/automaxprocs v1.5.3
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.29.1
k8s.io/apiextensions-apiserver v0.29.1
k8s.io/apimachinery v0.29.1
Expand All @@ -33,22 +34,22 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/analysis v0.22.2 // indirect
github.com/go-openapi/errors v0.21.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/loads v0.21.5 // indirect
github.com/go-openapi/spec v0.20.14 // indirect
github.com/go-openapi/strfmt v0.22.0 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/validate v0.23.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down Expand Up @@ -76,20 +77,19 @@ require (
go.mongodb.org/mongo-driver v1.13.1 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240209001042-7a0d5b415232 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240726031636-6f6746feab9c // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading

0 comments on commit ce7d72f

Please sign in to comment.