This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
141 lines (122 loc) · 3 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"strings"
"unicode"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclwrite"
)
func formatEmbeddedTerraformManifests(fset *token.FileSet, content []byte) ([]byte, error) {
parsed, err := parser.ParseFile(fset, "src.go", bytes.NewReader(content), parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("unable to parse content: %w", err)
}
var stack []ast.Node
ast.Inspect(parsed, func(n ast.Node) bool {
defer func() {
if n == nil {
// Done with node's children. Pop.
stack = stack[:len(stack)-1]
} else {
// Push the current node for children.
stack = append(stack, n)
}
}()
if n == nil || len(stack) == 0 {
return true
}
lit, ok := n.(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
return true
}
if len(lit.Value) == 0 || lit.Value[0] != '`' {
return true
}
val := strings.Trim(lit.Value, "`")
val = strings.ReplaceAll(val, "\t", "")
if !isTerraformManifest(val) {
return true
} else {
val = formatTerraformManifest(val)
}
indentation := getExpectedIndentation(stack, fset)
lit.Value = "`" + strings.TrimRightFunc(strings.ReplaceAll(val, "\n", "\n"+strings.Repeat("\t", indentation)), unicode.IsSpace) + "`"
return true
})
buf := new(bytes.Buffer)
if err = format.Node(buf, fset, parsed); err != nil {
return nil, fmt.Errorf("unable to write formatted output into buffer: %w", err)
}
return buf.Bytes(), nil
}
func isTerraformManifest(s string) bool {
_, diag := hclparse.NewParser().ParseHCL([]byte(s), "cand.tf")
return !diag.HasErrors()
}
func formatTerraformManifest(s string) string {
return string(hclwrite.Format([]byte(s)))
}
func getExpectedIndentation(stack []ast.Node, fset *token.FileSet) int {
if ret := lastReturnStatement(stack); ret != nil {
return fset.Position(ret.Pos()).Column
}
if ass := lastAssignmentStatement(stack); ass != nil {
return fset.Position(ass.Pos()).Column
}
if ass := lastDeclarationStatement(stack); ass != nil {
return fset.Position(ass.Pos()).Column
}
if ass := lastValueSpec(stack); ass != nil {
return fset.Position(ass.Pos()).Column - 4
}
return 0
}
func lastReturnStatement(stack []ast.Node) ast.Node {
for i := len(stack) - 1; i != 0; i-- {
switch stack[i].(type) {
case *ast.ReturnStmt:
return stack[i]
default:
continue
}
}
return nil
}
func lastAssignmentStatement(stack []ast.Node) ast.Node {
for i := len(stack) - 1; i != 0; i-- {
switch stack[i].(type) {
case *ast.AssignStmt:
return stack[i]
default:
continue
}
}
return nil
}
func lastDeclarationStatement(stack []ast.Node) ast.Node {
for i := len(stack) - 1; i != 0; i-- {
switch stack[i].(type) {
case *ast.DeclStmt:
return stack[i]
default:
continue
}
}
return nil
}
func lastValueSpec(stack []ast.Node) ast.Node {
for i := len(stack) - 1; i != 0; i-- {
switch stack[i].(type) {
case *ast.ValueSpec:
return stack[i]
default:
continue
}
}
return nil
}