-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (49 loc) · 1.31 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/terraform-exec/tfexec"
)
func main() {
installer := &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.0.6")),
}
fmt.Println("Installing Terraform...")
execPath, err := installer.Install(context.Background())
if err != nil {
log.Fatalf("error installing Terraform: %s", err)
}
fmt.Println("Running Terraform...")
workingDir := "./"
tf, err := tfexec.NewTerraform(workingDir, execPath)
if err != nil {
log.Fatalf("error running NewTerraform: %s", err)
}
fmt.Println("Init Terraform...")
err = tf.Init(context.Background(), tfexec.Upgrade(true))
if err != nil {
log.Fatalf("error running Init: %s", err)
}
// state, err := tf.Show(context.Background())
// if err != nil {
// log.Fatalf("error running Show: %s", err)
// }
fmt.Println("Plan Terraform...")
plan, err := tf.Plan(context.Background())
if err != nil {
log.Fatalf("error plan tf: %s", err)
}
if !plan {
log.Fatalf("unsuccessful plan tf")
}
fmt.Println("Applying and Execute Terraform...")
err = tf.Apply(context.Background())
if err != nil {
log.Fatalf("error apply tf: %s", err)
}
}