diff --git a/lib/blocks/blockreader.go b/lib/blocks/blockreader.go index 0fa651e9..13d42f9c 100644 --- a/lib/blocks/blockreader.go +++ b/lib/blocks/blockreader.go @@ -9,7 +9,6 @@ import ( "go/parser" "go/token" "io" - "io/ioutil" "path/filepath" "regexp" "strconv" @@ -157,7 +156,7 @@ func (br *Reader) DoTheThing(fs afero.Fs, filename string, stdin io.Reader, stdo if !br.ReadOnly { br.Writer = buf } else { - br.Writer = ioutil.Discard + br.Writer = io.Discard } } else { br.FileName = "stdin" @@ -165,7 +164,7 @@ func (br *Reader) DoTheThing(fs afero.Fs, filename string, stdin io.Reader, stdo br.Writer = stdout if br.ReadOnly { - br.Writer = ioutil.Discard + br.Writer = io.Discard } } @@ -230,7 +229,7 @@ func (br *Reader) doTheThingPatternMatch(fs afero.Fs, filename string, stdin io. buf = bytes.NewBuffer([]byte{}) br.Writer = buf } else { - br.Writer = ioutil.Discard + br.Writer = io.Discard } } else { br.FileName = "stdin" @@ -238,7 +237,7 @@ func (br *Reader) doTheThingPatternMatch(fs afero.Fs, filename string, stdin io. br.Writer = stdout if br.ReadOnly { - br.Writer = ioutil.Discard + br.Writer = io.Discard } } diff --git a/lib/fmtverbs/fmtverbs.go b/lib/fmtverbs/fmtverbs.go index 99ceb6dc..ec777aa9 100644 --- a/lib/fmtverbs/fmtverbs.go +++ b/lib/fmtverbs/fmtverbs.go @@ -24,8 +24,12 @@ func Escape(b string) string { // 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}`) + b = regexp.MustCompile(`(\bprovider\s+=\s+)%s`).ReplaceAllString(b, `${1}tfmtprovider.PROVIDER`) + b = regexp.MustCompile(`(\bprovider\s+=\s+)+%\[(\d+)\]s`).ReplaceAllString(b, `${1}tfmtprovider.PROVIDER_${2}`) + + // count meta-argument + b = regexp.MustCompile(`(\bcount\s+=\s+)%([ds])`).ReplaceAllString(b, `${1}1 # tfmtcount_${2}`) + b = regexp.MustCompile(`(\bcount\s+=\s+)+%(\[(\d+)\][ds])`).ReplaceAllString(b, `${1}1 # tfmtcount_${2}`) // %[n]s b = regexp.MustCompile(`(?m:^%(\.[0-9])?\[[\d]+\][sdfgtq]$)`).ReplaceAllString(b, `#@@_@@ TFMT:$0:TMFT @@_@@#`) @@ -124,6 +128,10 @@ func Unscape(fb string) string { // resource name %q fb = regexp.MustCompile(`"TFMTRESNAME_q"`).ReplaceAllLiteralString(fb, `%q`) + // count meta-argument + fb = regexp.MustCompile(`1\s+# tfmtcount_(\[\d+\][ds])`).ReplaceAllString(fb, `%${1}`) + fb = regexp.MustCompile(`1\s+# tfmtcount_([ds])`).ReplaceAllString(fb, `%${1}`) + // provider meta-argument fb = regexp.MustCompile(`tfmtprovider.PROVIDER_(\d+)`).ReplaceAllString(fb, `%[${1}]s`) fb = strings.ReplaceAll(fb, "tfmtprovider.PROVIDER", "%s") diff --git a/lib/fmtverbs/fmtverbs_test.go b/lib/fmtverbs/fmtverbs_test.go index 0130b71c..e865a99e 100644 --- a/lib/fmtverbs/fmtverbs_test.go +++ b/lib/fmtverbs/fmtverbs_test.go @@ -570,6 +570,59 @@ resource "resource" "test" { resource "resource" "test2" { provider = tfmtprovider.PROVIDER_1 } +`, + }, + { + name: "count meta-argument", + block: ` +resource "resource" "test" { + count = %d +} + +resource "resource" "test2" { + count = %[2]d +} + +resource "resource" "test3" { + count = %s +} + +resource "resource" "test4" { + count = %[3]s +} + +resource "other_resource" "test5" { + replica_count = %d +} + +resource "other_resource" "test6" { + replica_count = %[2]d +} +`, + expected: ` +resource "resource" "test" { + count = 1 # tfmtcount_d +} + +resource "resource" "test2" { + count = 1 # tfmtcount_[2]d +} + +resource "resource" "test3" { + count = 1 # tfmtcount_s +} + +resource "resource" "test4" { + count = 1 # tfmtcount_[3]s +} + +resource "other_resource" "test5" { + replica_count = "@@_@@ TFMT:%d:TFMT @@_@@" +} + +resource "other_resource" "test6" { + replica_count = "@@_@@ TFMT:%[2]d:TFMT @@_@@" +} `, }, } diff --git a/lib/upgrade012/upgrade.go b/lib/upgrade012/upgrade.go index 762827bd..5546ff1e 100644 --- a/lib/upgrade012/upgrade.go +++ b/lib/upgrade012/upgrade.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "strings" @@ -14,13 +13,13 @@ import ( func Block(ctx context.Context, tfPath string, log *logrus.Logger, b string) (string, error) { // Make temp directory - tempDir, err := ioutil.TempDir(".", "tmp-module") + tempDir, err := os.MkdirTemp(".", "tmp-module") if err != nil { log.Fatal(err) } // Create temp file - tmpFile, err := ioutil.TempFile(tempDir, "*.tf") + tmpFile, err := os.CreateTemp(tempDir, "*.tf") if err != nil { return "", err } @@ -55,7 +54,7 @@ func Block(ctx context.Context, tfPath string, log *logrus.Logger, b string) (st } // Read from temp file - raw, err := ioutil.ReadFile(tmpFile.Name()) + raw, err := os.ReadFile(tmpFile.Name()) if err != nil { return "", fmt.Errorf("failed to read %s: %w", tmpFile.Name(), err) }