Skip to content

Commit a944f0e

Browse files
authored
chore: enforce golangci-lint version (#6700)
Signed-off-by: knqyf263 <[email protected]>
1 parent 903bd69 commit a944f0e

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

magefiles/magefile.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io/fs"
9+
"log/slog"
810
"os"
911
"os/exec"
1012
"path/filepath"
@@ -13,6 +15,10 @@ import (
1315
"github.com/magefile/mage/mg"
1416
"github.com/magefile/mage/sh"
1517
"github.com/magefile/mage/target"
18+
19+
// Trivy packages should not be imported in Mage (see https://github.com/aquasecurity/trivy/pull/4242),
20+
// but this package doesn't have so many dependencies, and Mage is still fast.
21+
"github.com/aquasecurity/trivy/pkg/log"
1622
)
1723

1824
var (
@@ -24,6 +30,10 @@ var (
2430
}
2531
)
2632

33+
func init() {
34+
slog.SetDefault(log.New(log.NewHandler(os.Stderr, nil))) // stdout is suppressed in mage
35+
}
36+
2737
func version() (string, error) {
2838
if ver, err := sh.Output("git", "describe", "--tags", "--always"); err != nil {
2939
return "", err
@@ -60,15 +70,38 @@ func (Tool) Wire() error {
6070
}
6171

6272
// GolangciLint installs golangci-lint
63-
func (Tool) GolangciLint() error {
73+
func (t Tool) GolangciLint() error {
6474
const version = "v1.57.2"
65-
if exists(filepath.Join(GOBIN, "golangci-lint")) {
75+
bin := filepath.Join(GOBIN, "golangci-lint")
76+
if exists(bin) && t.matchGolangciLintVersion(bin, version) {
6677
return nil
6778
}
6879
command := fmt.Sprintf("curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b %s %s", GOBIN, version)
6980
return sh.Run("bash", "-c", command)
7081
}
7182

83+
func (Tool) matchGolangciLintVersion(bin, version string) bool {
84+
out, err := sh.Output(bin, "version", "--format", "json")
85+
if err != nil {
86+
slog.Error("Unable to get golangci-lint version", slog.Any("err", err))
87+
return false
88+
}
89+
var output struct {
90+
Version string `json:"Version"`
91+
}
92+
if err = json.Unmarshal([]byte(out), &output); err != nil {
93+
slog.Error("Unable to parse golangci-lint version", slog.Any("err", err))
94+
return false
95+
}
96+
97+
version = strings.TrimPrefix(version, "v")
98+
if output.Version != version {
99+
slog.Info("golangci-lint version mismatch", slog.String("expected", version), slog.String("actual", output.Version))
100+
return false
101+
}
102+
return true
103+
}
104+
72105
// Labeler installs labeler
73106
func (Tool) Labeler() error {
74107
if exists(filepath.Join(GOBIN, "labeler")) {

0 commit comments

Comments
 (0)