Powerful text interpolation with rise

Cameron Stitt

Mar 21 2019

Categories: Projects Tags: Go

Throughout my career I have had many times where I have needed to perform text interpolation, or templating, on files. Over the years, I have generally stuck to either envsubst or m4 which have performed admirably. However, there were a few issues that I felt they either didn’t solve easily. Some of these are:

After using Terraform’s configuration syntax for a little while, I admired the simplicity in which the syntax was formed and the powerful capabilities it provided. However, it was part of the Terraform toolchain and could not be used on specific files. This was when I decided to build my own text interpolation tool, rise.

This post will provide a very quick overview of the concepts and some links to project for further reading.

Simple Usage

There are two main concepts in rise. Config files and template files. A config file contains all of the different configuration that can then be utilised in a template file. The template file is the text to perform the interpolation on.

A simple example of a config file is as follows:

variable "i" {
  value = 6
}

This simply declares a variable i, with a default value of 6. More details on the syntax of a configuration file can be found at the HCL project page.

A simple example of a template file is as follows (note that this file is JSON, but could be any syntax):

{
  "i": ${var.i}
}

It’s a simple JSON structure, that will set the value of the key i, to the value from the configuration above.

If the config file was located at vars.hcl and the template at input.json, we could perform interpolation as follows:

rise -i ./input.json -o ./output.json --config ./vars.hcl

The contents of output.json should now read:

{
  "i": 6
}

Advanced Usage

There are a number of interpolation functions that can be used to perform complex actions. Here is a quick example to showcase a few of them.

Our config file, vars.hcl:

variable "animals" {
  value = ["dog", "cat", "cow"]
}

template "title" {
  content = "We are going to list some animals now!"
}

Our template file, input.txt:

${tmpl.title}

${join(", ", var.animals)}

Let’s run rise!

rise -i ./input.txt -o ./output.txt --config ./vars.hcl

The result:

We are going to list some animals now!

dog, cat, cow

There are many other interpolation functions that what was shown here. They can all be found in the rise docs.

If you wanted to know more about rise checkout the GitHub page, and the docs.