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 24, 2024
1 parent 7e5cdf9 commit dfac68b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .aspell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ allowed:
- frontends
- tcp
- crd
- linter
- linters
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ golangci_lint:
tags:
- go
script:
- make lint
- make lint-seq
commit-policy:
stage: lint
needs: []
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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: check-commit
check-commit:
cd bin;CHECK_COMMIT=${CHECK_COMMIT} sh check-commit.sh
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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
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.31.1
k8s.io/apiextensions-apiserver v0.31.1
k8s.io/apimachinery v0.31.1
Expand Down Expand Up @@ -87,7 +88,6 @@ require (
gopkg.in/evanphx/json-patch.v4 v4.12.0 // 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.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240726031636-6f6746feab9c // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
Expand Down

0 comments on commit dfac68b

Please sign in to comment.