|
| 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 | +} |
0 commit comments