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 7, 2024
1 parent 5ab0b78 commit 944b7ed
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 3,096 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
61 changes: 61 additions & 0 deletions config/binaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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

merge(b *binaries)
}

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

type binaries struct {
bins []string
}

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
33 changes: 27 additions & 6 deletions config/deps.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
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

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

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

func (d dependencies) Installs() []string {
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 +56,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 944b7ed

Please sign in to comment.