GithubHelp home page GithubHelp logo

cgmartin / envhandlebars Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 2.0 300 KB

Substitutes environment variables in Handlebars templates, useful for configuration files in Docker

JavaScript 85.96% Dockerfile 12.29% Shell 1.75%
docker config-template mustache-templates handlebars

envhandlebars's Introduction

envhandlebars

Dependency Status npm version Build Status

A simple templating utility, akin to envsubst, but using Handlebars for more complex logic.

Environment variables are used as the data input to a Handlebars template:

$ export NAME=world
$ echo "Hello {{NAME}}" | envhandlebars
Hello world

This is particularly useful for dynamic configuration files in Docker containers and other 12-Factor applications.

Install

$ npm install -g envhandlebars

Usage

Redirect a Mustache template file into stdin of envhandlebars and redirect the rendered stdout to a file:

Usage:
    envhandlebars [options] < templatefile > renderedfile

Options:
    --array-var-prefix="PRE_"  process arrays only for vars with the specified prefix [Default: all]
    --no-arrays                disable variable name array processing
    -h, --help                 print usage information

Complex Examples

Conditionals

Mustache conditionals can be used as is...

Template:

{{#if WORLD}}Hello {{WORLD}}{{/if}}!

Env vars:

WORLD=world

Output:

Hello world!

See the if block helper docs page for more information.

Iterators

Mustache iterators require using a special environment variable naming convention...

Array of strings

Template:

{{#each PEOPLE}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}!

Env vars:

# Convention: {VARNAME}_{INDEX}
PEOPLE_0=Chris
PEOPLE_1=John
PEOPLE_2=Shayne

Output:

Chris, John, Shayne!

Array of objects

Template:

{{#each PEOPLE}}{{FIRST}} {{LAST}}{{#unless @last}}, {{/unless}}{{/each}}!

Env vars:

# Convention: {VARNAME}_{INDEX}_{PROPERTY}
PEOPLE_0_FIRST=Chris
PEOPLE_0_LAST=Martin
PEOPLE_1_FIRST=John
PEOPLE_1_LAST=Papa
PEOPLE_2_FIRST=Shayne
PEOPLE_2_LAST=Boyer

Output:

Chris Martin, John Papa, Shayne Boyer!

See the each block helper docs page for more information.

NOTE:

Some environments may have variables that conflict with this convention.

Use the --array-var-prefix=FOO_ option to enable iterators only for variables that start with FOO_.

Alternatively, iterator parsing can be completely disabled with the --no-arrays option.

Docker Usage

To generate configuration dynamically within a docker container, envhandlebars can be used before the main process starts. It can be done in the CMD or within a wrapper script.

Here is an example docker-compose.yml for a custom nginx container:

nginx:
  build: .
  volumes:
   - ./mysite.template:/etc/nginx/conf.d/mysite.template
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
  command: /bin/bash -c "envhandlebars < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

The mysite.template file may then contain variable references like this:

listen {{NGINX_PORT}};

You just need to make sure to install Node.js and envhandlebars (via npm) within your custom Dockerfile.

For debian-based images:

RUN apt-get update && apt-get install -y nodejs && npm i -g envhandlebars

For alpine-based images:

RUN apk add --update nodejs && npm i -g envhandlebars

Custom Helpers or Partials

(New in v1.3.0+)

Custom Mustache helpers and partials can be implemented by extending the envhandlebars module with your own Node.js wrapper script:

#!/usr/bin/env node
// Script: `myenvhandlebars`
'use strict';
var envhandlebars = require('envhandlebars');

// The Handlebars context is passed into this function
// for registering helpers, partials or other extensions.
function extendHandlebars(Handlebars) {
    Handlebars.registerHelper('fullName', function(first, last) {
        return last + ', ' + first;
    });
}

envhandlebars({
    extendHandlebars: extendHandlebars
});

See the custom helpers docs page for more information.

License

MIT License

envhandlebars's People

Contributors

cgmartin avatar dependabot[bot] avatar luisdavim avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

envhandlebars's Issues

Error when running in container clusters

When an application uses this library to handle environment variables and is ran in a container cluster (like Marathon or Kubernetes), where envvars set by such software could be PORT=<container_port> and PORT_<lb_port>=<container_port>, for the example variables of PORT=2345 and PORT_1234=2345 a following backtrace is thrown and the application errors out.

/usr/local/lib/node_modules/envhandlebars/index.js:74
                vars[match[1]][match[2]] = opts.env[key];
                                         ^
TypeError: Cannot create property '1234' on string '2345'
    at parseVar (/usr/local/lib/node_modules/envhandlebars/index.js:74:42)
    at /usr/local/lib/node_modules/envhandlebars/index.js:52:13
    at Array.forEach (<anonymous>)
    at getVars (/usr/local/lib/node_modules/envhandlebars/index.js:51:31)
    at applyTemplate (/usr/local/lib/node_modules/envhandlebars/index.js:40:35)
    at ConcatStream.<anonymous> (/usr/local/lib/node_modules/envhandlebars/node_modules/concat-stream/index.js:36:43)
    at ConcatStream.emit (events.js:164:20)
    at finishMaybe (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:607:14)
    at endWritable (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:615:3)
    at ConcatStream.Writable.end (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:571:41)
Converting: /opt/admin/env/cfg.json.tpl
/usr/local/lib/node_modules/envhandlebars/index.js:74
                vars[match[1]][match[2]] = opts.env[key];
                                         ^
TypeError: Cannot create property '1234' on string '2345'
    at parseVar (/usr/local/lib/node_modules/envhandlebars/index.js:74:42)
    at /usr/local/lib/node_modules/envhandlebars/index.js:52:13
    at Array.forEach (<anonymous>)
    at getVars (/usr/local/lib/node_modules/envhandlebars/index.js:51:31)
    at applyTemplate (/usr/local/lib/node_modules/envhandlebars/index.js:40:35)
    at ConcatStream.<anonymous> (/usr/local/lib/node_modules/envhandlebars/node_modules/concat-stream/index.js:36:43)
    at ConcatStream.emit (events.js:164:20)
    at finishMaybe (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:607:14)
    at endWritable (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:615:3)
    at ConcatStream.Writable.end (/usr/local/lib/node_modules/envhandlebars/node_modules/readable-stream/lib/_stream_writable.js:571:41)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.