Giter Club home page Giter Club logo

jin2cli's Introduction

jin2cli

Build Status

json + jinja2 template == file

Install it

pip install jin2cli

Use it

jin2cli takes json data and passes it into a jinja2 template under the data variable.

jin2cli --help

Simple stuff

$ echo '{"name": "Tom"}' > my_json
$ echo 'Hello {{ data.name }}' > my_template
$ jin2cli my_template my_json
Hello Tom

jin2cli will output to stdout if no output file is provided.

Intermediate stuff

$ echo 'server { server_name {{ data.name }}; location / { echo {{ data.message }}; } }' > my_template
$ echo '{"name": "frontend", "message": "Hello World"}' > my_json
$ jin2cli my_template my_json -o server.conf && service nginx reload
 * Reloading nginx configuration nginx
   ...done.
$ !!
data + template already == test

If an output file is provided jin2cli will output to that file. jin2cli will exit with a status of 1 if the result will not change the current file, meaning commands like nginx reloads can be chained and executed only when changes occur.

Intermediate stuff with an API request thrown in

$ curl http://someapi/app_data.json
{"name": "frontend", "message": "Hello World"}
$ echo 'server { server_name {{ data.name }}; location / { echo {{ data.message }}; } }' > my_template
$ jin2cli my_template <(curl http://someapi/app_data.json) -o server.conf && service nginx reload
 * Reloading nginx configuration nginx
   ...done.
$ !!
data + template already == test

Anonymous pipes are awesome and people don't use them enough. Anywhere a file is used for input (or output) an anonymous pipe can build that file on the fly from any other command.

(Maybe too) advanced stuff

$ jin2cli \
    <(curl https://raw.githubusercontent.com/tommyvn/jin2cli/master/examples/docker_to_nginx/nginx.template) \
    <(echo '{"node": "app-1", "containers": '$(curl https://raw.githubusercontent.com/tommyvn/jin2cli/master/examples/docker_to_nginx/docker.json | python -c 'import json; import sys; print json.dumps([ dict(map(lambda (k, v): (k, v) if k != "Names" else ("Name", v[0].split("_")[0][1:]), c.iteritems())) for c in json.load(sys.stdin) ]);')'}') \
  -o servers.conf && service nginx reload
 * Reloading nginx configuration nginx
   ...done.
$ !!
data + template already == test

Anonymous pipes mean data for templates and json can come from anywhere, including json APIs like Docker. This example command uses an nginx template on github and example output from the /v1.12/containers/json docker API endpoint parsed thru a Python one-liner to generate an nginx config file which will route traffic to containers based on their names. It will reload nginx but only if the nginx config file changed.

jin2cli's People

Contributors

tommyvn avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

osterzel

jin2cli's Issues

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.