2019-08-05 00:13:27 +03:00
|
|
|
package here
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-26 00:11:34 +03:00
|
|
|
"fmt"
|
2019-08-05 00:13:27 +03:00
|
|
|
"os/exec"
|
2019-11-11 02:22:06 +03:00
|
|
|
"regexp"
|
2019-11-12 00:01:05 +03:00
|
|
|
"strings"
|
2019-09-12 04:29:39 +03:00
|
|
|
"sync"
|
2019-08-05 00:13:27 +03:00
|
|
|
)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
var cache = &infoMap{
|
|
|
|
data: &sync.Map{},
|
|
|
|
}
|
|
|
|
|
2019-08-05 00:13:27 +03:00
|
|
|
func run(n string, args ...string) ([]byte, error) {
|
|
|
|
c := exec.Command(n, args...)
|
|
|
|
|
|
|
|
bb := &bytes.Buffer{}
|
|
|
|
c.Stdout = bb
|
2019-10-26 00:11:34 +03:00
|
|
|
c.Stderr = bb
|
2019-08-05 00:13:27 +03:00
|
|
|
err := c.Run()
|
|
|
|
if err != nil {
|
2019-11-12 00:01:05 +03:00
|
|
|
return nil, fmt.Errorf("%w: %q: %s", err, strings.Join(c.Args, " "), bb)
|
2019-08-05 00:13:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return bb.Bytes(), nil
|
|
|
|
}
|
2019-09-12 04:29:39 +03:00
|
|
|
|
|
|
|
func Cache(p string, fn func(string) (Info, error)) (Info, error) {
|
|
|
|
i, ok := cache.Load(p)
|
|
|
|
if ok {
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
i, err := fn(p)
|
|
|
|
if err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
cache.Store(p, i)
|
|
|
|
return i, nil
|
|
|
|
}
|
2019-11-11 02:22:06 +03:00
|
|
|
|
|
|
|
func ClearCache() {
|
|
|
|
cache = &infoMap{
|
|
|
|
data: &sync.Map{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 01:29:41 +03:00
|
|
|
var nonGoDirRx = regexp.MustCompile(`cannot find main|go help modules|go: |build .:|no Go files|can't load package`)
|