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

Commit

Permalink
Auto-install golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 22, 2024
1 parent 5ab0b78 commit 52c27ae
Show file tree
Hide file tree
Showing 31 changed files with 822 additions and 3,174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lints.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.46.2
version: v1.55.2
editorconfig:
name: EditorConfig
runs-on: ubuntu-latest
Expand Down
27 changes: 21 additions & 6 deletions .github/workflows/test.yml
Expand Up @@ -7,6 +7,9 @@ on:
pull_request:
types: [opened, synchronize, reopened]

env:
FORCE_COLOR: true

jobs:

build:
Expand All @@ -15,8 +18,8 @@ jobs:
strategy:
matrix:
go-version:
- '1.18'
- '1.19'
- '1.22'
- '1.21'
steps:

- name: Set up Go ${{ matrix.go-version }}
Expand All @@ -28,7 +31,19 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Test
run: go test -v -count=1 -race ./...
env:
FORCE_COLOR: true
- name: Test unit
run: go run gotest.tools/gotestsum@latest
--format testname --
-count=1
-race
-timeout=5m
-short
./...

- name: Test e2e
run: go run gotest.tools/gotestsum@latest
--format testname --
-tags=e2e
-count=1
-timeout=10m
./tests/...
1 change: 1 addition & 0 deletions .golangci.yml
Expand Up @@ -31,6 +31,7 @@ linters:
- ireturn
- varnamelen
- exhaustruct
- depguard

issues:
exclude-rules:
Expand Down
2 changes: 1 addition & 1 deletion README.adoc
@@ -1,3 +1,3 @@
= A general purpuse, reusable Mage tasks
= A general purpose, reusable Mage tasks

This repo holds a list of reusable Mage tasks to be used in other projects.
23 changes: 15 additions & 8 deletions build.go
Expand Up @@ -5,30 +5,35 @@ import (
"errors"
"fmt"

"github.com/magefile/mage/mg"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/pkg/artifact"
"github.com/wavesoftware/go-magetasks/pkg/files"
"github.com/wavesoftware/go-magetasks/pkg/targets"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

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

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

buildArtifact(art, pp)
err := buildArtifact(art, pp)
if err != nil {
t.End(err)
return err
}
}
t.End()

return nil
}

