-
Notifications
You must be signed in to change notification settings - Fork 8
/
terraform.go
82 lines (72 loc) · 1.76 KB
/
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
go_version "github.com/hashicorp/go-version"
)
type resChanges struct {
ResChanges []ResChange `json:"resource_changes"`
}
// ResChange represents a resource change in a Terraform plan.
type ResChange struct {
Address string
Type string
Change Change
}
// Change represents a list of actions to one resource in a Terraform plan.
type Change struct {
Actions []changeAction
}
type changeAction string
const (
noOp changeAction = "no-op"
create changeAction = "create"
update changeAction = "update"
del changeAction = "delete"
)
// Resource represents a Terraform resource and consists of a type and an address.
type Resource struct {
Address string
Type string
}
func terraformExec(cfg config, executeInDryRun bool, args []string, extraArgs ...string) error {
args = append(extraArgs, args...)
if cfg.dryrun && !executeInDryRun {
fmt.Println("Dry-run, would have called: terraform", strings.Join(args, " "))
return nil
}
if cfg.verbose {
fmt.Println("Calling: terraform", strings.Join(args, " "))
}
cmd := exec.Command("terraform", args...)
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"TF_INPUT=false",
)
return cmd.Run()
}
func isPre012() (bool, error) {
cmd := exec.Command("terraform", "version")
cmdOutput := &bytes.Buffer{}
cmd.Stdout = cmdOutput
err := cmd.Run()
if err != nil {
return false, err
}
output := cmdOutput.Bytes()
var ver = regexp.MustCompile(`Terraform v(\d+\.\d+\.\d+)`)
result := ver.FindStringSubmatch(string(output))
v012, err := go_version.NewVersion("0.12")
if err != nil {
return false, err
}
current, err := go_version.NewVersion(result[1])
if err != nil {
return false, err
}
return current.LessThan(v012), nil
}