From fac68b0c09c87caae841b2ad64d4f981588b4c6e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 10 Jun 2014 11:30:54 -0700 Subject: [PATCH] terraform: match key lengths up in a diff --- terraform/diff.go | 8 +++++++- terraform/diff_test.go | 9 +++++++-- terraform/terraform_test.go | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/terraform/diff.go b/terraform/diff.go index 361b6eeb8..625f024aa 100644 --- a/terraform/diff.go +++ b/terraform/diff.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "sort" + "strings" "sync" ) @@ -37,9 +38,13 @@ func (d *Diff) String() string { buf.WriteString(name + "\n") + keyLen := 0 keys := make([]string, 0, len(rdiff.Attributes)) for key, _ := range rdiff.Attributes { keys = append(keys, key) + if len(key) > keyLen { + keyLen = len(key) + } } sort.Strings(keys) @@ -52,8 +57,9 @@ func (d *Diff) String() string { } buf.WriteString(fmt.Sprintf( - " %s: %#v => %#v\n", + " %s:%s %#v => %#v\n", attrK, + strings.Repeat(" ", keyLen-len(attrK)), attrDiff.Old, v)) } diff --git a/terraform/diff_test.go b/terraform/diff_test.go index 646fe761d..e5a2b6dd7 100644 --- a/terraform/diff_test.go +++ b/terraform/diff_test.go @@ -18,6 +18,10 @@ func TestDiff_String(t *testing.T) { Old: "foo", NewComputed: true, }, + "longfoo": &ResourceAttrDiff{ + Old: "foo", + New: "bar", + }, }, }, }, @@ -32,6 +36,7 @@ func TestDiff_String(t *testing.T) { const diffStrBasic = ` nodeA - bar: "foo" => "" - foo: "foo" => "bar" + bar: "foo" => "" + foo: "foo" => "bar" + longfoo: "foo" => "bar" ` diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 3bedcf3af..424486be8 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -401,6 +401,6 @@ const testTerraformDiffComputedStr = ` aws_instance.bar foo: "" => "" aws_instance.foo - id: "" => "" + id: "" => "" num: "" => "2" `