mirror of
https://github.com/docker/docker-agent.git
synced 2026-11-06 15:07:58 +00:00
Drop Exec and ExecWithEnv wrappers that minted context.Background and update binary_required tests to pass t.Context() through the existing context-aware helpers.
52 lines
1.3 KiB
Go
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
|
|
}
|