mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Fix error handling in mage build
* print gofmt errors * don't error on lint failures * explanatory comments and fix an error text
This commit is contained in:
parent
1d52bfbb47
commit
c9c19d7945
1 changed files with 23 additions and 5 deletions
28
magefile.go
28
magefile.go
|
@ -5,6 +5,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -124,14 +125,28 @@ func Fmt() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
failed := false
|
failed := false
|
||||||
|
first := true
|
||||||
for _, pkg := range pkgs {
|
for _, pkg := range pkgs {
|
||||||
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
|
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if err := sh.Run("gofmt", "-l", f); err != nil {
|
// gofmt doesn't exit with non-zero when it finds unformatted code
|
||||||
failed = false
|
// so we have to explicitly look for output, and if we find any, we
|
||||||
|
// should fail this target.
|
||||||
|
s, err := sh.Output("gofmt", "-l", f)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
|
||||||
|
failed = true
|
||||||
|
}
|
||||||
|
if s != "" {
|
||||||
|
if first {
|
||||||
|
fmt.Println("The following files are not gofmt'ed:")
|
||||||
|
first = false
|
||||||
|
}
|
||||||
|
failed = true
|
||||||
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -164,12 +179,15 @@ func Lint() error {
|
||||||
}
|
}
|
||||||
failed := false
|
failed := false
|
||||||
for _, pkg := range pkgs {
|
for _, pkg := range pkgs {
|
||||||
if _, err := sh.Exec(nil, os.Stderr, os.Stderr, "golint", "-set_exit_status", pkg); err != nil {
|
// We don't actually want to fail this target if we find golint errors,
|
||||||
|
// so we don't pass -set_exit_status, but we still print out any failures.
|
||||||
|
if _, err := sh.Exec(nil, os.Stderr, nil, "golint", pkg); err != nil {
|
||||||
|
fmt.Printf("ERROR: running go lint on %q: %v\n", pkg, err)
|
||||||
failed = true
|
failed = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if failed {
|
if failed {
|
||||||
return errors.New("golint errors!")
|
return errors.New("errors running golint")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -178,7 +196,7 @@ func Lint() error {
|
||||||
func Vet() error {
|
func Vet() error {
|
||||||
mg.Deps(govendor)
|
mg.Deps(govendor)
|
||||||
if err := sh.Run("govendor", "vet", "+local"); err != nil {
|
if err := sh.Run("govendor", "vet", "+local"); err != nil {
|
||||||
return errors.New("go vet errors!")
|
return fmt.Errorf("error running govendor: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue