forked from fatih/hclfmt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
145 lines (119 loc) · 2.68 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"github.com/hashicorp/hcl2/hclparse"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/gruntwork-io/terragrunt/util"
)
const Version = "0.1.0"
var (
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
version = flag.Bool("version", false, "version information")
)
func main() {
if err := realMain(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func realMain() error {
flag.Usage = usage
flag.Parse()
if *version {
fmt.Println(Version)
return nil
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return fmt.Errorf("creating cpu profile: %s\n", err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if flag.NArg() == 0 {
if *write {
return errors.New("error: cannot use -w with standard input")
}
return processFile("<standard input>", os.Stdin, os.Stdout, true)
}
for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i)
switch dir, err := os.Stat(path); {
case err != nil:
return err
case dir.IsDir():
walkDir(path)
default:
if err := processFile(path, nil, os.Stdout, false); err != nil {
return err
}
}
}
return nil
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: hclfmt [flags] [path ...]\n")
flag.PrintDefaults()
os.Exit(2)
}
func isHclFile(f os.FileInfo) bool {
// ignore non-hcl files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".hcl")
}
func walkDir(path string) {
filepath.Walk(path, visitFile)
}
func visitFile(path string, f os.FileInfo, err error) error {
if err == nil && isHclFile(f) {
err = processFile(path, nil, os.Stdout, false)
}
return err
}
func checkHcl(src []byte, hclFilename string) error {
parser := hclparse.NewParser()
_, diags := parser.ParseHCL(src, hclFilename)
diagWriter := util.GetDiagnosticsWriter(parser)
diagWriter.WriteDiagnostics(diags)
if diags.HasErrors() {
return diags
}
return nil
}
// If in == nil, the source is the contents of the file with the given filename.
func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error {
if in == nil {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
in = f
}
src, err := ioutil.ReadAll(in)
if err != nil {
return err
}
err = checkHcl(src, filename)
if err != nil {
return err
}
res := hclwrite.Format(src)
if *write {
err = ioutil.WriteFile(filename, res, 0644)
} else {
_, err = out.Write(res)
}
return err
}