Update mockgen.go to have better failure messaging
Mockgen now prints stderr instead of swallowing it
@@ -19,7 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
- "regexp"
+ "strings"
"time"
)
@@ -50,43 +50,52 @@ func main() {
packageName := os.Args[1]
interfaces := os.Args[2]
outputPath := os.Args[3]
- re := regexp.MustCompile("(?m)[\r\n]+^.*" + projectVendor + ".*$")
+ err := generateMocks(packageName, interfaces, outputPath)
+ if err != nil {
+ exitErr, ok := err.(*exec.ExitError)
+ if ok {
+ // Prevents swallowing CLI program errors that come
+ // from running mockgen and goimports
+ // https://golang.org/pkg/os/exec/#ExitError
+ fmt.Fprintln(os.Stderr, string(exitErr.Stderr))
+ }
+ // Print the encapsulating golang error type
+ fmt.Println(err)
+ os.Exit(1)
+ }
+
+}
+
+func generateMocks(packageName string, interfaces string, outputPath string) error {
copyrightHeader := fmt.Sprintf(copyrightHeaderFormat, time.Now().Year())
path, _ := filepath.Split(outputPath)
err := os.MkdirAll(path, os.ModeDir|0755)
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
outputFile, err := os.Create(outputPath)
+ defer outputFile.Close()
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
mockgen := exec.Command("mockgen", packageName, interfaces)
mockgenOut, err := mockgen.Output()
if err != nil {
- fmt.Println(err)
- os.Exit(1)
+ return err
}
- sanitized := re.ReplaceAllString(string(mockgenOut), "")
+ sanitized := strings.Replace(string(mockgenOut), projectVendor, "", -1)
withHeader := copyrightHeader + licenseBlock + sanitized
goimports := exec.Command("goimports")
goimports.Stdin = bytes.NewBufferString(withHeader)
goimports.Stdout = outputFile
- err = goimports.Run()
- outputFile.Close()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
+ return goimports.Run()
}
func usage() {
