Repository navigation
Expand file tree
/
Copy pathexec.go
More file actions
70 lines (60 loc) · 1.53 KB
/
Copy pathexec.go
File metadata and controls
70 lines (60 loc) · 1.53 KB
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
package main
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"strings"
)
// findStroppy locates the stroppy binary.
// Search order: STROPPY_BIN env → ./build/stroppy → $PATH
func findStroppy() (string, error) {
if bin := os.Getenv("STROPPY_BIN"); bin != "" {
if _, err := os.Stat(bin); err == nil {
return bin, nil
}
}
if _, err := os.Stat("./build/stroppy"); err == nil {
return "./build/stroppy", nil
}
if path, err := exec.LookPath("stroppy"); err == nil {
return path, nil
}
return "", fmt.Errorf("stroppy binary not found: set STROPPY_BIN, place it in ./build/, or add to PATH")
}
// runStroppy executes the stroppy CLI with the given args and extra env vars.
// Returns combined stdout+stderr output.
func runStroppy(ctx context.Context, args []string, env map[string]string) (string, error) {
bin, err := findStroppy()
if err != nil {
return "", err
}
cmd := exec.CommandContext(ctx, bin, args...)
// Inherit current env and merge extras
cmd.Env = os.Environ()
for k, v := range env {
cmd.Env = append(cmd.Env, k+"="+v)
}
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
// Merge output — stderr often has useful info from k6
output := strings.TrimSpace(stdout.String())
errOutput := strings.TrimSpace(stderr.String())
if errOutput != "" {
if output != "" {
output += "\n" + errOutput
} else {
output = errOutput
}
}
if err != nil {
if output != "" {
return "", fmt.Errorf("%w\n%s", err, output)
}
return "", err
}
return output, nil
}