-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
213 lines (187 loc) · 4.62 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
const versionStr = "0.0.1-dev"
var (
exprMode = flag.Bool("e", false, "parse as expression")
templateMode = flag.Bool("t", false, "parse as template")
showVersion = flag.Bool("version", false, "show the version number and immediately exit")
)
func main() {
flag.Usage = usage
flag.Parse()
if *showVersion {
fmt.Println(versionStr)
return
}
if flag.NArg() > 1 {
fmt.Fprintf(os.Stderr, "only one file or content can be specified\n")
os.Exit(2)
}
if flag.NArg() == 0 {
os.Exit(processFile("<stdin>", os.Stdin))
}
var fn string
var in io.Reader
if *exprMode || *templateMode {
fn = "<input>"
in = strings.NewReader(flag.Arg(0))
} else {
fn = flag.Arg(0)
in = nil
}
os.Exit(processFile(fn, in))
}
func processFile(fn string, in io.Reader) int {
var err error
if in == nil {
in, err = os.Open(fn)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open %s: %s\n", fn, err)
return 1
}
}
inSrc, err := io.ReadAll(in)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read %s: %s\n", fn, err)
return 1
}
var node hclsyntax.Node
var diags hcl.Diagnostics
if *exprMode {
node, diags = hclsyntax.ParseExpression(inSrc, fn, hcl.InitialPos)
} else if *templateMode {
node, diags = hclsyntax.ParseTemplate(inSrc, fn, hcl.InitialPos)
} else {
file, d := hclsyntax.ParseConfig(inSrc, fn, hcl.InitialPos)
node = file.Body.(*hclsyntax.Body)
diags = d
}
if diags.HasErrors() {
fmt.Fprintf(os.Stderr, "failed to parse. %d diagnostic(s):\n\n", len(diags))
for _, diag := range diags {
fmt.Fprintf(os.Stderr, "%s: %s\n", diag.Summary, diag.Detail)
}
return 1
}
diags = hclsyntax.Walk(node, &walker{file: inSrc})
if diags.HasErrors() {
fmt.Fprintf(os.Stderr, "failed to walk. %d diagnostic(s):\n\n", len(diags))
for _, diag := range diags {
fmt.Fprintf(os.Stderr, "%s: %s\n", diag.Summary, diag.Detail)
}
return 1
}
return 0
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: hcl-parse [options] [file or content]\n")
flag.PrintDefaults()
os.Exit(2)
}
type walker struct {
indent int
leaf bool
file []byte
}
var _ hclsyntax.Walker = (*walker)(nil)
func (w *walker) Enter(node hclsyntax.Node) hcl.Diagnostics {
if w.leaf {
panic("leaf node should not have children")
}
fmt.Print(strings.Repeat(" ", w.indent))
switch node := node.(type) {
case *hclsyntax.Attribute:
fmt.Printf(`(%T "%s"`, node, node.Name)
case *hclsyntax.Block:
fmt.Printf(`(%T "%s" %s`, node, node.Type, node.Labels)
case *hclsyntax.LiteralValueExpr:
fmt.Printf(`(%T "%s")`, node, node.SrcRange.SliceBytes(w.file))
w.leaf = true
case *hclsyntax.ScopeTraversalExpr:
fmt.Printf(`(%T "%s")`, node, node.SrcRange.SliceBytes(w.file))
w.leaf = true
case *hclsyntax.RelativeTraversalExpr:
fmt.Printf(`(%T "%s"`, node, node.Traversal.SourceRange().SliceBytes(w.file))
case *hclsyntax.FunctionCallExpr:
fmt.Printf(`(%T "%s"`, node, node.Name)
case *hclsyntax.ForExpr:
fmt.Printf(`(%T`, node)
if node.KeyVar != "" {
fmt.Printf(` key="%s"`, node.KeyVar)
}
if node.ValVar != "" {
fmt.Printf(` val="%s"`, node.ValVar)
}
case *hclsyntax.AnonSymbolExpr:
fmt.Printf(`(%T)`, node)
w.leaf = true
case *hclsyntax.BinaryOpExpr:
fmt.Printf(`(%T "%s"`, node, opAsString(node.Op))
case *hclsyntax.UnaryOpExpr:
fmt.Printf(`(%T "%s"`, node, opAsString(node.Op))
case *hclsyntax.ObjectConsKeyExpr:
fmt.Printf(`(%T`, node)
if keyword := hcl.ExprAsKeyword(node.Wrapped); keyword != "" {
fmt.Printf(` keyword="%s"`, keyword)
}
default:
fmt.Printf("(%T", node)
}
fmt.Print("\n")
w.indent += 2
return nil
}
func (w *walker) Exit(node hclsyntax.Node) hcl.Diagnostics {
w.indent -= 2
if w.leaf {
w.leaf = false
return nil
}
fmt.Print(strings.Repeat(" ", w.indent))
fmt.Printf(")\n")
return nil
}
func opAsString(op *hclsyntax.Operation) string {
switch op {
case hclsyntax.OpLogicalOr:
return "||"
case hclsyntax.OpLogicalAnd:
return "&&"
case hclsyntax.OpLogicalNot:
return "!"
case hclsyntax.OpEqual:
return "=="
case hclsyntax.OpNotEqual:
return "!="
case hclsyntax.OpGreaterThan:
return ">"
case hclsyntax.OpGreaterThanOrEqual:
return ">="
case hclsyntax.OpLessThan:
return "<"
case hclsyntax.OpLessThanOrEqual:
return "<="
case hclsyntax.OpAdd:
return "+"
case hclsyntax.OpSubtract:
return "-"
case hclsyntax.OpMultiply:
return "*"
case hclsyntax.OpDivide:
return "/"
case hclsyntax.OpModulo:
return "%"
case hclsyntax.OpNegate:
return "-"
default:
panic(fmt.Sprintf("unknown operation type: %T", op))
}
}