Hate YAML? Build your next tool with HCL!

weak pixel
2 min readMar 31, 2022

--

In this post, I want to show how you can implement your own tool using the HCL format.
The HCL configuration format is used by all the amazing HasiCorp tools like Terraform, Vault, and Nomad.

Most modern applications and tools use YAML these days which is generally an easy-to-read format but can also be the cause of a lot of pain because of the white space sensitivity. HCL on the other hand provides an easy-to-read format with a clear structure and additional features like variable interpolation and inline function calls.

Let’s start with a really simple example to parse HCL.

To map the HCL to our structs we can use struct tags:

And to decode the HCL we can use the decode function from the hclsimple package.

That was easy!

But what is if I want to support a different Step types? Let’s say I want to support `mkdir` to easily create directories.

If I run our tool I get the following error:

example.hcl:3,4–9: Unsupported block type; Blocks of type “mkdir” are not expected here.

We could update our Task struct and add a block list for “mkdir”:

but obviously, we would lose the execution order since we have two separate lists. This is not going to work for us.

As an alternative solution we could change our configuration and make the Step type a label:

Let’s reflect the configuration change to our structs.

As you can see we have added a new Step struct and use it in the Tasks struct instead of the ExecStep
The Step struct has an extra field called Remain. This field is required to capture all fields of the actual Step implementation.We will see later how we use the Remain field to decode the fields into our actual Step implementation.

Lastly, we add a new interface that allows us to run the Step implementation:

You can see above that all our Steps implement the Runner interface.

Now we have to adapt our parsing code:

The parsing does determine the Step type in the switch statement and creates an instance of the Struct. The struct is then the target of `gohcl.DecodeBody(step.Remain, nil, runner)` which decodes the remaining fields.

Voilà, we have an easy-to-extend Task execution engine.

Resources

Godocs:
- hcl
- hclsimple
- gohcl

Others:
- buildmedia.readthedocs.org/hcl.pdf

Source Gists:
- Example 1
- Example 2

Full Source Code

--

--