func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
func buildArtifact(art config.Artifact, pp tasks.PartProcessing) error {
found := false
for _, builder := range config.Actual().Builders {
if !builder.Accepts(art) {
Expand All @@ -37,8 +42,9 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
found = true
result := builder.Build(art, pp)
if result.Failed() {
pp.Done(result.Error)
return
err := result.Error
pp.Done(err)
return err
}
config.WithContext(func(ctx context.Context) context.Context {
return context.WithValue(ctx, artifact.BuildKey(art), result)
Expand All @@ -49,4 +55,5 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
err = ErrNoBuilderForArtifact
}
pp.Done(err)
return err
}
10 changes: 6 additions & 4 deletions checks.go
@@ -1,20 +1,22 @@
package magetasks

import (
"github.com/magefile/mage/mg"
"context"

"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/pkg/deps"
"github.com/wavesoftware/go-magetasks/pkg/targets"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

// Check will run all lints checks.
func Check() {
mg.Deps(deps.Install)
func Check(ctx context.Context) {
targets.Deps(ctx, deps.Install)
t := tasks.Start("🔍", "Checking", len(config.Actual().Checks) > 0)
for _, check := range config.Actual().Checks {
p := t.Part(check.Name)
pp := p.Starting()
pp.Done(check.Operation(pp))
}
t.End(nil)
t.End()
}
72 changes: 72 additions & 0 deletions config/binaries.go
@@ -0,0 +1,72 @@
package config

import (
"context"
"fmt"
"log"
"os"

"github.com/cardil/ghet/pkg/ghet/download"
"github.com/cardil/ghet/pkg/ghet/install"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

type Binaries interface {
Configurator
Install(ctx context.Context, t *tasks.Task, destination string) error
Count() int

merge(b *binaries)
}

func NewBinaries(bins ...string) Binaries {
return &binaries{
bins: bins,
}
}

type binaries struct {
bins []string
}

func (b *binaries) Count() int {
return len(b.bins)
}

func (b *binaries) Install(ctx context.Context, t *tasks.Task, destination string) error {
for _, binSpec := range b.bins {
p := t.Part(fmt.Sprintf("Pre-built binary %q", binSpec))
pp := p.Starting()
args := download.Args{
Args: install.Parse(binSpec),
Destination: destination,
}
bin := args.Asset.FileName.ToString()
path := fmt.Sprintf("%s/%s", destination, bin)
if fileExist(path) {
log.Println("Skipping installation of", binSpec,
"because it already exists:", path)
p.Skip("already installed")
continue
}
if err := download.Action(ctx, args); err != nil {
pp.Done(err)
return err
}
pp.Notify("installed")
}
return nil
}

func (b *binaries) Configure(cfg Configurable) {
cfg.Config().Dependencies.Binaries.merge(b)
}

func (b *binaries) merge(b2 *binaries) {
b.bins = append(b.bins, b2.bins...)
}

func fileExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}
14 changes: 4 additions & 10 deletions config/defaults.go
Expand Up @@ -2,8 +2,6 @@ package config

import (
"context"

"github.com/fatih/color"
)

var (
Expand All @@ -18,15 +16,11 @@ func FillInDefaultValues(cfg Config) Config {
if len(cfg.BuildDirPath) == 0 {
cfg.BuildDirPath = []string{"build", "_output"}
}
empty := &MageTag{}
if cfg.MageTag == *empty {
cfg.MageTag = MageTag{
Color: color.FgCyan,
Label: "[MAGE]",
}
if cfg.Dependencies.Golang == nil {
cfg.Dependencies.Golang = NewDependencies("gotest.tools/gotestsum@latest")
}
if cfg.Dependencies == nil {
cfg.Dependencies = NewDependencies("gotest.tools/gotestsum@latest")
if cfg.Dependencies.Binaries == nil {
cfg.Dependencies.Binaries = NewBinaries()
}
if cfg.Context == nil {
cfg.Context = context.TODO()
Expand Down
42 changes: 36 additions & 6 deletions config/deps.go
@@ -1,9 +1,20 @@
package config

import (
"context"
"fmt"

"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-magetasks/pkg/tasks"
)

type Dependencies interface {
Configurator
Installs() []string
merge(other Dependencies)
Install(ctx context.Context, t *tasks.Task, dest string) error
Count() int

merge(other dependencies)
installs() []string
}

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

func (d dependencies) Installs() []string {
func (d dependencies) Count() int {
return len(d.set)
}

func (d dependencies) Install(_ context.Context, t *tasks.Task, dest string) error {
for _, dep := range d.installs() {
pp := t.Part(fmt.Sprintf("Go install %q", dep)).Starting()
env := map[string]string{
"GOBIN": dest,
}
if err := sh.RunWith(env, "go", "install", dep); err != nil {
pp.Done(err)
return err
}
pp.Notify("installed")
}
return nil
}

func (d dependencies) installs() []string {
keys := make([]string, len(d.set))

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

func (d dependencies) Configure(cfg Configurable) {
cfg.Config().Dependencies.merge(d)
cfg.Config().Dependencies.Golang.merge(d)
}

func (d dependencies) merge(other Dependencies) {
for _, dep := range other.Installs() {
func (d dependencies) merge(other dependencies) {
for _, dep := range other.installs() {
d.set[dep] = exists
}
}
19 changes: 6 additions & 13 deletions config/types.go
Expand Up @@ -3,7 +3,6 @@ package config
import (
"context"

"github.com/fatih/color"
"github.com/wavesoftware/go-magetasks/pkg/version"
)

Expand All @@ -18,12 +17,12 @@ type Config struct {
// Version contains the version information.
*Version

// MageTag holds default mage tag settings.
MageTag

// Dependencies will hold additional Golang dependencies that needs to be
// installed before running tasks.
Dependencies Dependencies
// Dependencies will hold additional Golang and pre-compiled dependencies
// that needs to be installed before running tasks.
Dependencies struct {
Golang Dependencies
Binaries
}

// Artifacts holds a list of artifacts to be built.
Artifacts []Artifact
Expand Down Expand Up @@ -80,12 +79,6 @@ func StaticResolver(value string) Resolver {
}
}

// MageTag holds a mage tag.
type MageTag struct {
Color color.Attribute
Label string
}

// Result hold a result of an Artifact build or publish.
type Result struct {
Error error
Expand Down

0 comments on commit 52c27ae

Please sign in to comment.