git-pre-commit-hook/check_govet.go

44 lines
1.1 KiB
Go
Raw Normal View History

2020-02-16 09:48:17 +00:00
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
)
// goVet executes "go vet" recursively for each module in the git repo. As of
// go 1.13, "go vet ./..." is required to recurse but does not straddle
// module boundaries. It must be run in a valid package directory. It is
// significantly faster than running individual "go vet ." in each dir.
func goVet() error {
return filepath.Walk(".", goVetW)
}
func goVetW(path string, info os.FileInfo, err error) error {
switch {
case err != nil:
return err
case info.Mode().IsDir() && strings.HasPrefix(info.Name(), "."):
return filepath.SkipDir
case info.Mode().IsRegular() && info.Name() == "go.mod":
dir := filepath.Dir(path)
Info(" running %q in %s", aurora.Blue("go vet"), aurora.Green(dir))
cmd := exec.Command("go", "vet", "./...")
cmd.Dir = path
op, err := cmd.CombinedOutput()
switch {
case len(op) > 0 && err != nil:
return fmt.Errorf("go vet failed: %s\n%s\n", aurora.Red(err), op)
case len(op) > 0:
return fmt.Errorf("%s:\n%s\n", aurora.Red("go vet reports"), op)
case err != nil:
return err
}
}
return nil
}