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

Fix/import #19

Merged
merged 2 commits into from
Nov 12, 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
44 changes: 40 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,40 @@ func (app *App) processFile(path string, state *tfstate.TFState) error {
func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error) {
switch attr.Expr.(type) {
case *hclsyntax.TemplateExpr:
result := []string{}
for _, part := range attr.Expr.(*hclsyntax.TemplateExpr).Parts {
switch part.(type) {
case *hclsyntax.LiteralValueExpr:
return part.(*hclsyntax.LiteralValueExpr).Val.AsString(), nil
result = append(result, part.(*hclsyntax.LiteralValueExpr).Val.AsString())
case *hclsyntax.ScopeTraversalExpr:
valueSlice := []string{"\"", "${"}
for _, traversals := range part.(*hclsyntax.ScopeTraversalExpr).Variables() {
tl := len(traversals)
for i, traversal := range traversals {
switch traversal.(type) {
case hcl.TraverseRoot:
valueSlice = append(valueSlice, traversal.(hcl.TraverseRoot).Name)
valueSlice = append(valueSlice, ".")
if i == tl-1 {
valueSlice = valueSlice[:len(valueSlice)-1]
}
case hcl.TraverseAttr:
valueSlice = append(valueSlice, traversal.(hcl.TraverseAttr).Name)
valueSlice = append(valueSlice, ".")
if i == tl-1 {
valueSlice = valueSlice[:len(valueSlice)-1]
}
}
}
}
valueSlice = append(valueSlice, "}")
result = append(result, strings.Join(valueSlice, ""))
default:
return "", fmt.Errorf("unexpected type: %T", part)
}
}
result = append(result, "\"")
return strings.Join(result, ""), nil
case *hclsyntax.ScopeTraversalExpr:
valueSlice := []string{}
for _, traversals := range attr.Expr.(*hclsyntax.ScopeTraversalExpr).Variables() {
Expand All @@ -128,9 +154,19 @@ func (app *App) getValueFromAttribute(attr *hclsyntax.Attribute) (string, error)
}
case hcl.TraverseIndex:
valueSlice = valueSlice[:len(valueSlice)-1]
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
if i == tl-1 {
return strings.Join(valueSlice, ""), nil
switch traversal.(hcl.TraverseIndex).Key.Type().FriendlyName() {
case "string":
valueSlice = append(valueSlice, fmt.Sprintf("[\"%s\"]", traversal.(hcl.TraverseIndex).Key.AsString()))
if i == tl-1 {
return strings.Join(valueSlice, ""), nil
}
case "number":
valueSlice = append(valueSlice, fmt.Sprintf("[%s]", traversal.(hcl.TraverseIndex).Key.AsBigFloat().String()))
if i == tl-1 {
return strings.Join(valueSlice, ""), nil
}
default:
return "", fmt.Errorf("unexpected type: %T", traversal.(hcl.TraverseIndex).Key.Type().FriendlyName())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error
s.Init(bytes.NewReader(data))
s.Mode = scanner.ScanIdents | scanner.ScanFloats
s.IsIdentRune = func(ch rune, i int) bool {
return ch == '-' || ch == '_' || ch == '.' || ch == '[' || ch == ']' || ch == ':' || ch == '"' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
return ch == '-' || ch == '_' || ch == '.' || ch == '[' || ch == ']' || ch == ':' || ch == '"' || ch == '$' || ch == '{' || ch == '}' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

var lastPos int
Expand Down
23 changes: 23 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ import {
to = module.foo["hoge"]
}
bbb
`),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
# import
aaa
import {
id = "${local.a}-1"
to = module.foo[0]
}
bbb
`),
to: "module.foo[0]",
id: "${local.a}-1",
},
want: []byte(`
# import
aaa
bbb
`),
wantErr: false,
},
Expand Down
Loading