hcl icon indicating copy to clipboard operation
hcl copied to clipboard

hclwrite: generate the output it can't parse

Open giautm opened this issue 1 year ago • 0 comments

The input with atlas {} is parse correctly without issue, then I append new child block, it cause the issue when I parse its ouput again. Even formatted with hclwrite.Format,

func Example_Ident() {
	// This input is fine, and valid.
	f, diags := hclwrite.ParseConfig([]byte("atlas {}"), "atlas.hcl", hcl.InitialPos)
	if diags.HasErrors() {
		panic("failed to parse config")
	}
	// Append the cloud block
	f.Body().FirstMatchingBlock("atlas", nil).
		Body().AppendNewBlock("cloud", nil)
	// Format the output.
	out := hclwrite.Format(f.Bytes())
	// Parse the output to check if it's valid.
	_, diags = hclwrite.ParseConfig(out, "atlas.hcl", hcl.InitialPos)

	fmt.Println(string(out))
	fmt.Println(diags)
	// Output:
	// atlas { cloud {
	//   }
	// }
	// atlas.hcl:1,9-16: Argument definition required; A single-line block definition can contain only a single argument. If you meant to define argument "cloud", use an equals sign to assign it a value. To define a nested block, place it on a line of its own within its parent block.
}

giautm avatar Jul 25 '24 11:07 giautm