pkger/here/here.go

40 lines
561 B
Go
Raw Permalink Normal View History

2019-08-05 00:13:27 +03:00
package here
import (
"bytes"
"fmt"
2019-08-05 00:13:27 +03:00
"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 = bb
2019-08-05 00:13:27 +03:00
err := c.Run()
if err != nil {
return nil, fmt.Errorf("%w: %s", err, 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
}