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

Commit f9ca0d2

Browse files
committed
Auto-install golangci-lint
1 parent 5ab0b78 commit f9ca0d2

31 files changed

+818
-3174
lines changed

.github/workflows/lints.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: golangci/golangci-lint-action@v2
1717
with:
1818
# 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
19+
version: v1.55.2
2020
editorconfig:
2121
name: EditorConfig
2222
runs-on: ubuntu-latest

.github/workflows/test.yml

+21-6
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,8 +18,8 @@ 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 }}
@@ -28,7 +31,19 @@ jobs:
2831
- name: Check out code into the Go module directory
2932
uses: actions/checkout@v2
3033

31-
- name: Test
32-
run: go test -v -count=1 -race ./...
33-
env:
34-
FORCE_COLOR: true
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+
./...
42+
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

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,34 @@ 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+
t.End(err)
27+
28+
return err
2729
}
2830
t.End()
31+
32+
return nil
2933
}
3034

31-
func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
35+
func buildArtifact(art config.Artifact, pp tasks.PartProcessing) error {
3236
found := false
3337
for _, builder := range config.Actual().Builders {
3438
if !builder.Accepts(art) {
@@ -37,8 +41,9 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
3741
found = true
3842
result := builder.Build(art, pp)
3943
if result.Failed() {
40-
pp.Done(result.Error)
41-
return
44+
err := result.Error
45+
pp.Done(err)
46+
return err
4247
}
4348
config.WithContext(func(ctx context.Context) context.Context {
4449
return context.WithValue(ctx, artifact.BuildKey(art), result)
@@ -49,4 +54,5 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
4954
err = ErrNoBuilderForArtifact
5055
}
5156
pp.Done(err)
57+
return err
5258
}

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
}

config/types.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"context"
55

6-
"github.com/fatih/color"
76
"github.com/wavesoftware/go-magetasks/pkg/version"
87
)
98

@@ -18,12 +17,12 @@ type Config struct {
1817
// Version contains the version information.
1918
*Version
2019

21-
// MageTag holds default mage tag settings.
22-
MageTag
23-
24-
// Dependencies will hold additional Golang dependencies that needs to be
25-
// installed before running tasks.
26-
Dependencies Dependencies
20+
// Dependencies will hold additional Golang and pre-compiled dependencies
21+
// that needs to be installed before running tasks.
22+
Dependencies struct {
23+
Golang Dependencies
24+
Binaries
25+
}
2726

2827
// Artifacts holds a list of artifacts to be built.
2928
Artifacts []Artifact
@@ -80,12 +79,6 @@ func StaticResolver(value string) Resolver {
8079
}
8180
}
8281

83-
// MageTag holds a mage tag.
84-
type MageTag struct {
85-
Color color.Attribute
86-
Label string
87-
}
88-
8982
// Result hold a result of an Artifact build or publish.
9083
type Result struct {
9184
Error error

0 commit comments

Comments
 (0)