]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/hcl2/hclwrite/public.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / hcl2 / hclwrite / public.go
1 package hclwrite
2
3 import (
4 "bytes"
5
6 "github.com/hashicorp/hcl2/hcl"
7 )
8
9 // NewFile creates a new file object that is empty and ready to have constructs
10 // added t it.
11 func NewFile() *File {
12 body := &Body{
13 inTree: newInTree(),
14 items: newNodeSet(),
15 }
16 file := &File{
17 inTree: newInTree(),
18 }
19 file.body = file.inTree.children.Append(body)
20 return file
21 }
22
23 // ParseConfig interprets the given source bytes into a *hclwrite.File. The
24 // resulting AST can be used to perform surgical edits on the source code
25 // before turning it back into bytes again.
26 func ParseConfig(src []byte, filename string, start hcl.Pos) (*File, hcl.Diagnostics) {
27 return parse(src, filename, start)
28 }
29
30 // Format takes source code and performs simple whitespace changes to transform
31 // it to a canonical layout style.
32 //
33 // Format skips constructing an AST and works directly with tokens, so it
34 // is less expensive than formatting via the AST for situations where no other
35 // changes will be made. It also ignores syntax errors and can thus be applied
36 // to partial source code, although the result in that case may not be
37 // desirable.
38 func Format(src []byte) []byte {
39 tokens := lexConfig(src)
40 format(tokens)
41 buf := &bytes.Buffer{}
42 tokens.WriteTo(buf)
43 return buf.Bytes()
44 }