git-pre-commit-hook/has_go.go

54 lines
794 B
Go
Raw Permalink Normal View History

2020-02-16 09:48:17 +00:00
package main
import (
"io/ioutil"
"path/filepath"
"github.com/logrusorgru/aurora"
)
func hasGo() bool {
h, err := hasGoAux(".", 3)
if err != nil {
Warn("error scanning directory for go.mod: %v", aurora.Red(err))
return false
}
return h
}
func hasGoAux(dir string, depth int) (bool, error) {
fi, err := ioutil.ReadDir(dir)
if err != nil {
return false, err
}
var dirs []string
for _, f := range fi {
switch {
case f.Name()[0] == '.':
// do nothing
case f.IsDir():
dirs = append(dirs, f.Name())
case f.Name() == "go.mod":
return true, nil
}
}
if depth == 0 {
return false, nil
}
depth--
for _, subdir := range dirs {
h, err := hasGoAux(filepath.Join(dir, subdir), depth)
if h || err != nil {
return h, err
}
}
return false, nil
}