@@ -2,9 +2,11 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
7
8
"io/fs"
9
+ "log/slog"
8
10
"os"
9
11
"os/exec"
10
12
"path/filepath"
@@ -13,6 +15,10 @@ import (
13
15
"github.com/magefile/mage/mg"
14
16
"github.com/magefile/mage/sh"
15
17
"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"
16
22
)
17
23
18
24
var (
24
30
}
25
31
)
26
32
33
+ func init () {
34
+ slog .SetDefault (log .New (log .NewHandler (os .Stderr , nil ))) // stdout is suppressed in mage
35
+ }
36
+
27
37
func version () (string , error ) {
28
38
if ver , err := sh .Output ("git" , "describe" , "--tags" , "--always" ); err != nil {
29
39
return "" , err
@@ -60,15 +70,38 @@ func (Tool) Wire() error {
60
70
}
61
71
62
72
// GolangciLint installs golangci-lint
63
- func (Tool ) GolangciLint () error {
73
+ func (t Tool ) GolangciLint () error {
64
74
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 ) {
66
77
return nil
67
78
}
68
79
command := fmt .Sprintf ("curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b %s %s" , GOBIN , version )
69
80
return sh .Run ("bash" , "-c" , command )
70
81
}
71
82
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
+
72
105
// Labeler installs labeler
73
106
func (Tool ) Labeler () error {
74
107
if exists (filepath .Join (GOBIN , "labeler" )) {
0 commit comments