|
6 | 6 |
|
7 | 7 | "github.com/hashicorp/hcl/v2" |
8 | 8 | "github.com/hashicorp/hcl/v2/hclsyntax" |
| 9 | + "github.com/zclconf/go-cty/cty" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | // StackAST provides methods for working with terragrunt.stack.hcl files. |
@@ -39,42 +40,28 @@ func (s *stackAST) FindNodeAt(pos hcl.Pos) *ast.IndexedNode { |
39 | 40 |
|
40 | 41 | // GetUnitLabel returns the label of the given node, if it is a unit block |
41 | 42 | func (s *stackAST) GetUnitLabel(node *ast.IndexedNode) (string, bool) { |
42 | | - attr := ast.FindFirstParentMatch(node, ast.IsAttribute) |
43 | | - if attr == nil { |
44 | | - return "", false |
45 | | - } |
46 | | - |
47 | | - unitBlock := ast.FindFirstParentMatch(attr, isUnitBlock) |
48 | | - if unitBlock == nil { |
49 | | - return "", false |
50 | | - } |
51 | | - |
52 | | - name := "" |
53 | | - if labels := unitBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 { |
54 | | - name = labels[0] |
55 | | - } |
56 | | - |
57 | | - return name, true |
| 43 | + return firstLabelFromContainingBlock(node, isUnitBlock) |
58 | 44 | } |
59 | 45 |
|
60 | 46 | // GetStackLabel returns the label of the given node, if it is a stack block |
61 | 47 | func (s *stackAST) GetStackLabel(node *ast.IndexedNode) (string, bool) { |
| 48 | + return firstLabelFromContainingBlock(node, isStackBlock) |
| 49 | +} |
| 50 | + |
| 51 | +// firstLabelFromContainingBlock walks up to the containing attribute and then the |
| 52 | +// nearest block matching blockMatcher, returning that block's first label. |
| 53 | +func firstLabelFromContainingBlock(node *ast.IndexedNode, blockMatcher func(*ast.IndexedNode) bool) (string, bool) { |
62 | 54 | attr := ast.FindFirstParentMatch(node, ast.IsAttribute) |
63 | 55 | if attr == nil { |
64 | 56 | return "", false |
65 | 57 | } |
66 | 58 |
|
67 | | - stackBlock := ast.FindFirstParentMatch(attr, isStackBlock) |
68 | | - if stackBlock == nil { |
| 59 | + block := ast.FindFirstParentMatch(attr, blockMatcher) |
| 60 | + if block == nil { |
69 | 61 | return "", false |
70 | 62 | } |
71 | 63 |
|
72 | | - name := "" |
73 | | - if labels := stackBlock.Node.(*hclsyntax.Block).Labels; len(labels) > 0 { |
74 | | - name = labels[0] |
75 | | - } |
76 | | - |
77 | | - return name, true |
| 64 | + return block.Node.(*hclsyntax.Block).Labels[0], true |
78 | 65 | } |
79 | 66 |
|
80 | 67 | // GetUnitSource returns the source attribute value from a unit block |
@@ -162,14 +149,14 @@ func (s *stackAST) getBlockAttribute(node *ast.IndexedNode, blockMatcher func(*a |
162 | 149 | func (s *stackAST) extractStringValue(expr hclsyntax.Expression) (string, bool) { |
163 | 150 | switch e := expr.(type) { |
164 | 151 | case *hclsyntax.LiteralValueExpr: |
165 | | - if e.Val.Type().FriendlyName() == "string" { |
| 152 | + if e.Val.Type() == cty.String { |
166 | 153 | return e.Val.AsString(), true |
167 | 154 | } |
168 | 155 | case *hclsyntax.TemplateExpr: |
169 | 156 | // Handle quoted strings which are parsed as TemplateExpr |
170 | 157 | if len(e.Parts) == 1 { |
171 | 158 | if literal, ok := e.Parts[0].(*hclsyntax.LiteralValueExpr); ok { |
172 | | - if literal.Val.Type().FriendlyName() == "string" { |
| 159 | + if literal.Val.Type() == cty.String { |
173 | 160 | return literal.Val.AsString(), true |
174 | 161 | } |
175 | 162 | } |
|
0 commit comments