From 8f85e9ff0f0834f903ba9f596dc1850ec5d1978b Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Fri, 5 Aug 2022 17:01:57 -0700 Subject: [PATCH 1/5] Uses Terraform 0.12.31 for the `upgrade012` command --- lib/upgrade012/tfinstall.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/upgrade012/tfinstall.go b/lib/upgrade012/tfinstall.go index ae82755f..325db84e 100644 --- a/lib/upgrade012/tfinstall.go +++ b/lib/upgrade012/tfinstall.go @@ -13,7 +13,7 @@ import ( ) func InstallTerraform(ctx context.Context) (string, error) { - vc, _ := version.NewConstraint("=0.12.29") + vc, _ := version.NewConstraint("=0.12.31") rv := &releases.Versions{ Product: product.Terraform, Constraints: vc, From 697ea38b3d84fbaa57eca95473bc52b0bda5a8b3 Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Wed, 20 Oct 2021 11:50:38 -0700 Subject: [PATCH 2/5] Adds support for format verbs in resource names --- cli/blocks_test.go | 14 ++++++++++- cli/cmds.go | 2 +- cli/diff_test.go | 9 ++++--- cli/fmt_test.go | 9 ++++--- cli/testdata/fmt_compat.go | 12 +++++++++ cli/testdata/fmt_compat_fmtcompat.go | 12 +++++++++ cli/testdata/fmt_compat_upgrade012.go | 12 +++++++++ cli/upgrade012_test.go | 9 ++++--- lib/blocks/blockreader.go | 4 ++- lib/blocks/blockreader_test.go | 36 +++++++++++++++++++++++++++ lib/blocks/testdata/test1.go | 8 ++++++ lib/fmtverbs/fmtverbs.go | 18 ++++++++++++++ lib/fmtverbs/fmtverbs_test.go | 35 ++++++++++++++++++++++++++ 13 files changed, 165 insertions(+), 15 deletions(-) diff --git a/cli/blocks_test.go b/cli/blocks_test.go index 33325900..1a35885a 100644 --- a/cli/blocks_test.go +++ b/cli/blocks_test.go @@ -148,7 +148,7 @@ resource "aws_vpc" "test" { { name: "Go fmt verbs", sourcefile: "testdata/fmt_compat.go", - lineCount: 64, + lineCount: 76, expectedBlocks: []block{ { startLine: 8, @@ -207,6 +207,18 @@ resource "aws_vpc" "test" { replica_count = %[2]d } } +`, + }, + { + startLine: 67, + endLine: 75, + text: `resource "aws_s3_bucket" %[1]q { + bucket = "tf-test-bucket-with-quotedname" +} + +resource "aws_s3_bucket" "%[1]s-copy" { + bucket = "tf-test-bucket-with-name-in-quotes" +} `, }, }, diff --git a/cli/cmds.go b/cli/cmds.go index 6dc8858d..748ceed3 100644 --- a/cli/cmds.go +++ b/cli/cmds.go @@ -224,7 +224,7 @@ func Make() *cobra.Command { pflags := root.PersistentFlags() pflags.BoolP("fmtcompat", "f", false, "enable format string (%s, %d etc) compatibility") pflags.BoolP("check", "c", false, "return an error during diff if formatting is required") - pflags.BoolP("verbose", "v", false, "show files as they are processed& additional stats") + pflags.BoolP("verbose", "v", false, "show files as they are processed & additional stats") pflags.BoolP("quiet", "q", false, "quiet mode, only shows block line numbers ") pflags.BoolP("uncoloured", "u", false, "disable coloured output") diff --git a/cli/diff_test.go b/cli/diff_test.go index 6a7a262d..1e6ee646 100644 --- a/cli/diff_test.go +++ b/cli/diff_test.go @@ -48,18 +48,19 @@ var diffTestcases = []struct { "block 3 @ %s:30 failed to process with: failed to parse hcl: testdata/fmt_compat.go:4,3-4:", "block 4 @ %s:44 failed to process with: failed to parse hcl: testdata/fmt_compat.go:3,3-4:", "block 5 @ %s:53 failed to process with: failed to parse hcl: testdata/fmt_compat.go:2,26-27:", + "block 6 @ %s:67 failed to process with: failed to parse hcl: testdata/fmt_compat.go:1,26-27:", }, - lineCount: 64, - totalBlockCount: 5, + lineCount: 76, + totalBlockCount: 6, }, { name: "Go fmt verbs --fmtcompat", sourcefile: "testdata/fmt_compat.go", resultfile: "testdata/fmt_compat_diff_fmtcompat.go.txt", fmtcompat: true, - lineCount: 64, + lineCount: 76, unformattedBlockCount: 3, - totalBlockCount: 5, + totalBlockCount: 6, }, { name: "Go bad terraform", diff --git a/cli/fmt_test.go b/cli/fmt_test.go index 59bc1c67..7b26c4f8 100644 --- a/cli/fmt_test.go +++ b/cli/fmt_test.go @@ -57,18 +57,19 @@ var fmtTestcases = []struct { "block 3 @ %s:30 failed to process with: failed to parse hcl: %s:4,3-4:", "block 4 @ %s:44 failed to process with: failed to parse hcl: %s:3,3-4:", "block 5 @ %s:53 failed to process with: failed to parse hcl: %s:2,26-27:", + "block 6 @ %s:67 failed to process with: failed to parse hcl: %s:1,26-27:", }, - lineCount: 64, - totalBlockCount: 5, + lineCount: 76, + totalBlockCount: 6, }, { name: "Go fmt verbs --fmtcompat", sourcefile: "testdata/fmt_compat.go", resultfile: "testdata/fmt_compat_fmtcompat.go", fmtcompat: true, - lineCount: 64, + lineCount: 76, updatedBlockCount: 3, - totalBlockCount: 5, + totalBlockCount: 6, }, { name: "Go bad terraform", diff --git a/cli/testdata/fmt_compat.go b/cli/testdata/fmt_compat.go index 43b370a6..dfc92ff1 100644 --- a/cli/testdata/fmt_compat.go +++ b/cli/testdata/fmt_compat.go @@ -62,3 +62,15 @@ resource "aws_elasticache_replication_group" "for-expression" { } `, randInt) } + +func testFormatVerbResourceName(name string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" %[1]q { + bucket = "tf-test-bucket-with-quotedname" +} + +resource "aws_s3_bucket" "%[1]s-copy" { + bucket = "tf-test-bucket-with-name-in-quotes" +} +`, name) +} diff --git a/cli/testdata/fmt_compat_fmtcompat.go b/cli/testdata/fmt_compat_fmtcompat.go index 0dcd98ce..cbced97a 100644 --- a/cli/testdata/fmt_compat_fmtcompat.go +++ b/cli/testdata/fmt_compat_fmtcompat.go @@ -62,3 +62,15 @@ resource "aws_elasticache_replication_group" "for-expression" { } `, randInt) } + +func testFormatVerbResourceName(name string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" %[1]q { + bucket = "tf-test-bucket-with-quotedname" +} + +resource "aws_s3_bucket" "%[1]s-copy" { + bucket = "tf-test-bucket-with-name-in-quotes" +} +`, name) +} diff --git a/cli/testdata/fmt_compat_upgrade012.go b/cli/testdata/fmt_compat_upgrade012.go index d72dcdf2..4dc70cc7 100644 --- a/cli/testdata/fmt_compat_upgrade012.go +++ b/cli/testdata/fmt_compat_upgrade012.go @@ -62,3 +62,15 @@ resource "aws_elasticache_replication_group" "for-expression" { } `, randInt) } + +func testFormatVerbResourceName(name string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" %[1]q { + bucket = "tf-test-bucket-with-quotedname" +} + +resource "aws_s3_bucket" "%[1]s-copy" { + bucket = "tf-test-bucket-with-name-in-quotes" +} +`, name) +} diff --git a/cli/upgrade012_test.go b/cli/upgrade012_test.go index d70889dc..06773e62 100644 --- a/cli/upgrade012_test.go +++ b/cli/upgrade012_test.go @@ -47,9 +47,10 @@ var upgradeTestcases = []struct { "block 3 @ %s:30 failed to process with: terraform init failed:", "block 4 @ %s:44 failed to process with: terraform init failed:", "block 5 @ %s:53 failed to process with: terraform init failed:", + "block 6 @ %s:67 failed to process with: terraform init failed:", }, - lineCount: 64, - totalBlockCount: 5, + lineCount: 76, + totalBlockCount: 6, }, { name: "Go fmt verbs --fmtcompat", @@ -62,9 +63,9 @@ var upgradeTestcases = []struct { // for-expressions were added in Terraform 0.12 "block 5 @ %s:53 failed to process with: terraform 0.12upgrade failed:", }, - lineCount: 64, + lineCount: 76, updatedBlockCount: 1, - totalBlockCount: 5, + totalBlockCount: 6, }, { name: "Go bad terraform", diff --git a/lib/blocks/blockreader.go b/lib/blocks/blockreader.go index 3c808125..ff67a9e6 100644 --- a/lib/blocks/blockreader.go +++ b/lib/blocks/blockreader.go @@ -130,7 +130,9 @@ func (bv blockVisitor) Visit(cursor *astutil.Cursor) bool { return true } -var terraformMatcher = regexp.MustCompile(`(((resource|data)\s+"[-a-z0-9_]+")|(variable|output))\s+"[-a-zA-Z0-9_]+"\s+\{`) +// Includes matching Go format verbs in the resource, data source, variable, or output name. +// Technically, this is only valid for the Go matcher, but included generally for simplicity. +var terraformMatcher = regexp.MustCompile(`(((resource|data)\s+"[-a-z0-9_]+")|(variable|output))\s+"[-a-z0-9_%\[\]]+"\s+\{`) // A simple check to see if the content looks like a Terraform configuration. // Looks for a line with either a resource, data source, variable, or output declaration diff --git a/lib/blocks/blockreader_test.go b/lib/blocks/blockreader_test.go index cf940141..edc460f6 100644 --- a/lib/blocks/blockreader_test.go +++ b/lib/blocks/blockreader_test.go @@ -94,6 +94,14 @@ func TestBlockDetection(t *testing.T) { resource "aws_s3_bucket" "leading-space-and-line" { bucket = "tf-test-bucket-leading-space-and-line-%d" } +`, + }, + { + leadingPadding: "\n", + trailingPadding: "\n", + text: `resource "aws_s3_bucket" "%s" { + bucket = "tf-test-bucket-with-quotedname" +} `, }, }, @@ -212,6 +220,34 @@ output "arn" { }`, expected: true, }, + { + text: ` +resource "aws_s3_bucket" "%s" { + bucket = "tf-test-bucket-simple" +}`, + expected: true, + }, + // { + // text: ` + // resource "aws_s3_bucket" "%[1]s" { + // bucket = "tf-test-bucket-simple" + // }`, + // expected: true, + // }, + // { + // text: ` + // resource "aws_s3_bucket" %q { + // bucket = "tf-test-bucket-simple" + // }`, + // expected: true, + // }, + // { + // text: ` + // resource "aws_s3_bucket" %[1]q { + // bucket = "tf-test-bucket-simple" + // }`, + // expected: true, + // }, { text: "%d: bad create: \n%#v\n%#v", expected: false, diff --git a/lib/blocks/testdata/test1.go b/lib/blocks/testdata/test1.go index 242a9ba1..baeca089 100644 --- a/lib/blocks/testdata/test1.go +++ b/lib/blocks/testdata/test1.go @@ -75,6 +75,14 @@ func testLeadingWhiteSpaceAndLine(randInt int) string { `, randInt) } +func testFormatVerbResourceName(name string) string { + return fmt.Sprintf(` +resource "aws_s3_bucket" "%s" { + bucket = "tf-test-bucket-with-quotedname" +} +`, name) +} + func notTerraformSimpleString() string { fmt.Sprintf("%d: bad create: \n%#v\n%#v", i, cm, tc.Create) } diff --git a/lib/fmtverbs/fmtverbs.go b/lib/fmtverbs/fmtverbs.go index 46c3724f..5f99ab3c 100644 --- a/lib/fmtverbs/fmtverbs.go +++ b/lib/fmtverbs/fmtverbs.go @@ -11,6 +11,12 @@ func Escape(b string) string { // conditional expression: = %t ? ... b = regexp.MustCompile(`(=\s*)(%\[[\d+]\]t)(\s\?)`).ReplaceAllString(b, `${1}true/*@@_@@ TFMT:${2}:TFMT @@_@@*/${3}`) + // resource name %s + b = regexp.MustCompile(`([resource|data]\s+"[-a-zA-Z0-9_]+"\s+"[-a-zA-Z0-9_]*)%(?:\[([\d]+)\])?s`).ReplaceAllString(b, `${1}TFMTRESNAME_${2}s`) + + // resource name %q + b = regexp.MustCompile(`([resource|data]\s+"[-a-zA-Z0-9_]+"\s+)%(?:\[([\d]+)\])?q`).ReplaceAllString(b, `${1}"TFMTRESNAME_${2}q"`) + // %s - whole line // figure out why the * doesn't match both later b = regexp.MustCompile(`(?m:^%(\.[0-9])?[sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) @@ -95,5 +101,17 @@ func Unscape(fb string) string { // %s = fb = regexp.MustCompile(`Ω([sdfgtq])`).ReplaceAllString(fb, `%${1}`) + // resource name %[n]s + fb = regexp.MustCompile(`TFMTRESNAME_(\d+)s`).ReplaceAllString(fb, `%[$1]s`) + + // resource name %s + fb = regexp.MustCompile(`TFMTRESNAME_s`).ReplaceAllString(fb, `%s`) + + // resource name %[n]s + fb = regexp.MustCompile(`"TFMTRESNAME_(\d+)q"`).ReplaceAllString(fb, `%[$1]q`) + + // resource name %s + fb = regexp.MustCompile(`"TFMTRESNAME_q"`).ReplaceAllString(fb, `%q`) + return fb } diff --git a/lib/fmtverbs/fmtverbs_test.go b/lib/fmtverbs/fmtverbs_test.go index 2e792308..7b6dee55 100644 --- a/lib/fmtverbs/fmtverbs_test.go +++ b/lib/fmtverbs/fmtverbs_test.go @@ -518,6 +518,41 @@ resource "resource" "test" { attr = [for x in range(TFMTFNPARAM_d, TFMTFNPARAM_d) : element(aws_subnet.test[*].availability_zone, x)] attr = [for x in range(TFMTFNPARAM_1d, TFMTFNPARAM_2d) : element(aws_subnet.test[*].availability_zone, x)] } +`, + }, + { + name: "verb in resource name", + block: ` +resource "resource" "%s" { +} + +resource "resource" "test-%[1]s" { +} + +resource "resource" "%s-test" { +} + +data "data_source" %[1]q { +} + +resource "resource" %q { +} +`, + expected: ` +resource "resource" "TFMTRESNAME_s" { +} + +resource "resource" "test-TFMTRESNAME_1s" { +} + +resource "resource" "TFMTRESNAME_s-test" { +} + +data "data_source" "TFMTRESNAME_1q" { +} + +resource "resource" "TFMTRESNAME_q" { +} `, }, } From fab03c21c757e2bbd86c4e447850d2f26e4f8d50 Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Fri, 22 Oct 2021 11:48:52 -0700 Subject: [PATCH 3/5] Adds support for partially-verbed resource references --- lib/fmtverbs/fmtverbs.go | 14 ++++++++++---- lib/fmtverbs/fmtverbs_test.go | 20 ++++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/fmtverbs/fmtverbs.go b/lib/fmtverbs/fmtverbs.go index 5f99ab3c..a14ddc9b 100644 --- a/lib/fmtverbs/fmtverbs.go +++ b/lib/fmtverbs/fmtverbs.go @@ -42,10 +42,16 @@ func Escape(b string) string { b = regexp.MustCompile(`(?m:\[(%(\.[0-9])?\[[\d]+\][sdfgtq](,\s*)?)+\])`).ReplaceAllString(b, `["@@_@@ TFMT:$0:TFMT @@_@@"]`) // .12 - something.%s.prop - b = regexp.MustCompile(`\.%([sdfgtq])`).ReplaceAllString(b, `.TFMTKTKTTFMT$1`) + b = regexp.MustCompile(`\.%([sdt])`).ReplaceAllString(b, `.TFMTKTKTTFMT${1}`) // .12 - something.%[n]s.prop - b = regexp.MustCompile(`\.%\[(\d+)\]([sdfgtq])`).ReplaceAllString(b, `.TFMTKTKTTFMT_$1$2`) + b = regexp.MustCompile(`\.%\[(\d+)\]([sdt])`).ReplaceAllString(b, `.TFMTKTKTTFMT_${1}${2}`) + + // .12 - something.text%s.prop + b = regexp.MustCompile(`\.([-a-zA-Z0-9_]+)%([sdt])`).ReplaceAllString(b, `.${1}TFMTKTKTTFMT${2}`) + + // .12 - something.text%[n]s.prop + b = regexp.MustCompile(`\.([-a-zA-Z0-9_]+)%\[(\d+)\]([sdt])`).ReplaceAllString(b, `.${1}TFMTKTKTTFMT_${2}${3}`) // = %s b = regexp.MustCompile(`(?m:%(\.[0-9])?[sdfgtq](\.[a-z_]+)*$)`).ReplaceAllString(b, `"@@_@@ TFMT:$0:TFMT @@_@@"`) @@ -84,10 +90,10 @@ func Unscape(fb string) string { fb = strings.ReplaceAll(fb, ":TFMT @@_@@\"", "") // .12 - something.%[n]s.prop - fb = regexp.MustCompile(`\.TFMTKTKTTFMT_(\d+)([sdfgtq])`).ReplaceAllString(fb, `.%[$1]$2`) + fb = regexp.MustCompile(`TFMTKTKTTFMT_(\d+)`).ReplaceAllString(fb, `%[$1]`) // .12 - something.%s.prop - fb = strings.ReplaceAll(fb, ".TFMTKTKTTFMT", ".%") + fb = strings.ReplaceAll(fb, "TFMTKTKTTFMT", "%") // function(..., %[n]s, ...) fb = regexp.MustCompile(`TFMTFNPARAM_(\d+)([sdfgtq])`).ReplaceAllString(fb, `%[$1]$2`) diff --git a/lib/fmtverbs/fmtverbs_test.go b/lib/fmtverbs/fmtverbs_test.go index 7b6dee55..5b654a37 100644 --- a/lib/fmtverbs/fmtverbs_test.go +++ b/lib/fmtverbs/fmtverbs_test.go @@ -245,20 +245,14 @@ resource "resource" "test" { resource "resource" "test" { s = data.source.%s.id d = data.source.%d.id - f = data.source.%f.id - g = data.source.%g.id t = data.source.%t.id - q = data.source.%q.id } `, expected: ` resource "resource" "test" { s = data.source.TFMTKTKTTFMTs.id d = data.source.TFMTKTKTTFMTd.id - f = data.source.TFMTKTKTTFMTf.id - g = data.source.TFMTKTKTTFMTg.id t = data.source.TFMTKTKTTFMTt.id - q = data.source.TFMTKTKTTFMTq.id } `, }, @@ -268,20 +262,14 @@ resource "resource" "test" { resource "resource" "test" { s = data.source.%[1]s.id d = data.source.%[2]d.id - f = data.source.%[3]f.id - g = data.source.%[4]g.id t = data.source.%[5]t.id - q = data.source.%[6]q.id } `, expected: ` resource "resource" "test" { s = data.source.TFMTKTKTTFMT_1s.id d = data.source.TFMTKTKTTFMT_2d.id - f = data.source.TFMTKTKTTFMT_3f.id - g = data.source.TFMTKTKTTFMT_4g.id t = data.source.TFMTKTKTTFMT_5t.id - q = data.source.TFMTKTKTTFMT_6q.id } `, }, @@ -524,34 +512,42 @@ resource "resource" "test" { name: "verb in resource name", block: ` resource "resource" "%s" { + kat = resource.test-%s.byte } resource "resource" "test-%[1]s" { + kat = resource.%s-test.byte } resource "resource" "%s-test" { + kat = resource.%s.byte } data "data_source" %[1]q { } resource "resource" %q { + kat = resource.%[1]s.byte } `, expected: ` resource "resource" "TFMTRESNAME_s" { + kat = resource.test-TFMTKTKTTFMTs.byte } resource "resource" "test-TFMTRESNAME_1s" { + kat = resource.TFMTKTKTTFMTs-test.byte } resource "resource" "TFMTRESNAME_s-test" { + kat = resource.TFMTKTKTTFMTs.byte } data "data_source" "TFMTRESNAME_1q" { } resource "resource" "TFMTRESNAME_q" { + kat = resource.TFMTKTKTTFMT_1s.byte } `, }, From 9d962a1a46cdbd81f414afdf60a09e38ea58cbef Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Fri, 22 Oct 2021 13:15:42 -0700 Subject: [PATCH 4/5] Handles more verbs in resource names --- lib/fmtverbs/fmtverbs.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/fmtverbs/fmtverbs.go b/lib/fmtverbs/fmtverbs.go index a14ddc9b..894d3161 100644 --- a/lib/fmtverbs/fmtverbs.go +++ b/lib/fmtverbs/fmtverbs.go @@ -11,8 +11,8 @@ func Escape(b string) string { // conditional expression: = %t ? ... b = regexp.MustCompile(`(=\s*)(%\[[\d+]\]t)(\s\?)`).ReplaceAllString(b, `${1}true/*@@_@@ TFMT:${2}:TFMT @@_@@*/${3}`) - // resource name %s - b = regexp.MustCompile(`([resource|data]\s+"[-a-zA-Z0-9_]+"\s+"[-a-zA-Z0-9_]*)%(?:\[([\d]+)\])?s`).ReplaceAllString(b, `${1}TFMTRESNAME_${2}s`) + // resource name contains %[sdtfg] + b = regexp.MustCompile(`([resource|data]\s+"[-a-zA-Z0-9_]+"\s+"[-a-zA-Z0-9_]*)%(?:\[([\d]+)\])?([sdtfg])`).ReplaceAllString(b, `${1}TFMTRESNAME_${2}${3}`) // resource name %q b = regexp.MustCompile(`([resource|data]\s+"[-a-zA-Z0-9_]+"\s+)%(?:\[([\d]+)\])?q`).ReplaceAllString(b, `${1}"TFMTRESNAME_${2}q"`) @@ -108,15 +108,15 @@ func Unscape(fb string) string { fb = regexp.MustCompile(`Ω([sdfgtq])`).ReplaceAllString(fb, `%${1}`) // resource name %[n]s - fb = regexp.MustCompile(`TFMTRESNAME_(\d+)s`).ReplaceAllString(fb, `%[$1]s`) + fb = regexp.MustCompile(`TFMTRESNAME_(\d+)([sdtfg])`).ReplaceAllString(fb, `%[${1}]${2}`) // resource name %s - fb = regexp.MustCompile(`TFMTRESNAME_s`).ReplaceAllString(fb, `%s`) + fb = regexp.MustCompile(`TFMTRESNAME_([sdtfg])`).ReplaceAllString(fb, `%${1}`) - // resource name %[n]s + // resource name %[n]q fb = regexp.MustCompile(`"TFMTRESNAME_(\d+)q"`).ReplaceAllString(fb, `%[$1]q`) - // resource name %s + // resource name %q fb = regexp.MustCompile(`"TFMTRESNAME_q"`).ReplaceAllString(fb, `%q`) return fb From 9a9f3eca551034bbc3e178481ab0923c6dc3baff Mon Sep 17 00:00:00 2001 From: Graham Davison Date: Mon, 25 Oct 2021 12:48:15 -0700 Subject: [PATCH 5/5] Adds support for `provider` meta-argument special case --- lib/fmtverbs/fmtverbs.go | 13 +++++++++++-- lib/fmtverbs/fmtverbs_test.go | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/fmtverbs/fmtverbs.go b/lib/fmtverbs/fmtverbs.go index 894d3161..99ceb6dc 100644 --- a/lib/fmtverbs/fmtverbs.go +++ b/lib/fmtverbs/fmtverbs.go @@ -22,6 +22,11 @@ func Escape(b string) string { b = regexp.MustCompile(`(?m:^%(\.[0-9])?[sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) b = regexp.MustCompile(`(?m:^[ \t]*%(\.[0-9])?[sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) + // provider meta-argument + // The provider name must be in lowercase + b = regexp.MustCompile(`(provider\s+=\s+)%s`).ReplaceAllString(b, `${1}tfmtprovider.PROVIDER`) + b = regexp.MustCompile(`(provider\s+=\s+)+%\[(\d+)\]s`).ReplaceAllString(b, `${1}tfmtprovider.PROVIDER_${2}`) + // %[n]s b = regexp.MustCompile(`(?m:^%(\.[0-9])?\[[\d]+\][sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) b = regexp.MustCompile(`(?m:^[ \t]*%(\.[0-9])?\[[\d]+\][sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) @@ -114,10 +119,14 @@ func Unscape(fb string) string { fb = regexp.MustCompile(`TFMTRESNAME_([sdtfg])`).ReplaceAllString(fb, `%${1}`) // resource name %[n]q - fb = regexp.MustCompile(`"TFMTRESNAME_(\d+)q"`).ReplaceAllString(fb, `%[$1]q`) + fb = regexp.MustCompile(`"TFMTRESNAME_(\d+)q"`).ReplaceAllString(fb, `%[${1}]q`) // resource name %q - fb = regexp.MustCompile(`"TFMTRESNAME_q"`).ReplaceAllString(fb, `%q`) + fb = regexp.MustCompile(`"TFMTRESNAME_q"`).ReplaceAllLiteralString(fb, `%q`) + + // provider meta-argument + fb = regexp.MustCompile(`tfmtprovider.PROVIDER_(\d+)`).ReplaceAllString(fb, `%[${1}]s`) + fb = strings.ReplaceAll(fb, "tfmtprovider.PROVIDER", "%s") return fb } diff --git a/lib/fmtverbs/fmtverbs_test.go b/lib/fmtverbs/fmtverbs_test.go index 5b654a37..0130b71c 100644 --- a/lib/fmtverbs/fmtverbs_test.go +++ b/lib/fmtverbs/fmtverbs_test.go @@ -549,6 +549,27 @@ data "data_source" "TFMTRESNAME_1q" { resource "resource" "TFMTRESNAME_q" { kat = resource.TFMTKTKTTFMT_1s.byte } +`, + }, + { + name: "provider meta-argument", + block: ` +resource "resource" "test" { + provider = %s +} + +resource "resource" "test2" { + provider = %[1]s +} +`, + expected: ` +resource "resource" "test" { + provider = tfmtprovider.PROVIDER +} + +resource "resource" "test2" { + provider = tfmtprovider.PROVIDER_1 +} `, }, }