2015-10-16 20:22:28 +00:00
|
|
|
package printer
|
2015-10-03 00:30:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2015-10-24 21:04:31 +00:00
|
|
|
"github.com/fatih/hcl/ast"
|
2015-10-03 00:30:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type printer struct {
|
2015-10-24 22:23:50 +00:00
|
|
|
out []byte // raw printer result
|
2015-10-24 21:04:31 +00:00
|
|
|
cfg Config
|
|
|
|
node ast.Node
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *printer) output() []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
return p.printNode(p.node)
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Config node controls the output of Fprint.
|
|
|
|
type Config struct {
|
2015-10-25 13:08:09 +00:00
|
|
|
SpaceWidth int // if set, it will use spaces instead of tabs for alignment
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 21:04:31 +00:00
|
|
|
func (c *Config) fprint(output io.Writer, node ast.Node) error {
|
2015-10-03 00:30:57 +00:00
|
|
|
p := &printer{
|
2015-10-24 21:04:31 +00:00
|
|
|
cfg: *c,
|
|
|
|
node: node,
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := output.Write(p.output()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// flush tabwriter, if any
|
|
|
|
var err error
|
|
|
|
if tw, _ := output.(*tabwriter.Writer); tw != nil {
|
|
|
|
err = tw.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-24 21:04:31 +00:00
|
|
|
func (c *Config) Fprint(output io.Writer, node ast.Node) error {
|
|
|
|
return c.fprint(output, node)
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 21:04:31 +00:00
|
|
|
// Fprint "pretty-prints" an HCL node to output
|
2015-10-03 00:30:57 +00:00
|
|
|
// It calls Config.Fprint with default settings.
|
2015-10-24 21:04:31 +00:00
|
|
|
func Fprint(output io.Writer, node ast.Node) error {
|
2015-10-25 13:08:09 +00:00
|
|
|
return (&Config{}).Fprint(output, node)
|
2015-10-03 00:30:57 +00:00
|
|
|
}
|