/
utils.go
95 lines (74 loc) · 1.89 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
package utils
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"path"
"path/filepath"
"golang.org/x/sync/errgroup"
)
func ExecuteBinary(binaryPath string, args []string, v interface{}) ([]byte, error) {
command := exec.Command(binaryPath, args...)
var stdOut, stErr bytes.Buffer
command.Stdout = &stdOut
command.Stderr = &stErr
if err := command.Run(); err != nil {
return nil, fmt.Errorf("failed to execute binary %s %v with error: %s: %s", binaryPath, args, stErr.String(), err.Error())
}
if v == nil {
return stdOut.Bytes(), nil
}
if err := json.Unmarshal(stdOut.Bytes(), v); err != nil {
// TODO Maybe failback to text parsing instead??
return nil, err
}
return nil, nil
}
func BinaryAbsPath(p string) (string, error) {
lPath, err := exec.LookPath(p)
if err != nil {
return "", fmt.Errorf("failed to look up path for %q: %w", p, err)
}
return AbsPath(lPath)
}
// Returns and error if given binaries are not accesible trough PATH
func BinariesAccessible(binaries ...string) error {
var eg errgroup.Group
for _, bin := range binaries {
bin := bin
eg.Go(func() error {
if _, err := BinaryAbsPath(bin); err != nil {
return err
}
return nil
})
}
return eg.Wait()
}
func AbsPath(p string) (string, error) {
if filepath.IsAbs(p) {
return p, nil
}
aPath, err := filepath.Abs(p)
if err != nil {
return "", fmt.Errorf("failed to get absolute path for %q: %w", p, err)
}
return aPath, nil
}
func AbsPathWithPrefix(prefix, p string) (string, error) {
if filepath.IsAbs(p) {
return p, nil
}
aPath, err := filepath.Abs(path.Join(prefix, p))
if err != nil {
return "", fmt.Errorf("failed to get absolute path for %q: %w", p, err)
}
return aPath, nil
}
func VegaNodeHomePath(networkHomePath string, nodeIdx int) string {
return filepath.Join(networkHomePath, "vega", fmt.Sprintf("node%d", nodeIdx))
}
func ToPoint[T any](val T) *T {
return &val
}