pkger/here/here.go

40 lines
541 B
Go
Raw Permalink Normal View History

2019-08-05 00:13:27 +03:00
package here
import (
"bytes"
"os"
"os/exec"
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
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
return nil, err
}
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
}