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

馃巵 Auto-install golangci-lint #44

Merged
merged 2 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions .github/workflows/lints.yml
Expand Up @@ -6,24 +6,30 @@ on:
branches:
- master
pull_request:

env:
FORCE_COLOR: true

jobs:
golangci:
name: Golangci
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
cache: false
- uses: golangci/golangci-lint-action@v4
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
steps:
- uses: actions/checkout@v2
- name: ECLint
uses: snow-actions/eclint@v1.0.1
- uses: actions/checkout@v4
- uses: snow-actions/eclint@v1.0.1
with:
args: check

2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Run Snyk to check for vulnerabilities
uses: snyk/actions/golang@master
Expand Down
31 changes: 23 additions & 8 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,20 +18,32 @@ jobs:
strategy:
matrix:
go-version:
- '1.18'
- '1.19'
- '1.22'
- '1.21'
steps:

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2
uses: actions/checkout@v4

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

- name: Test
run: go test -v -count=1 -race ./...
env:
FORCE_COLOR: true
- 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
}
}