Coder Social home page Coder Social logo

3rawkz / j2cli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kolypto/j2cli

0.0 1.0 0.0 380 KB

Jinja2 Command-Line Tool, reworked

Home Page: http://pypi.python.org/pypi/j2cli

License: BSD 2-Clause "Simplified" License

Makefile 3.53% Python 96.47%

j2cli's Introduction

Build Status

j2cli - Jinja2 Command-Line Tool

j2cli is a command-line tool for templating in shell-scripts, leveraging the Jinja2 library.

Features:

  • Jinja2 templating
  • Allows to use environment variables! Hello Docker :)
  • INI, YAML, JSON data sources supported

Inspired by mattrobenolt/jinja2-cli

Installation

pip install j2cli

To enable the YAML support with pyyaml:

pip install j2cli[yaml]

Usage

Compile a template using INI-file data source:

$ j2 config.j2 data.ini

Compile using JSON data source:

$ j2 config.j2 data.json

Compile using YAML data source (requires PyYAML):

$ j2 config.j2 data.yaml

Compile using JSON data on stdin:

$ curl http://example.com/service.json | j2 --format=json config.j2

Compile using environment variables (hello Docker!):

$ j2 config.j2

Or even read environment variables from a file:

$ j2 --format=env config.j2 data.env

Reference

j2 accepts the following arguments:

  • template: Jinja2 template file to render
  • data: (optional) path to the data used for rendering. The default is -: use stdin

Options:

  • --format, -f: format for the data file. The default is ?: guess from file extension.

There is some special behavior with environment variables:

  • When data is not provided (data is -), --format defaults to env and thus reads environment variables
  • When --format=env, it can read a special "environment variables" file made like this: env > /tmp/file.env

Formats

env

Data input from environment variables.

Render directly from the current environment variable values:

$ j2 config.j2

Or alternatively, read the values from a file:

NGINX_HOSTNAME=localhost
NGINX_WEBROOT=/var/www/project
NGINX_LOGS=/var/log/nginx/

And render with:

$ j2 config.j2 data.env
$ env | j2 --format=env config.j2.

This is especially useful with Docker to link containers together.

ini

INI data input format.

data.ini:

[nginx]
hostname=localhost
webroot=/var/www/project
logs=/var/log/nginx/

Usage:

$ j2 config.j2 data.ini
$ cat data.ini | j2 --format=ini config.j2

json

JSON data input format

data.json:

{
    "nginx":{
        "hostname": "localhost",
        "webroot": "/var/www/project",
        "logs": "/var/log/nginx/"
    }
}

Usage:

$ j2 config.j2 data.json
$ cat data.json | j2 --format=ini config.j2

yaml

YAML data input format.

data.yaml:

nginx:
  hostname: localhost
  webroot: /var/www/project
  logs: /var/log/nginx

Usage:

$ j2 config.j2 data.yml
$ cat data.yml | j2 --format=yaml config.j2

Extras

Filters

docker_link(value, format='{addr}:{port}')

Given a Docker Link environment variable value, format it into something else.

This first parses a Docker Link value like this:

DB_PORT=tcp://172.17.0.5:5432

Into a dict:

{
  'proto': 'tcp',
  'addr': '172.17.0.5',
  'port': '5432'
}

And then uses format to format it, where the default format is '{addr}:{port}'.

More info here: Docker Links

j2cli's People

Contributors

anshumanb avatar kolypto avatar mattrobenolt avatar michilu avatar

Watchers

 avatar

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.