2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-11 21:34:27 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DockerExec simply shell out to the docker CLI binary on your host.
|
|
|
|
func DockerExec(args []string, stdout io.Writer) error {
|
|
|
|
return cmdExec("docker", "docker", args, stdout, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdExec(name, binary string, args []string, stdout io.Writer, dir string) error {
|
|
|
|
if binary == "" {
|
|
|
|
panic("binary named " + name + " was not detected")
|
|
|
|
}
|
|
|
|
var errWriter bytes.Buffer
|
|
|
|
|
|
|
|
if stdout == nil {
|
|
|
|
stdout = os.Stdout
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(binary, args...)
|
|
|
|
if dir != "" {
|
|
|
|
cmd.Dir = dir
|
|
|
|
}
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = &errWriter
|
|
|
|
cmd.Stdin = nil
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("could not invoke %q: %v : %s", name, err, errWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|