From ed47c8132cb9c7b059a0258de93954bec92990b8 Mon Sep 17 00:00:00 2001 From: Zlatko Bratkovic Date: Tue, 24 Sep 2024 15:50:11 +0200 Subject: [PATCH] BUILD/MEDIUM: linter: add sequential running of linters --- .gitlab-ci.yml | 2 +- Makefile | 5 ++ cmd/linters/main.go | 118 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- 4 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 cmd/linters/main.go diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0aa69525..8cc206e5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -86,7 +86,7 @@ golangci_lint: tags: - go script: - - make lint + - make lint-seq lint-commit-msg: stage: lint needs: [] diff --git a/Makefile b/Makefile index 0368d338..55933bda 100644 --- a/Makefile +++ b/Makefile @@ -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 . diff --git a/cmd/linters/main.go b/cmd/linters/main.go new file mode 100644 index 00000000..86256a88 --- /dev/null +++ b/cmd/linters/main.go @@ -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) +} diff --git a/go.mod b/go.mod index 9693f845..21e7ff61 100644 --- a/go.mod +++ b/go.mod @@ -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.30.3 k8s.io/apiextensions-apiserver v0.30.3 k8s.io/apimachinery v0.30.3 @@ -86,7 +87,6 @@ require ( 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.130.1 // indirect k8s.io/kube-openapi v0.0.0-20240726031636-6f6746feab9c // indirect k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect