-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform.go
40 lines (34 loc) · 998 Bytes
/
terraform.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
package terraform
import (
"fmt"
"path"
)
func terraformExec(clusterDir string, args ...string) error {
// Create an executor
ex, err := newExecutor()
if err != nil {
return fmt.Errorf("could not create Terraform executor: %s", err)
}
err = ex.execute(clusterDir, args...)
if err != nil {
return fmt.Errorf("failed to run Terraform: %s", err)
}
return nil
}
// Apply runs "terraform apply" in the given directory. It returns the absolute
// path of the tfstate file, rooted in the specified directory, along with any
// errors from Terraform.
func Apply(dir string, extraArgs ...string) (string, error) {
stateFileName := "terraform.tfstate"
defaultArgs := []string{
"apply",
"-auto-approve",
fmt.Sprintf("-state=%s", stateFileName),
}
args := append(defaultArgs, extraArgs...)
return path.Join(dir, stateFileName), terraformExec(dir, args...)
}
// Init runs "terraform init" in the given directory.
func Init(dir string) error {
return terraformExec(dir, "init")
}