Files
docker-agent/e2e/binary/shellout.go
T
David Gageot 8458a0ba1b test(e2e): remove contextless binary exec helpers
Drop Exec and ExecWithEnv wrappers that minted context.Background and update binary_required tests to pass t.Context() through the existing context-aware helpers.
2026-06-29 10:17:44 +02:00

52 lines
1.3 KiB
Go

package binary
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
// CmdResult output of a command, including stdout and stderr
type CmdResult struct {
Stdout string
Stderr string
}
func ExecWithContext(ctx context.Context, cmd string, args ...string) (CmdResult, error) {
return ExecWithContextInDir(ctx, "", cmd, args, nil)
}
func ExecWithContextAndEnv(ctx context.Context, env []string, cmd string, args ...string) (CmdResult, error) {
return ExecWithContextInDir(ctx, "", cmd, args, env)
}
func ExecWithContextInDir(ctx context.Context, dir, cmd string, args, env []string) (CmdResult, error) {
command := exec.CommandContext(ctx, cmd, args...)
command.Dir = dir
command.Env = append(os.Environ(), env...)
res, err := runCmd(command)
if err != nil {
return res, fmt.Errorf("executing '%s %s' : %s: %s", cmd, strings.Join(args, " "), err.Error(), res.Stdout+"\n"+res.Stderr)
}
return res, nil
}
func runCmd(c *exec.Cmd) (CmdResult, error) {
if c.Stdout != nil {
return CmdResult{}, errors.New("exec: Stdout already set")
}
if c.Stderr != nil {
return CmdResult{}, errors.New("exec: Stderr already set")
}
var outBuffer bytes.Buffer
var errBuffer bytes.Buffer
c.Stdout = &outBuffer
c.Stderr = &errBuffer
err := c.Run()
return CmdResult{outBuffer.String(), errBuffer.String()}, err
}