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 Feb 8, 2024
1 parent 5ab0b78 commit e81f70a
Show file tree
Hide file tree
Showing 18 changed files with 753 additions and 3,116 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ linters:
- ireturn
- varnamelen
- exhaustruct
- depguard

issues:
exclude-rules:
Expand Down
22 changes: 14 additions & 8 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ 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)
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 +41,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 +54,5 @@ func buildArtifact(art config.Artifact, pp tasks.PartProcessing) {
err = ErrNoBuilderForArtifact
}
pp.Done(err)
return err
}
10 changes: 6 additions & 4 deletions checks.go
Original file line number Diff line number Diff line change
@@ -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()
}
66 changes: 66 additions & 0 deletions config/binaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package config

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

"github.com/cardil/ghet/pkg/ghet/download"
"github.com/cardil/ghet/pkg/ghet/install"
)

type Binaries interface {
Configurator
Install(ctx context.Context, 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, destination string) error {
for _, binSpec := range b.bins {
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)
continue
}
if err := download.Action(ctx, args); err != nil {
return err
}
}
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
}
7 changes: 5 additions & 2 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func FillInDefaultValues(cfg Config) Config {
Label: "[MAGE]",
}
}
if cfg.Dependencies == nil {
cfg.Dependencies = NewDependencies("gotest.tools/gotestsum@latest")
if cfg.Dependencies.Golang == nil {
cfg.Dependencies.Golang = NewDependencies("gotest.tools/gotestsum@latest")
}
if cfg.Dependencies.Binaries == nil {
cfg.Dependencies.Binaries = NewBinaries()
}
if cfg.Context == nil {
cfg.Context = context.TODO()
Expand Down
38 changes: 32 additions & 6 deletions config/deps.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package config

import (
"context"

"github.com/magefile/mage/sh"
)

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

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

func NewDependencies(deps ...string) Dependencies {
Expand All @@ -22,7 +31,24 @@ 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, dest string) error {
for _, dep := range d.installs() {
env := map[string]string{
"GOBIN": dest,
}
err := sh.RunWith(env, "go", "install", dep)
if err != nil {
return err
}
}
return nil
}

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

i := 0
Expand All @@ -35,11 +61,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
}
}
10 changes: 7 additions & 3 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/fatih/color"

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

Expand All @@ -21,9 +22,12 @@ type Config struct {
// 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
Loading

0 comments on commit e81f70a

Please sign in to comment.