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

Commit

Permalink
Better parts processing (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 21, 2021
1 parent 9a5a4c1 commit 337d32c
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 43 deletions.
34 changes: 7 additions & 27 deletions checks.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
package magetasks

import (
"os"
"path"

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

// Check will run all lints checks.
func Check() {
t := tasks.StartMultiline("🔍", "Checking")
mg.Deps(revive, staticcheck)
t.End(nil)
}

func revive() error {
mg.Deps(internal.BuildDeps)
reviveConfig := path.Join(internal.RepoDir(), "revive.toml")
if fileExists(reviveConfig) {
return sh.RunV("revive", "-config", "revive.toml", "-formatter", "stylish", "./...")
}
return nil
}

func staticcheck() error {
mg.Deps(internal.BuildDeps)
return sh.RunV("staticcheck", "-f", "stylish", "./...")
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
t := tasks.StartMultiline("🔍", "Checking")
for _, check := range config.Checks {
p := t.Part(check.Name)
ps := p.Starting()
ps.Done(check.Task())
}
return !info.IsDir()
t.End(nil)
}
4 changes: 2 additions & 2 deletions cleaning.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func Clean() {
errs := make([]error, 0, 1)
errs = append(errs, err)
for _, task := range config.CleaningTasks {
err = task()
errs = append(errs, err)
p := t.Part(task.Name)
p.Starting().Done(task.Task())
}
t.End(errs...)
}
24 changes: 20 additions & 4 deletions config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,34 @@ package config
import "github.com/fatih/color"

var (
RepoDir string
// RepoDir holds a repository path.
RepoDir string

// BuildDirPath holds a build dir path.
BuildDirPath = []string{"build", "_output"}
MageTag = MageTagStruct{
// MageTag holds default mage tag settings.
MageTag = MageTagStruct{
Color: color.FgCyan,
Label: "[MAGE]",
}

// Dependencies will hold additional dependencies that needs to be installed
// before running tasks.
Dependencies = []string{
"github.com/kyoh86/richgo",
"github.com/mgechev/revive",
"honnef.co/go/tools/cmd/staticcheck",
}

// VersionVariablePath a Golang path to version holding variable.
VersionVariablePath string
Binaries []Binary
CleaningTasks []CleaningTask

// Binaries a list of binaries to build.
Binaries []Binary

// CleaningTasks additional cleaning tasks.
CleaningTasks []CustomTask

// Checks holds a list of checks to perform.
Checks []CustomTask
)
8 changes: 7 additions & 1 deletion config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package config

import "github.com/fatih/color"

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

// Binary represents a binary that will be built.
type Binary struct {
Name string
}

type CleaningTask func() error
// CustomTask is a custom function that will be used in the build.
type CustomTask struct {
Name string
Task func() error
}
4 changes: 1 addition & 3 deletions container/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func Images() {

if len(config.Binaries) > 0 {
t := tasks.StartMultiline("📦", "Packaging OCI images")
errs := make([]error, 0)
for _, binary := range config.Binaries {
p := t.Part(binary.Name)
cf := containerFile(binary)
Expand All @@ -32,9 +31,8 @@ func Images() {
}
err := sh.RunV(containerEngine(), args...)
st.Done(err)
errs = append(errs, err)
}
t.End(errs...)
t.End()
}
}

Expand Down
10 changes: 8 additions & 2 deletions container/publish.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package container

import (
"fmt"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-magetasks/config"
Expand All @@ -16,13 +18,17 @@ func Publish() {
t := tasks.StartMultiline("📤", "Publishing OCI images")
errs := make([]error, 0)
for _, binary := range config.Binaries {
p := t.Part(binary.Name)
cf := containerFile(binary)
im := imageName(binary)
if internal.DontExists(cf) {
p.Skip(fmt.Sprintf("no container image for %s", im))
continue
}
args := []string{"push", imageName(binary)}
ps := p.Starting()
args := []string{"push", im}
err := sh.RunV(containerEngine(), args...)
errs = append(errs, err)
ps.Done(err)
}
t.End(errs...)
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/checks/golangci_lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checks

import (
"path"

"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/internal"
)

// GolangCiLint will configure golangci-lint in the build.
func GolangCiLint() {
config.Checks = append(config.Checks, config.CustomTask{
Name: "golangci-lint",
Task: golangCiLint,
})
}

func golangCiLint() error {
c := path.Join(internal.RepoDir(), ".golangci.yaml")
if internal.DontExists(c) {
return nil
}
return sh.RunV("golangci-lint", "run", "./...")
}
27 changes: 27 additions & 0 deletions pkg/checks/revive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package checks

import (
"path"

"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/internal"
)

// Revive will configure revive in the build.
func Revive() {
config.Dependencies = append(config.Dependencies, "github.com/mgechev/revive")
config.Checks = append(config.Checks, config.CustomTask{
Name: "revive",
Task: revive,
})
}

func revive() error {
c := path.Join(internal.RepoDir(), "revive.toml")
if internal.DontExists(c) {
return nil
}
return sh.RunV("revive", "-config", "revive.toml",
"-formatter", "stylish", "./...")
}
27 changes: 27 additions & 0 deletions pkg/checks/staticcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package checks

import (
"path"

"github.com/magefile/mage/sh"
"github.com/wavesoftware/go-magetasks/config"
"github.com/wavesoftware/go-magetasks/internal"
)

// Staticcheck will configure staticcheck in the build.
func Staticcheck() {
config.Dependencies = append(config.Dependencies,
"honnef.co/go/tools/cmd/staticcheck")
config.Checks = append(config.Checks, config.CustomTask{
Name: "staticcheck",
Task: staticcheck,
})
}

func staticcheck() error {
c := path.Join(internal.RepoDir(), "staticcheck.conf")
if internal.DontExists(c) {
return nil
}
return sh.RunV("staticcheck", "-f", "stylish", "./...")
}
15 changes: 11 additions & 4 deletions pkg/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Task struct {
icon string
action string
multiline bool
raised []error
}

// Part represents a part of a bigger Task.
Expand Down Expand Up @@ -48,9 +49,12 @@ type partProcessing struct {

// Done is reporting a completeness of part processing.
func (p *partProcessing) Done(err error) {
if p.p.t.multiline && err != nil {
msg := fmt.Sprintf("%s have failed: %v\n", p.p.name, err)
fmt.Print(mageTag() + red(msg))
if err != nil {
if p.p.t.multiline {
msg := fmt.Sprintf(" %s %s have failed: %v\n", p.p.t.icon, p.p.name, err)
fmt.Print(mageTag() + red(msg))
}
p.p.t.raised = append(p.p.t.raised, err)
}
}

Expand Down Expand Up @@ -96,7 +100,10 @@ func (t *Task) start() {
// End will report task completion, either successful or failures.
func (t *Task) End(errs ...error) {
var msg string
merr := multierror.Append(nil, errs...)
sum := make([]error, 0, len(errs)+len(t.raised))
sum = append(sum, t.raised...)
sum = append(sum, errs...)
merr := multierror.Append(nil, sum...)
err := merr.ErrorOrNil()
if err != nil {
msg = erroneousMsg(t)
Expand Down
57 changes: 57 additions & 0 deletions tests/example/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
run:
timeout: 5m

linters:
disable-all: false
presets:
- bugs
- unused
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- exhaustive
- funlen
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- asciicheck
- gocognit
- godot
- godox
- goerr113
- maligned
- nestif
- prealloc
- testpackage
disable:
- whitespace
- wsl
3 changes: 3 additions & 0 deletions tests/example/Magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"

"github.com/joho/godotenv"
"github.com/wavesoftware/go-magetasks/pkg/checks"

// mage:import
"github.com/wavesoftware/go-magetasks"
"github.com/wavesoftware/go-magetasks/config"
Expand All @@ -28,4 +30,5 @@ func init() {
config.Binaries = append(config.Binaries, config.Binary{Name: bin})
}
config.VersionVariablePath = "github.com/wavesoftware/go-magetasks/tests/example/internal.Version"
checks.GolangCiLint()
}

0 comments on commit 337d32c

Please sign in to comment.