Skip to content

Commit

Permalink
Merge pull request #205 from qgrp-m-kondratenko/main
Browse files Browse the repository at this point in the history
Improve quoteJQQuery reliability
  • Loading branch information
fujiwara authored Sep 17, 2024
2 parents 40f94a2 + e1a6f64 commit de69972
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
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

0 comments on commit de69972

Please sign in to comment.