Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for count meta-variable #68

Merged
merged 5 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/blocks/blockreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
Expand Down Expand Up @@ -157,15 +156,15 @@ 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"
br.Reader = inStream
br.Writer = stdout

if br.ReadOnly {
br.Writer = ioutil.Discard
br.Writer = io.Discard
}
}

Expand Down Expand Up @@ -230,15 +229,15 @@ 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"
br.Reader = stdin
br.Writer = stdout

if br.ReadOnly {
br.Writer = ioutil.Discard
br.Writer = io.Discard
}
}

Expand Down
12 changes: 10 additions & 2 deletions lib/fmtverbs/fmtverbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@_@@#`)
Expand Down Expand Up @@ -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")
Expand Down
53 changes: 53 additions & 0 deletions lib/fmtverbs/fmtverbs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@_@@"
}
`,
},
}
Expand Down
7 changes: 3 additions & 4 deletions lib/upgrade012/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"strings"

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down