2015-04-27 23:19:51 +00:00
|
|
|
package hcl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-03-09 20:55:26 +00:00
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
hclParser "github.com/hashicorp/hcl/hcl/parser"
|
|
|
|
jsonParser "github.com/hashicorp/hcl/json/parser"
|
2015-04-27 23:19:51 +00:00
|
|
|
)
|
|
|
|
|
2016-04-26 00:18:04 +00:00
|
|
|
// ParseBytes accepts as input byte slice and returns ast tree.
|
2015-04-27 23:19:51 +00:00
|
|
|
//
|
2016-04-26 00:18:04 +00:00
|
|
|
// Input can be either JSON or HCL
|
|
|
|
func ParseBytes(in []byte) (*ast.File, error) {
|
|
|
|
return parse(in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseString accepts input as a string and returns ast tree.
|
|
|
|
func ParseString(input string) (*ast.File, error) {
|
|
|
|
return parse([]byte(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(in []byte) (*ast.File, error) {
|
|
|
|
switch lexMode(in) {
|
2015-04-27 23:19:51 +00:00
|
|
|
case lexModeHcl:
|
2016-04-26 00:18:04 +00:00
|
|
|
return hclParser.Parse(in)
|
2015-04-27 23:19:51 +00:00
|
|
|
case lexModeJson:
|
2016-04-26 00:18:04 +00:00
|
|
|
return jsonParser.Parse(in)
|
2015-04-27 23:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unknown config format")
|
|
|
|
}
|
2016-04-26 00:18:04 +00:00
|
|
|
|
|
|
|
// Parse parses the given input and returns the root object.
|
|
|
|
//
|
|
|
|
// The input format can be either HCL or JSON.
|
|
|
|
func Parse(input string) (*ast.File, error) {
|
|
|
|
return parse([]byte(input))
|
|
|
|
}
|