Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a9e52a9

Browse files
authored
🎁 Auto-install golangci-lint (#44)
* Auto-install golangci-lint * Use knative/client-pkg#164
1 parent 5ab0b78 commit a9e52a9

32 files changed

+857
-3196
lines changed

.github/workflows/lints.yml

+13-7
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,30 @@ on:
66
branches:
77
- master
88
pull_request:
9+
10+
env:
11+
FORCE_COLOR: true
12+
913
jobs:
1014
golangci:
1115
name: Golangci
1216
runs-on: ubuntu-latest
1317
steps:
14-
- uses: actions/checkout@v2
15-
- name: golangci-lint
16-
uses: golangci/golangci-lint-action@v2
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-go@v5
20+
with:
21+
go-version: '1.21'
22+
cache: false
23+
- uses: golangci/golangci-lint-action@v4
1724
with:
1825
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
19-
version: v1.46.2
26+
version: v1.55.2
2027
editorconfig:
2128
name: EditorConfig
2229
runs-on: ubuntu-latest
2330
steps:
24-
- uses: actions/checkout@v2
25-
- name: ECLint
26-
uses: snow-actions/[email protected]
31+
- uses: actions/checkout@v4
32+
- uses: snow-actions/[email protected]
2733
with:
2834
args: check
2935

.github/workflows/security.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Checkout code
16-
uses: actions/checkout@v2
16+
uses: actions/checkout@v4
1717

1818
- name: Run Snyk to check for vulnerabilities
1919
uses: snyk/actions/golang@master

.github/workflows/test.yml

+23-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
pull_request:
88
types: [opened, synchronize, reopened]
99

10+
env:
11+
FORCE_COLOR: true
12+
1013
jobs:
1114

1215
build:
@@ -15,20 +18,32 @@ jobs:
1518
strategy:
1619
matrix:
1720
go-version:
18-
- '1.18'
19-
- '1.19'
21+
- '1.22'
22+
- '1.21'
2023
steps:
2124

2225
- name: Set up Go ${{ matrix.go-version }}
23-
uses: actions/setup-go@v2
26+
uses: actions/setup-go@v5
2427
with:
2528
go-version: ${{ matrix.go-version }}
2629
id: go
2730

2831
- name: Check out code into the Go module directory
29-
uses: actions/checkout@v2
32+
uses: actions/checkout@v4
33+
34+
- name: Test unit
35+
run: go run gotest.tools/gotestsum@latest
36+
--format testname --
37+
-count=1
38+
-race
39+
-timeout=5m
40+
-short
41+
./...
3042

31-
- name: Test
32-
run: go test -v -count=1 -race ./...
33-
env:
34-
FORCE_COLOR: true
43+
- name: Test e2e
44+
run: go run gotest.tools/gotestsum@latest
45+
--format testname --
46+
-tags=e2e
47+
-count=1
48+
-timeout=10m
49+
./tests/...

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ linters:
3131
- ireturn
3232
- varnamelen
3333
- exhaustruct
34+
- depguard
3435

3536
issues:
3637
exclude-rules:

README.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
= A general purpuse, reusable Mage tasks
1+
= A general purpose, reusable Mage tasks
22

33
This repo holds a list of reusable Mage tasks to be used in other projects.

build.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,35 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/magefile/mage/mg"
98
"github.com/wavesoftware/go-magetasks/config"
109
"github.com/wavesoftware/go-magetasks/pkg/artifact"
11-
"github.com/wavesoftware/go-magetasks/pkg/files"
10+
"github.com/wavesoftware/go-magetasks/pkg/targets"
1211
"github.com/wavesoftware/go-magetasks/pkg/tasks"
1312
)
1413

1514
// ErrNoBuilderForArtifact when no builder for artifact is found.
1615
var ErrNoBuilderForArtifact = errors.New("no builder for artifact found")
1716

