2017-09-11 23:00:31 +00:00
|
|
|
package hclwrite
|
2017-06-07 14:24:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-06-07 14:24:10 +00:00
|
|
|
)
|
|
|
|
|
2018-08-01 15:45:22 +00:00
|
|
|
// NewFile creates a new file object that is empty and ready to have constructs
|
|
|
|
// added t it.
|
|
|
|
func NewFile() *File {
|
|
|
|
body := &Body{
|
2018-08-14 14:55:47 +00:00
|
|
|
inTree: newInTree(),
|
|
|
|
items: newNodeSet(),
|
2018-08-01 15:45:22 +00:00
|
|
|
}
|
|
|
|
file := &File{
|
|
|
|
inTree: newInTree(),
|
|
|
|
}
|
|
|
|
file.body = file.inTree.children.Append(body)
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2018-01-27 19:02:46 +00:00
|
|
|
// ParseConfig interprets the given source bytes into a *hclwrite.File. The
|
2017-06-07 15:28:43 +00:00
|
|
|
// resulting AST can be used to perform surgical edits on the source code
|
|
|
|
// before turning it back into bytes again.
|
2017-09-11 23:40:37 +00:00
|
|
|
func ParseConfig(src []byte, filename string, start hcl.Pos) (*File, hcl.Diagnostics) {
|
2017-06-07 14:24:10 +00:00
|
|
|
return parse(src, filename, start)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format takes source code and performs simple whitespace changes to transform
|
|
|
|
// it to a canonical layout style.
|
|
|
|
//
|
|
|
|
// Format skips constructing an AST and works directly with tokens, so it
|
|
|
|
// is less expensive than formatting via the AST for situations where no other
|
|
|
|
// changes will be made. It also ignores syntax errors and can thus be applied
|
|
|
|
// to partial source code, although the result in that case may not be
|
|
|
|
// desirable.
|
|
|
|
func Format(src []byte) []byte {
|
|
|
|
tokens := lexConfig(src)
|
|
|
|
format(tokens)
|
|
|
|
buf := &bytes.Buffer{}
|
2018-08-01 15:45:22 +00:00
|
|
|
tokens.WriteTo(buf)
|
2017-06-07 14:24:10 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|