-
Notifications
You must be signed in to change notification settings - Fork 313
/
buildutil.go
83 lines (68 loc) · 1.33 KB
/
buildutil.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package buildutil
import (
"fmt"
"os/exec"
"sync"
"github.com/cockroachdb/errors"
)
type Bailout struct {
Err error
}
func (b Bailout) Error() string {
return b.Err.Error()
}
func Bail(err error) {
panic(Bailout{err})
}
func Bailf(format string, args ...any) {
Bail(fmt.Errorf(format, args...))
}
func Must[T any](val T, err error) T {
if err != nil {
Bail(err)
}
return val
}
func Check(err error) {
if err != nil {
Bail(err)
}
}
func TarGzip(srcDirectory string, tarFile string) error {
// Create the tar.gz file from the src directory
cmd := exec.Command("tar", "-czf", tarFile, "-C", srcDirectory, ".")
// nosemgrep
if out, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "failed to create tar.gz: %s", out)
}
return nil
}
// RunParallel runs the given functions in parallel, bailing with the first error
func RunParallel(functions ...func()) {
var wg sync.WaitGroup
wg.Add(len(functions))
var firstErr error
var mu sync.Mutex
for _, f := range functions {
f := f
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
if b, ok := err.(Bailout); ok {
mu.Lock()
defer mu.Unlock()
if firstErr == nil {
firstErr = b.Err
}
} else {
panic(err)
}
}
}()
f()
}()
}
wg.Wait()
Check(firstErr)
}