Skip to content

Commit

Permalink
Pass args to terraform plan
Browse files Browse the repository at this point in the history
  • Loading branch information
mbode committed Aug 6, 2019
1 parent fc3323d commit e27f7af
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ go get github.com/mbode/terraform-state-mover
terraform-state-mover # inside a Terraform root directory
```

Extra arguments are passed to the `terraform plan` call. This makes the following possible:
```bash
terraform-state-mover -var key=value # setting variables
terraform-state-mover -var-file=variables.tfvars # using variable files
```

## Demo

![](demo.gif)
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
)

func main() {
args := os.Args[1:]

dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

changes := changes(dir)
changes := changes(dir, args)
srcs := filter(changes, del)

if len(srcs) == 0 {
Expand Down
8 changes: 4 additions & 4 deletions mover.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import "log"
import (
"log"
)

func move(dir string, from Resource, to Resource) {
log.Printf("Moving %v to %v.\n", from.Address, to.Address)

if err := terraformExec(dir, "state", "mv", from.Address, to.Address); err != nil {
if err := terraformExec(dir, []string{}, "state", "mv", from.Address, to.Address); err != nil {
log.Panicf("terraform state mv failed with %s\n", err)
}
}
2 changes: 1 addition & 1 deletion mover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ resource "null_resource" "second" {}`
{"null_resource.new", "null_resource", Change{[]changeAction{noOp}}},
{"null_resource.second", "null_resource", Change{[]changeAction{noOp}}},
}
if got := changes(dir); !reflect.DeepEqual(got, want) {
if got := changes(dir, []string{}); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
4 changes: 2 additions & 2 deletions planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"reflect"
)

func changes(dir string) []ResChange {
func changes(dir string, args []string) []ResChange {
tfPlan, err := ioutil.TempFile(dir, "tfplan")
if err != nil {
log.Panicf("ioutil.TempFile() failed with %s\n", err)
}
tfPlanName := tfPlan.Name()
defer os.Remove(tfPlanName)

if err := terraformExec(dir, "plan", "-out="+tfPlanName); err != nil {
if err := terraformExec(dir, args, "plan", "-out="+tfPlanName); err != nil {
log.Panicf("terraform plan failed with %s\n", err)
}

Expand Down
12 changes: 6 additions & 6 deletions planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ resource "null_resource" "second" {}`
t.Fatal(err)
}

if err := terraformExec(dir, "init"); err != nil {
if err := terraformExec(dir, []string{}, "init"); err != nil {
t.Fatalf("terraform init failed with %s\n", err)
}

want := []ResChange{
{"null_resource.first", "null_resource", Change{[]changeAction{create}}},
{"null_resource.second", "null_resource", Change{[]changeAction{create}}},
}
if got := changes(dir); !reflect.DeepEqual(got, want) {
if got := changes(dir, []string{}); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
Expand All @@ -46,7 +46,7 @@ resource "null_resource" "second" {}`
{"null_resource.first", "null_resource", Change{[]changeAction{del}}},
{"null_resource.second", "null_resource", Change{[]changeAction{del}}},
}
if got := changes(dir); !reflect.DeepEqual(got, want) {
if got := changes(dir, []string{}); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
Expand All @@ -67,7 +67,7 @@ resource "null_resource" "second" {}`
{"null_resource.first", "null_resource", Change{[]changeAction{noOp}}},
{"null_resource.second", "null_resource", Change{[]changeAction{noOp}}},
}
if got := changes(dir); !reflect.DeepEqual(got, want) {
if got := changes(dir, []string{}); !reflect.DeepEqual(got, want) {
t.Errorf("changes() = %q, want %q", got, want)
}
}
Expand Down Expand Up @@ -103,10 +103,10 @@ func prepareState(dir string, content string, t *testing.T) {
if err := ioutil.WriteFile(dir+"/main.tf", []byte(content), 0644); err != nil {
t.Fatal(err)
}
if err := terraformExec(dir, "init"); err != nil {
if err := terraformExec(dir, []string{}, "init"); err != nil {
t.Fatal(err)
}
if err := terraformExec(dir, "apply", "-auto-approve"); err != nil {
if err := terraformExec(dir, []string{}, "apply", "-auto-approve"); err != nil {
t.Fatal(err)
}
}
6 changes: 5 additions & 1 deletion terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ type Resource struct {
Type string
}

func terraformExec(dir string, args ...string) error {
func terraformExec(dir string, args []string, extraArgs ...string) error {
args = append(extraArgs, args...)
cmd := exec.Command("terraform", args...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(),
"TF_INPUT=false",
)
return cmd.Run()
}

0 comments on commit e27f7af

Please sign in to comment.