1817
// Build will build project artifacts, binaries and images.
19-
func Build() {
20-
mg.Deps(Test, files.EnsureBuildDir)
18+
func Build(ctx context.Context) error {
19+
targets.Deps(ctx, Test)
2120
t := tasks.Start("🔨", "Building", len(config.Actual().Artifacts) > 0)
2221
for _, art := range config.Actual().Artifacts {
2322
p := t.Part(fmt.Sprintf("%s %s", art.GetType(), art.GetName()))
2423
pp := p.Starting()
2524

26-
buildArtifact(art, pp)
25+
err := buildArtifact(art, pp)
26+
if err != nil {
27+
t.End(err)
28+
return err
29+
}
2730
}
2831
t.End()
32+
33+
return nil
2934
}
3035

31-
func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
36+
func buildArtifact(art config.Artifact, pp tasks.PartProcessing) error {
3237
found := false
3338
for _, builder := range config.Actual().Builders {
3439
if !builder.Accepts(art) {
@@ -37,8 +42,9 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
3742
found = true
3843
result := builder.Build(art, pp)
3944
if result.Failed() {
40-
pp.Done(result.Error)
41-
return
45+
err := result.Error
46+
pp.Done(err)
47+
return err
4248
}
4349
config.WithContext(func(ctx context.Context) context.Context {
4450
return context.WithValue(ctx, artifact.BuildKey(art), result)
@@ -49,4 +55,5 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
4955
err = ErrNoBuilderForArtifact
5056
}
5157
pp.Done(err)
58+
return err
5259
}

checks.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package magetasks
22

33
import (
4-
"github.com/magefile/mage/mg"
4+
"context"
5+
56
"github.com/wavesoftware/go-magetasks/config"
67
"github.com/wavesoftware/go-magetasks/pkg/deps"
8+
"github.com/wavesoftware/go-magetasks/pkg/targets"
79
"github.com/wavesoftware/go-magetasks/pkg/tasks"
810
)
911

1012
// Check will run all lints checks.
11-
func Check() {
12-
mg.Deps(deps.Install)
13+
func Check(ctx context.Context) {
14+
targets.Deps(ctx, deps.Install)
1315
t := tasks.Start("🔍", "Checking", len(config.Actual().Checks) > 0)
1416
for _, check := range config.Actual().Checks {
1517
p := t.Part(check.Name)
1618
pp := p.Starting()
1719
pp.Done(check.Operation(pp))
1820
}
19-
t.End(nil)
21+
t.End()
2022
}

config/binaries.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package config
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/cardil/ghet/pkg/ghet/download"
10+
"github.com/cardil/ghet/pkg/ghet/install"
11+
"github.com/wavesoftware/go-magetasks/pkg/tasks"
12+
)
13+
14+
type Binaries interface {
15+
Configurator
16+
Install(ctx context.Context, t *tasks.Task, destination string) error
17+
Count() int
18+
19+
merge(b *binaries)
20+
}
21+
22+
func NewBinaries(bins ...string) Binaries {
23+
return &binaries{
24+
bins: bins,
25+
}
26+
}
27+
28+
type binaries struct {
29+
bins []string
30+
}
31+
32+
func (b *binaries) Count() int {
33+
return len(b.bins)
34+
}
35+
36+
func (b *binaries) Install(ctx context.Context, t *tasks.Task, destination string) error {
37+
for _, binSpec := range b.bins {
38+
p := t.Part(fmt.Sprintf("Pre-built binary %q", binSpec))
39+
pp := p.Starting()
40+
args := download.Args{
41+
Args: install.Parse(binSpec),
42+
Destination: destination,
43+
}
44+
bin := args.Asset.FileName.ToString()
45+
path := fmt.Sprintf("%s/%s", destination, bin)
46+
if fileExist(path) {
47+
log.Println("Skipping installation of", binSpec,
48+
"because it already exists:", path)
49+
p.Skip("already installed")
50+
continue
51+
}
52+
if err := download.Action(ctx, args); err != nil {
53+
pp.Done(err)
54+
return err
55+
}
56+
pp.Notify("installed")
57+
}
58+
return nil
59+
}
60+
61+
func (b *binaries) Configure(cfg Configurable) {
62+
cfg.Config().Dependencies.Binaries.merge(b)
63+
}
64+
65+
func (b *binaries) merge(b2 *binaries) {
66+
b.bins = append(b.bins, b2.bins...)
67+
}
68+
69+
func fileExist(path string) bool {
70+
_, err := os.Stat(path)
71+
return err == nil
72+
}

config/defaults.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package config
22

33
import (
44
"context"
5-
6-
"github.com/fatih/color"
75
)
86

97
var (
@@ -18,15 +16,11 @@ func FillInDefaultValues(cfg Config) Config {
1816
if len(cfg.BuildDirPath) == 0 {
1917
cfg.BuildDirPath = []string{"build", "_output"}
2018
}
21-
empty := &MageTag{}
22-
if cfg.MageTag == *empty {
23-
cfg.MageTag = MageTag{
24-
Color: color.FgCyan,
25-
Label: "[MAGE]",
26-
}
19+
if cfg.Dependencies.Golang == nil {
20+
cfg.Dependencies.Golang = NewDependencies("gotest.tools/gotestsum@latest")
2721
}
28-
if cfg.Dependencies == nil {
29-
cfg.Dependencies = NewDependencies("gotest.tools/gotestsum@latest")
22+
if cfg.Dependencies.Binaries == nil {
23+
cfg.Dependencies.Binaries = NewBinaries()
3024
}
3125
if cfg.Context == nil {
3226
cfg.Context = context.TODO()

config/deps.go

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package config
22

3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/magefile/mage/sh"
8+
"github.com/wavesoftware/go-magetasks/pkg/tasks"
9+
)
10+
311
type Dependencies interface {
412
Configurator
5-
Installs() []string
6-
merge(other Dependencies)
13+
Install(ctx context.Context, t *tasks.Task, dest string) error
14+
Count() int
15+
16+
merge(other dependencies)
17+
installs() []string
718
}
819

920
func NewDependencies(deps ...string) Dependencies {
@@ -22,7 +33,26 @@ type dependencies struct {
2233
set map[string]bool
2334
}
2435

25-
func (d dependencies) Installs() []string {
36+
func (d dependencies) Count() int {
37+
return len(d.set)
38+
}
39+
40+
func (d dependencies) Install(_ context.Context, t *tasks.Task, dest string) error {
41+
for _, dep := range d.installs() {
42+
pp := t.Part(fmt.Sprintf("Go install %q", dep)).Starting()
43+
env := map[string]string{
44+
"GOBIN": dest,
45+
}
46+
if err := sh.RunWith(env, "go", "install", dep); err != nil {
47+
pp.Done(err)
48+
return err
49+
}
50+
pp.Notify("installed")
51+
}
52+
return nil
53+
}
54+
55+
func (d dependencies) installs() []string {
2656
keys := make([]string, len(d.set))
2757

2858
i := 0
@@ -35,11 +65,11 @@ func (d dependencies) Installs() []string {
3565
}
3666

3767
func (d dependencies) Configure(cfg Configurable) {
38-
cfg.Config().Dependencies.merge(d)
68+
cfg.Config().Dependencies.Golang.merge(d)
3969
}
4070

41-
func (d dependencies) merge(other Dependencies) {
42-
for _, dep := range other.Installs() {
71+
func (d dependencies) merge(other dependencies) {
72+
for _, dep := range other.installs() {
4373
d.set[dep] = exists
4474
}
4575
}

0 commit comments

Comments
 (0)