4
4
"encoding/json"
5
5
"fmt"
6
6
"github.com/hashicorp/hcl2/hcl"
7
+ "github.com/hashicorp/hcl2/hcl/hclsyntax"
7
8
"github.com/hashicorp/hcl2/hclwrite"
8
9
"regexp"
9
10
"strings"
@@ -19,10 +20,32 @@ func WriteUnquotedAttribute(block *hclwrite.Block, attrName string, attrValue st
19
20
20
21
// WriteActionProperties is used to pretty print the properties of an action, writing a multiline map for the properties,
21
22
// and extracting JSON blobs as maps for easy reading.
22
- func WriteActionProperties (block * hclwrite.Block , step int , action int , properties map [string ]string ) {
23
- block .Body ().Blocks ()[step ].Body ().Blocks ()[action ].Body ().SetAttributeTraversal ("properties" , hcl.Traversal {
24
- hcl.TraverseRoot {Name : extractJsonAsMap (properties )},
25
- })
23
+ func WriteActionProperties (block * hclwrite.Block , stepName string , actionName string , properties map [string ]string ) {
24
+ for _ , stepBlock := range block .Body ().Blocks () {
25
+ stepNameTokens := hclwrite.Tokens {}
26
+ blockStepName := getAttributeValue (stepBlock .Body ().GetAttribute ("name" ).BuildTokens (stepNameTokens ))
27
+ if blockStepName == stepName {
28
+ for _ , actionBlock := range stepBlock .Body ().Blocks () {
29
+ actionNameTokens := hclwrite.Tokens {}
30
+ blockActionName := getAttributeValue (actionBlock .Body ().GetAttribute ("name" ).BuildTokens (actionNameTokens ))
31
+ if blockActionName == actionName {
32
+ actionBlock .Body ().SetAttributeTraversal ("properties" , hcl.Traversal {
33
+ hcl.TraverseRoot {Name : extractJsonAsMap (properties )},
34
+ })
35
+ }
36
+ }
37
+ }
38
+ }
39
+ }
40
+
41
+ func getAttributeValue (tokens hclwrite.Tokens ) string {
42
+ for _ , token := range tokens {
43
+ if token .Type == hclsyntax .TokenQuotedLit {
44
+ return string (token .Bytes )
45
+ }
46
+ }
47
+
48
+ return ""
26
49
}
27
50
28
51
func extractJsonAsMap (properties map [string ]string ) string {
0 commit comments