-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_executor.go
67 lines (56 loc) · 1.89 KB
/
project_executor.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package orchestration
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/atorrescogollo/terraform-cascade/internal/project"
"github.com/atorrescogollo/terraform-cascade/internal/terraform"
)
type terraformExecutorFn = func(execDir string, args []string, stdout io.Writer, sterr io.Writer, stdin io.Reader) *exec.ExitError
// ProjectExecutor is the executor for terraform commands
// given a list of projects
type ProjectExecutor struct {
terraformExecFn terraformExecutorFn
stdout io.Writer
stderr io.Writer
stdin io.Reader
}
// NewTerraformProjectExecutorWithOS ProjectExecutor constructor with terraform and default OS streams
func NewTerraformProjectExecutorWithOS() *ProjectExecutor {
return NewProjectExecutor(terraform.Exec, os.Stdout, os.Stderr, os.Stdin)
}
// NewTerraformProjectExecutor ProjectExecutor constructor with terraform
func NewTerraformProjectExecutor(stdout io.Writer, stderr io.Writer, stdin io.Reader) *ProjectExecutor {
return NewProjectExecutor(terraform.Exec, stdout, stderr, stdin)
}
// NewProjectExecutor ProjectExecutor constructor
func NewProjectExecutor(terraformExecFn terraformExecutorFn, stdout io.Writer, stderr io.Writer, stdin io.Reader) *ProjectExecutor {
return &ProjectExecutor{
terraformExecFn,
stdout,
stderr,
stdin,
}
}
// Execute executes terraform in each project
func (p ProjectExecutor) Execute(projects []project.TerraformProject, args []string) error {
for _, project := range projects {
p.stdout.Write([]byte(`
===============[ Running terraform in ` + project.RelativePath + ` ]===============
`))
exitErr := p.terraformExecFn(
filepath.Join(project.BaseDir, project.RelativePath),
args,
p.stdout,
p.stderr,
p.stdin,
)
if exitErr != nil && exitErr.ExitCode() != 0 {
fmt.Println("Error running terraform in", project.BaseDir, ":", exitErr)
return exitErr
}
}
return nil
}