2015-10-24 21:04:31 +00:00
|
|
|
package printer
|
2015-10-24 22:23:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/fatih/hcl/ast"
|
|
|
|
)
|
|
|
|
|
2015-10-25 13:08:09 +00:00
|
|
|
const (
|
|
|
|
blank = byte(' ')
|
|
|
|
newline = byte('\n')
|
|
|
|
tab = byte('\t')
|
|
|
|
)
|
|
|
|
|
2015-10-25 22:34:41 +00:00
|
|
|
// output prints creates a printable HCL output and returns it.
|
2015-10-26 22:23:22 +00:00
|
|
|
func (p *printer) output(n interface{}) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
switch t := n.(type) {
|
|
|
|
case *ast.ObjectList:
|
2015-10-25 13:08:09 +00:00
|
|
|
for i, item := range t.Items {
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectItem(item))
|
2015-10-25 13:08:09 +00:00
|
|
|
if i != len(t.Items)-1 {
|
2015-10-25 13:12:08 +00:00
|
|
|
buf.Write([]byte{newline, newline})
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
case *ast.ObjectKey:
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteString(t.Token.Text)
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ObjectItem:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectItem(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.LiteralType:
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteString(t.Token.Text)
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ListType:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.list(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ObjectType:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectType(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf(" unknown type: %T\n", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) objectItem(o *ast.ObjectItem) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2015-10-26 22:23:22 +00:00
|
|
|
if o.LeadComment != nil {
|
2015-10-26 22:42:05 +00:00
|
|
|
for _, comment := range o.LeadComment.List {
|
|
|
|
buf.WriteString(comment.Text)
|
|
|
|
buf.WriteByte(newline)
|
|
|
|
}
|
2015-10-26 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 22:23:50 +00:00
|
|
|
for i, k := range o.Keys {
|
|
|
|
buf.WriteString(k.Token.Text)
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
|
|
|
|
// reach end of key
|
2015-10-25 15:59:29 +00:00
|
|
|
if i == len(o.Keys)-1 && len(o.Keys) == 1 {
|
2015-10-24 22:23:50 +00:00
|
|
|
buf.WriteString("=")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.output(o.Val))
|
2015-10-26 22:23:22 +00:00
|
|
|
|
|
|
|
if o.Val.Pos().Line == o.Keys[0].Pos().Line && o.LineComment != nil {
|
|
|
|
buf.WriteByte(blank)
|
2015-10-26 22:42:05 +00:00
|
|
|
for _, comment := range o.LineComment.List {
|
|
|
|
buf.WriteString(comment.Text)
|
|
|
|
}
|
2015-10-26 22:23:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 22:23:50 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-27 20:51:44 +00:00
|
|
|
func (p *printer) alignedItems(items []*ast.ObjectItem) []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
var longestLine int
|
|
|
|
for _, item := range items {
|
|
|
|
lineLen := len(item.Keys[0].Token.Text) + len(p.output(item.Val))
|
|
|
|
if lineLen > longestLine {
|
|
|
|
longestLine = lineLen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
curLen := 0
|
|
|
|
for i, k := range item.Keys {
|
|
|
|
buf.WriteString(k.Token.Text)
|
|
|
|
buf.WriteByte(blank)
|
|
|
|
|
|
|
|
// reach end of key
|
|
|
|
if i == len(item.Keys)-1 && len(item.Keys) == 1 {
|
|
|
|
buf.WriteString("=")
|
|
|
|
buf.WriteByte(blank)
|
|
|
|
}
|
|
|
|
|
|
|
|
curLen = len(k.Token.Text) // two blanks and one assign
|
|
|
|
}
|
|
|
|
val := p.output(item.Val)
|
|
|
|
buf.Write(val)
|
|
|
|
curLen += len(val)
|
|
|
|
|
|
|
|
if item.Val.Pos().Line == item.Keys[0].Pos().Line && item.LineComment != nil {
|
|
|
|
for i := 0; i < longestLine-curLen+1; i++ {
|
|
|
|
buf.WriteByte(blank)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range item.LineComment.List {
|
|
|
|
buf.WriteString(comment.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte(newline)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) literal(l *ast.LiteralType) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
return []byte(l.Token.Text)
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) objectType(o *ast.ObjectType) []byte {
|
2015-10-25 13:08:09 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("{")
|
|
|
|
buf.WriteByte(newline)
|
|
|
|
|
2015-10-27 20:51:44 +00:00
|
|
|
// check if we have adjacent one liner items. If yes we'll going to align
|
|
|
|
// the comments.
|
2015-10-27 21:59:54 +00:00
|
|
|
var index int
|
|
|
|
for {
|
|
|
|
var oneLines []*ast.ObjectItem
|
|
|
|
for _, item := range o.List.Items[index:] {
|
|
|
|
// protect agains slice bounds
|
|
|
|
if index == len(o.List.Items)-1 {
|
|
|
|
// check for the latest item of a series of one liners in the
|
|
|
|
// end of a list.
|
|
|
|
if index != 0 && // do not check if the list length is one
|
|
|
|
lines(string(p.objectItem(item))) < 1 && // be sure it's really a one line
|
|
|
|
o.List.Items[index-1].Pos().Line == item.Pos().Line-1 {
|
|
|
|
|
|
|
|
oneLines = append(oneLines, item)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.List.Items[1+index].Pos().Line == item.Pos().Line+1 {
|
|
|
|
oneLines = append(oneLines, item)
|
|
|
|
index++
|
|
|
|
} else {
|
|
|
|
// break in any nonadjacent items
|
|
|
|
break
|
|
|
|
}
|
2015-10-27 20:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:59:54 +00:00
|
|
|
// fmt.Printf("len(oneLines) = %+v\n", len(oneLines))
|
|
|
|
// for _, i := range oneLines {
|
|
|
|
// a := i.Keys[0]
|
|
|
|
// fmt.Printf("a = %+v\n", a)
|
|
|
|
// }
|
|
|
|
|
|
|
|
if len(oneLines) != 0 {
|
|
|
|
items := p.alignedItems(oneLines)
|
|
|
|
buf.Write(p.indent(items))
|
|
|
|
|
|
|
|
if index != len(o.List.Items) {
|
|
|
|
buf.WriteByte(newline)
|
|
|
|
}
|
2015-10-27 20:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:59:54 +00:00
|
|
|
if index == len(o.List.Items) {
|
|
|
|
break
|
|
|
|
}
|
2015-10-27 20:51:44 +00:00
|
|
|
|
2015-10-27 21:59:54 +00:00
|
|
|
buf.Write(p.indent(p.objectItem(o.List.Items[index])))
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-27 21:59:54 +00:00
|
|
|
index++
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString("}")
|
2015-10-27 21:59:54 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-25 13:08:09 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
// printList prints a HCL list
|
|
|
|
func (p *printer) list(l *ast.ListType) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("[")
|
|
|
|
|
|
|
|
for i, item := range l.List {
|
|
|
|
if item.Pos().Line != l.Lbrack.Line {
|
2015-10-25 16:09:22 +00:00
|
|
|
// multiline list, add newline before we add each item
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-25 16:09:22 +00:00
|
|
|
// also indent each line
|
|
|
|
buf.Write(p.indent(p.output(item)))
|
|
|
|
} else {
|
|
|
|
buf.Write(p.output(item))
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if i != len(l.List)-1 {
|
|
|
|
buf.WriteString(",")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
} else if item.Pos().Line != l.Lbrack.Line {
|
|
|
|
buf.WriteString(",")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString("]")
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
2015-10-25 13:08:09 +00:00
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
// indent indents the lines of the given buffer for each non-empty line
|
2015-10-25 15:02:40 +00:00
|
|
|
func (p *printer) indent(buf []byte) []byte {
|
2015-10-25 15:10:34 +00:00
|
|
|
var prefix []byte
|
|
|
|
if p.cfg.SpacesWidth != 0 {
|
|
|
|
for i := 0; i < p.cfg.SpacesWidth; i++ {
|
2015-10-25 15:02:40 +00:00
|
|
|
prefix = append(prefix, blank)
|
|
|
|
}
|
2015-10-25 15:10:34 +00:00
|
|
|
} else {
|
|
|
|
prefix = []byte{tab}
|
2015-10-25 15:02:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 14:51:18 +00:00
|
|
|
var res []byte
|
|
|
|
bol := true
|
|
|
|
for _, c := range buf {
|
|
|
|
if bol && c != '\n' {
|
2015-10-25 15:02:40 +00:00
|
|
|
res = append(res, prefix...)
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-25 14:51:18 +00:00
|
|
|
res = append(res, c)
|
|
|
|
bol = c == '\n'
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-25 14:51:18 +00:00
|
|
|
return res
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-27 21:59:54 +00:00
|
|
|
|
|
|
|
func lines(txt string) int {
|
|
|
|
endline := 0
|
|
|
|
for i := 0; i < len(txt); i++ {
|
|
|
|
if txt[i] == '\n' {
|
|
|
|
endline++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return endline
|
|
|
|
}
|