-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
here.go
57 lines (49 loc) · 1.02 KB
/
here.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package here
import (
"bytes"
"fmt"
"os/exec"
"regexp"
"sync"
)
type Here struct {
infos *infoMap
curOnce sync.Once
curErr error
current Info
}
// New returns a Here type that will cache
// all results. This speeds up repeated calls,
// and can be useful for testing.
func New() Here {
return Here{
infos: &infoMap{
data: &sync.Map{},
},
}
}
func run(n string, args ...string) ([]byte, error) {
c := exec.Command(n, args...)
bb := &bytes.Buffer{}
ebb := &bytes.Buffer{}
c.Stdout = bb
c.Stderr = ebb
err := c.Run()
if err != nil {
return nil, fmt.Errorf("%v %w: %s", c.Args, err, ebb)
}
return bb.Bytes(), nil
}
func (h Here) cache(p string, fn func(string) (Info, error)) (Info, error) {
i, ok := h.infos.Load(p)
if ok {
return i, nil
}
i, err := fn(p)
if err != nil {
return i, fmt.Errorf("here.cache: %s: %w", p, err)
}
h.infos.Store(p, i)
return i, nil
}
var nonGoDirRx = regexp.MustCompile(`cannot find main|go help modules|go: |build .:|no Go files|can't load package|not using modules`)