Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: enforce golangci-lint version #6700

Merged
merged 2 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"os/exec"
"path/filepath"
Expand All @@ -13,6 +15,10 @@ import (
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"

// Trivy packages should not be imported in Mage (see https://github.com/aquasecurity/trivy/pull/4242),
// but this package doesn't have so many dependencies, and Mage is still fast.
"github.com/aquasecurity/trivy/pkg/log"
)

var (
Expand All @@ -24,6 +30,10 @@ var (
}
)

func init() {
slog.SetDefault(log.New(log.NewHandler(os.Stderr, nil))) // stdout is suppressed in mage
}

func version() (string, error) {
if ver, err := sh.Output("git", "describe", "--tags", "--always"); err != nil {
return "", err
Expand Down Expand Up @@ -60,15 +70,38 @@ func (Tool) Wire() error {
}

// GolangciLint installs golangci-lint
func (Tool) GolangciLint() error {
func (t Tool) GolangciLint() error {
const version = "v1.57.2"
if exists(filepath.Join(GOBIN, "golangci-lint")) {
bin := filepath.Join(GOBIN, "golangci-lint")
if exists(bin) && t.matchGolangciLintVersion(bin, version) {
return nil
}
command := fmt.Sprintf("curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b %s %s", GOBIN, version)
return sh.Run("bash", "-c", command)
}

func (Tool) matchGolangciLintVersion(bin, version string) bool {
out, err := sh.Output(bin, "version", "--format", "json")
if err != nil {
slog.Error("Unable to get golangci-lint version", slog.Any("err", err))
return false
}
var output struct {
Version string `json:"Version"`
}
if err = json.Unmarshal([]byte(out), &output); err != nil {
slog.Error("Unable to parse golangci-lint version", slog.Any("err", err))
return false
}

version = strings.TrimPrefix(version, "v")
if output.Version != version {
slog.Info("golangci-lint version mismatch", slog.String("expected", version), slog.String("actual", output.Version))
return false
}
return true
}

// Labeler installs labeler
func (Tool) Labeler() error {
if exists(filepath.Join(GOBIN, "labeler")) {
Expand Down