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

Improve quoteJQQuery reliability #205

Merged
merged 3 commits into from
Sep 17, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Test binary, built with `go test -c`
*.test

# Binary built with `go build`
cmd/tfstate-lookup/tfstate-lookup

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

Expand Down
37 changes: 25 additions & 12 deletions tfstate/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -242,24 +243,36 @@ func (s *TFState) Lookup(key string) (*Object, error) {
//
// quoteJQQuery does it.
func quoteJQQuery(query string) string {
if !strings.Contains(query, "-") {
return query
}
parts := strings.Split(query, ".")
var builder strings.Builder
splitRegex := regexp.MustCompile(`[.\[\]]`)
indexRegex := regexp.MustCompile(`^-?[0-9]+$`)
parts := splitRegex.Split(query, -1)
parts_coalesced := make([]string, 0, len(parts))

for _, part := range parts {
// Split(".outputs", ".") -> {"", "outputs"}
if part == "" {
continue
if part != "" {
parts_coalesced = append(parts_coalesced, part)
}
if strings.Contains(part, "-") {
builder.WriteString(`["`)
}

var builder strings.Builder
builder.WriteByte('.')

for _, part := range parts_coalesced {
builder.WriteByte('[')
if indexRegex.MatchString(part) {
builder.WriteString(part)
builder.WriteString(`"]`)
} else {
builder.WriteByte('.')
if !strings.HasPrefix(part, `"`) {
builder.WriteByte('"')
}

builder.WriteString(part)

if !strings.HasSuffix(part, `"`) {
builder.WriteByte('"')
}
}
builder.WriteByte(']')
}
return builder.String()
}
Expand Down
9 changes: 9 additions & 0 deletions tfstate/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
var TestNames = []string{
`output.bar`,
`output.foo`,
`output.dash-tuple`,
`data.aws_caller_identity.current`,
`aws_acm_certificate.main`,
`module.logs.aws_cloudwatch_log_group.main`,
Expand Down Expand Up @@ -182,6 +183,14 @@ var TestSuitesOK = []TestSuite{
Key: `data.terraform_remote_state.hyphenated-id.outputs.repository-uri`,
Result: `123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/app`,
},
{
Key: `output.dash-tuple[1]`,
Result: float64(2),
},
{
Key: `output.dash-tuple[-1]`,
Result: float64(1),
},
}

func testLookupState(t *testing.T, state *tfstate.TFState) {
Expand Down
15 changes: 15 additions & 0 deletions tfstate/test/terraform.tfstate
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
"string"
]
]
},
"dash-tuple": {
"value": [
3,
2,
1
],
"type": [
"tuple",
[
"number",
"number",
"number"
]
]
}
},
"resources": [
Expand Down
Loading