hclfmt: only parse hcl files
This commit is contained in:
parent
c61b08ec1c
commit
9c29a82788
37
hclfmt.go
37
hclfmt.go
@ -6,10 +6,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go/scanner"
|
"go/scanner"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -71,22 +74,22 @@ func usage() {
|
|||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isGoFile(f os.FileInfo) bool {
|
|
||||||
// ignore non-Go files
|
|
||||||
name := f.Name()
|
|
||||||
return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
|
|
||||||
}
|
|
||||||
|
|
||||||
func report(err error) {
|
func report(err error) {
|
||||||
scanner.PrintError(os.Stderr, err)
|
scanner.PrintError(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func walkDir(path string) {
|
||||||
filepath.Walk(path, visitFile)
|
filepath.Walk(path, visitFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func visitFile(path string, f os.FileInfo, err error) error {
|
func visitFile(path string, f os.FileInfo, err error) error {
|
||||||
if err == nil && isGoFile(f) {
|
if err == nil && isHclFile(f) {
|
||||||
err = processFile(path, nil, os.Stdout, false)
|
err = processFile(path, nil, os.Stdout, false)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -97,5 +100,25 @@ func visitFile(path string, f os.FileInfo, err error) error {
|
|||||||
|
|
||||||
// If in == nil, the source is the contents of the file with the given filename.
|
// 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 {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
obj, err := hcl.Parse(string(src))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("obj = %+v\n", obj)
|
||||||
return errors.New("not imlemented yet")
|
return errors.New("not imlemented yet")
|
||||||
}
|
}
|
||||||
|
42
testdata/complex.hcl
vendored
Normal file
42
testdata/complex.hcl
vendored
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// This comes from Terraform, as a test
|
||||||
|
variable "foo" {
|
||||||
|
default = "bar"
|
||||||
|
description = "bar"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "aws" {
|
||||||
|
access_key = "foo"
|
||||||
|
secret_key = "bar"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "do" {
|
||||||
|
api_key = "${var.foo}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group" "firewall" {
|
||||||
|
count = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
resource aws_instance "web" {
|
||||||
|
ami = "${var.foo}"
|
||||||
|
security_groups = [
|
||||||
|
"foo",
|
||||||
|
"${aws_security_group.firewall.foo}"
|
||||||
|
]
|
||||||
|
|
||||||
|
network_interface {
|
||||||
|
device_index = 0
|
||||||
|
description = "Main network interface"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_instance" "db" {
|
||||||
|
security_groups = "${aws_security_group.firewall.*.id}"
|
||||||
|
VPC = "foo"
|
||||||
|
|
||||||
|
depends_on = ["aws_instance.web"]
|
||||||
|
}
|
||||||
|
|
||||||
|
output "web_ip" {
|
||||||
|
value = "${aws_instance.web.private_ip}"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user