Skip to content

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!

License

Notifications You must be signed in to change notification settings

ashleydavis/figit

Repository files navigation

figit

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!

Or expand JSON and YAML templates using Handlebars template syntax.

I use this to produce Kubernetes configurations from templates.

If you like this project, please star this repo and support my work

Features

  • Produce data from JavaScript code.
  • Produce data from templated YAML and JSON files.
  • Use variables, environment variables and whatever data sources you like.
  • Use whatever npm packages you like.
  • Use YAML and JSON data files and standard input for Handlebars template data.

Install it

npm install -g figit

Command line reference

Evaluate file.js and output JSON:

figit file.js

Evaluate multiple files and output YAML:

figit file1 file2 fileN --output yaml

General use:

figit [file+] --output json | yaml [--stdin json | yaml] [--data <json-or-yaml-data-file>]

A simple JavaScript template

Create a template:

// myfile.js
module.exports = {
    /* Whatever code you want here to produce data. */
    data: {
        msg: "Hello world!",
    },
};

Instantiate data from the template:

figit myfile.js

JSON output:

{
    "data": {
        "msg": "Hello world!"
    }
}

Or output YAML:

figit myfile.js --output yaml

YAML output:

data:
    msg: "Hello world!"

A simple data template

Create a JSON template:

// myfile.json
{
    "data": {
        "msg": "{{MESSAGE}}",
    },
};

Or create a YAML template instead:

# myfile.yaml
data:
    msg: "{{MESSAGE}}"

Create a file to contain the Handlebars template data (this can be JSON or YAML as well):

// mydata.json
{
    "MESSAGE": "Hello world!"
}

Note: Template variables are also populated from environment variables, so you don't necessarily need to create a template data, you could instead set a MESSAGE environment variable like this:

export MESSAGE="Hello world"

Instantiate data from the template and output as JSON:

figit myfile.json --data mydata.json --output json

JSON output:

{
    "data": {
        "msg": "Hello world!"
    }
}

Or output YAML:

figit myfile.json --data mydata.json --output yaml

YAML output:

data:
    msg: "Hello world!"

You can also combine template data from a data file, environment variables and standard input.

Load template data from a file like this:

figit myfile.json --data mydata.json

Template data is overwritten by environment variables, so in this case a MESSAGE environment variable would override the MESSAGE field from the data file.

You can read template from standard input in JSON or YAML format like this:

figit myfile.json --stdin json < mydata.json

You can use this to pipe sensitive variables from one process to another without having to save them to disk.

You can combine the methods of loading template data:

figit myfile.json --data base-data.json --stdin json < override-data.json

This allows you to have multiple levels of data. Base template data is read from the file specified by --data. Environment variables can then override the base data. Finally data read from standard input provides the final values for template data.

A complex JavaScript template

Here's my usecase for Figit, templating Kubernetes configurations.

// mydeployment.js
const APP_NAME = process.env.APP_NAME;
if (!APP_NAME) {
    throw new Error(`Expected environment variable ${APP_NAME}`);
}

module.exports = {
    apiVersion: "apps/v1",
    kind: "Deployment",
    metadata: {
        name: ,
        labels: {
            app: APP_NAME,
        },
    },
    spec: {
        replicas: 3,
        selector: {
            matchLabels: {
                app: APP_NAME,
            },
        },
        template: {
            metadata: {
                labels: {
                    app: APP_NAME,
                },
            },
            spec: {
                containers: [
                    {
                        name: APP_NAME,
                        image: IMAGE_NAME,
                        ports: [
                            {
                                containerPort: 80,
                            },
                        ],
                    },
                ],
            },
        },
    },
};

Then in my deployment pipeline:

export APP_NAME=myapp
export IMAGE_NAME=myimage:latest
figit mydeployment.js | kubectl apply -f -

About

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published