1 // Package printer implements printing of AST nodes to HCL format.
9 "github.com/hashicorp/hcl/hcl/ast"
10 "github.com/hashicorp/hcl/hcl/parser"
13 var DefaultConfig = Config{
17 // A Config node controls the output of Fprint.
19 SpacesWidth int // if set, it will use spaces instead of tabs for alignment
22 func (c *Config) Fprint(output io.Writer, node ast.Node) error {
25 comments: make([]*ast.CommentGroup, 0),
26 standaloneComments: make([]*ast.CommentGroup, 0),
30 p.collectComments(node)
32 if _, err := output.Write(p.unindent(p.output(node))); err != nil {
36 // flush tabwriter, if any
38 if tw, _ := output.(*tabwriter.Writer); tw != nil {
45 // Fprint "pretty-prints" an HCL node to output
46 // It calls Config.Fprint with default settings.
47 func Fprint(output io.Writer, node ast.Node) error {
48 return DefaultConfig.Fprint(output, node)
51 // Format formats src HCL and returns the result.
52 func Format(src []byte) ([]byte, error) {
53 node, err := parser.Parse(src)
59 if err := DefaultConfig.Fprint(&buf, node); err != nil {
63 // Add trailing newline to result
65 return buf.Bytes(), nil