printer: add spacewidth support
This commit is contained in:
parent
0fc42b65df
commit
e947512362
@ -70,7 +70,7 @@ func (p *printer) printObjectType(o *ast.ObjectType) []byte {
|
||||
buf.WriteByte(newline)
|
||||
|
||||
for _, item := range o.List.Items {
|
||||
buf.Write(indent(p.printObjectItem(item)))
|
||||
buf.Write(p.indent(p.printObjectItem(item)))
|
||||
buf.WriteByte(newline)
|
||||
}
|
||||
|
||||
@ -110,12 +110,19 @@ func writeBlank(buf io.ByteWriter, indent int) {
|
||||
}
|
||||
}
|
||||
|
||||
func indent(buf []byte) []byte {
|
||||
func (p *printer) indent(buf []byte) []byte {
|
||||
prefix := []byte{tab}
|
||||
if p.cfg.SpaceWidth != 0 {
|
||||
for i := 0; i < p.cfg.SpaceWidth; i++ {
|
||||
prefix = append(prefix, blank)
|
||||
}
|
||||
}
|
||||
|
||||
var res []byte
|
||||
bol := true
|
||||
for _, c := range buf {
|
||||
if bol && c != '\n' {
|
||||
res = append(res, []byte{tab}...)
|
||||
res = append(res, prefix...)
|
||||
}
|
||||
res = append(res, c)
|
||||
bol = c == '\n'
|
||||
|
2
printer/testdata/complexhcl.golden
vendored
2
printer/testdata/complexhcl.golden
vendored
@ -36,4 +36,4 @@ resource "aws_instance" "db" = {
|
||||
|
||||
output "web_ip" = {
|
||||
value = "${aws_instance.web.private_ip}"
|
||||
}
|
||||
}
|
42
testdata/complex.hcl
vendored
42
testdata/complex.hcl
vendored
@ -1,42 +0,0 @@
|
||||
// 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