Skip to content

Commit c9c19d7

Browse files
natefinchbep
authored andcommitted
Fix error handling in mage build
* print gofmt errors * don't error on lint failures * explanatory comments and fix an error text
1 parent 1d52bfb commit c9c19d7

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

‎magefile.go‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"bytes"
77
"errors"
8+
"fmt"
89
"io/ioutil"
910
"os"
1011
"path/filepath"
@@ -124,14 +125,28 @@ func Fmt() error {
124125
return err
125126
}
126127
failed := false
128+
first := true
127129
for _, pkg := range pkgs {
128130
files, err := filepath.Glob(filepath.Join(pkg, "*.go"))
129131
if err != nil {
130132
return nil
131133
}
132134
for _, f := range files {
133-
if err := sh.Run("gofmt", "-l", f); err != nil {
134-
failed = false
135+
// gofmt doesn't exit with non-zero when it finds unformatted code
136+
// so we have to explicitly look for output, and if we find any, we
137+
// should fail this target.
138+
s, err := sh.Output("gofmt", "-l", f)
139+
if err != nil {
140+
fmt.Printf("ERROR: running gofmt on %q: %v\n", f, err)
141+
failed = true
142+
}
143+
if s != "" {
144+
if first {
145+
fmt.Println("The following files are not gofmt'ed:")
146+
first = false
147+
}
148+
failed = true
149+
fmt.Println(s)
135150
}
136151
}
137152
}
@@ -164,12 +179,15 @@ func Lint() error {
164179
}
165180
failed := false
166181
for _, pkg := range pkgs {
167-
if _, err := sh.Exec(nil, os.Stderr, os.Stderr, "golint", "-set_exit_status", pkg); err != nil {
182+
// We don't actually want to fail this target if we find golint errors,
183+
// so we don't pass -set_exit_status, but we still print out any failures.
184+
if _, err := sh.Exec(nil, os.Stderr, nil, "golint", pkg); err != nil {
185+
fmt.Printf("ERROR: running go lint on %q: %v\n", pkg, err)
168186
failed = true
169187
}
170188
}
171189
if failed {
172-
return errors.New("golint errors!")
190+
return errors.New("errors running golint")
173191
}
174192
return nil
175193
}
@@ -178,7 +196,7 @@ func Lint() error {
178196
func Vet() error {
179197
mg.Deps(govendor)
180198
if err := sh.Run("govendor", "vet", "+local"); err != nil {
181-
return errors.New("go vet errors!")
199+
return fmt.Errorf("error running govendor: %v", err)
182200
}
183201
return nil
184202
}

0 commit comments

Comments
 (0)