-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (70 loc) · 1.47 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
package main
import (
"flag"
"fmt"
"go/ast"
"io/ioutil"
"os"
"reflect"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
)
var (
verbose bool
expression bool
)
func init() {
flag.BoolVar(&verbose, "verbose", false, "Show hcl.Range node together")
flag.BoolVar(&expression, "e", false, "Parse the HCL as an expression")
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), `Usage:
hcldump [filename]
`)
flag.PrintDefaults()
}
func main() {
flag.CommandLine.Usage = usage
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) != 1 {
usage()
os.Exit(1)
}
fname := args[0]
src, err := ioutil.ReadFile(fname)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot read file: %v\n", err)
os.Exit(1)
}
var (
root interface{}
diags hcl.Diagnostics
)
if expression {
root, diags = hclsyntax.ParseExpression(src, fname, hcl.InitialPos)
} else {
root, diags = hclsyntax.ParseConfig(src, fname, hcl.InitialPos)
}
if len(diags) != 0 {
fmt.Fprintf(os.Stderr, "failed to parse: %v\n", diags.Error())
//os.Exit(1)
}
var filter ast.FieldFilter
if !verbose {
filter = func(name string, value reflect.Value) bool {
if _, ok := value.Interface().(hcl.Range); ok {
return false
}
if _, ok := value.Interface().([]hcl.Range); ok {
return false
}
return true
}
}
if err := ast.Fprint(os.Stdout, nil, root, filter); err != nil {
fmt.Fprintf(os.Stderr, "print error: %v", err)
os.Exit(1)
}
}