-
Notifications
You must be signed in to change notification settings - Fork 180
/
cli.go
48 lines (39 loc) · 1.03 KB
/
cli.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
package terraform
import (
"fmt"
"io"
"os"
"os/exec"
)
type CLI struct {
errorBuffer io.Writer
outputBuffer io.Writer
tfDataDir string
}
func NewCLI(errorBuffer, outputBuffer io.Writer, tfDataDir string) CLI {
return CLI{
errorBuffer: errorBuffer,
outputBuffer: outputBuffer,
tfDataDir: tfDataDir,
}
}
func (c CLI) Run(stdout io.Writer, workingDirectory string, args []string) error {
return c.RunWithEnv(stdout, workingDirectory, args, []string{})
}
func (c CLI) RunWithEnv(stdout io.Writer, workingDirectory string, args []string, extraEnvVars []string) error {
path, err := NewBinary().BinaryPath()
if err != nil {
return err
}
command := exec.Command(path, args...)
command.Dir = workingDirectory
command.Env = os.Environ()
command.Env = append(command.Env, extraEnvVars...)
command.Stdout = io.MultiWriter(stdout, c.outputBuffer)
command.Stderr = c.errorBuffer
err = command.Run()
if err != nil {
return fmt.Errorf("command execution failed got: %s stderr:\n %s", err, c.errorBuffer)
}
return nil
}