Example #1
0
// 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) {
	switch lexMode(input) {
	case lexModeHcl:
		return hclParser.Parse([]byte(input))
	case lexModeJson:
		return jsonParser.Parse([]byte(input))
	}

	return nil, fmt.Errorf("unknown config format")
}
Example #2
0
func parse(in []byte) (*ast.File, error) {
	switch lexMode(in) {
	case lexModeHcl:
		return hclParser.Parse(in)
	case lexModeJson:
		return jsonParser.Parse(in)
	}

	return nil, fmt.Errorf("unknown config format")
}
Example #3
0
// format parses src, prints the corresponding AST, verifies the resulting
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte) ([]byte, error) {
	formatted, err := Format(src)
	if err != nil {
		return nil, err
	}

	// make sure formatted output is syntactically correct
	if _, err := parser.Parse(formatted); err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	return formatted, nil
}
Example #4
0
// Format formats src HCL and returns the result.
func Format(src []byte) ([]byte, error) {
	node, err := parser.Parse(src)
	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer
	if err := DefaultConfig.Fprint(&buf, node); err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
Example #5
0
// format parses src, prints the corresponding AST, verifies the resulting
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte) ([]byte, error) {
	// parse src
	node, err := parser.Parse(src)
	if err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	var buf bytes.Buffer

	cfg := &Config{}
	if err := cfg.Fprint(&buf, node); err != nil {
		return nil, fmt.Errorf("print: %s", err)
	}

	// make sure formatted output is syntactically correct
	res := buf.Bytes()

	if _, err := parser.Parse(src); err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	return res, nil
}
Example #6
0
// Format formats src HCL and returns the result.
func Format(src []byte) ([]byte, error) {
	node, err := parser.Parse(src)
	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer
	if err := DefaultConfig.Fprint(&buf, node); err != nil {
		return nil, err
	}

	// Add trailing newline to result
	buf.WriteString("\n")

	return buf.Bytes(), nil
}