mustang

A command-line tool for mustache template, generating outputs from various data sources

npm install mustang
4 downloads in the last week
12 downloads in the last month

Mustang

Build Status

A command-line tool for mustache template, generating outputs from various data sources

Abstract

Mustang is a command-line tool to generate desired outputs from various data sources (e.g. CSV/JSON file, URL, MySQL, MongoDB), by applying mustache template.

Install

  $ npm install -g mustang

or

  $ git clone git://github.com/stomita/mustang.git 
  $ cd mustang
  $ npm link

Usage

Following is a very simple command to generate output to stdout from specified mustache template by using the input file as its templating context.

  $ mustang -t <template_file> -i <input_file>

Following is an example to output html from a CSV file input.

  $ cat input.csv
  NO,TITLE
  1,"Hello, World"
  2,"Apple, Orange, and Banana"
  3,"Thank you!"

  $ cat template.mustache
  {{#.}}
  <div>{{NO}}: {{TITLE}}</div>
  {{/.}}

  $ mustang -t template.mustache -i input.csv
  <div>1: Hello, World</div>
  <div>2: Apple, Orange, and Banana</div>
  <div>3: Thank you!</div>

Currently input file must be formatted in CSV or JSON. By default mustang checks its file extension and detect its format. You can add '-f' option to specify input file format explicitly.

  $ cat input.txt
  [
    { "NO": 1, "TITLE": "Hello, World" },
    { "NO": 2, "TITLE": "Apple, Orange, and Banana" },
    { "NO": 3, "TITLE": "Thank you!" }
  ]

  $ mustang -t template.mustache -i input.txt -f json
  <div>1: Hello, World</div>
  <div>2: Apple, Orange, and Banana</div>
  <div>3: Thank you!</div>

You can also use stdin for piping.

  $ cat input.txt | mustang -t template.mustache -f json
  <div>1: Hello, World</div>
  <div>2: Apple, Orange, and Banana</div>
  <div>3: Thank you!</div>  

Adding '-o' option, mustang output is saved to specified file.

  $ mustang -t template.mustache -i input.csv -o output.html
  $ cat output.html
  <div>1: Hello, World</div>
  <div>2: Apple, Orange, and Banana</div>
  <div>3: Thank you!</div>  

If you want to output multiple files from each input records, add '-m' option and set output directory path in '-o' option.

  $ cat template.mustache
  <div>{{NO}}: {{TITLE}}</div>

  $ mkdir output
  $ mustang -t template.mustache -i input.csv -o output -m
  $ ls output
  output-1.html output-2.html output-3.html

  $ cat output/output-1.html
  <div>1: Hello, World</div>

By using "-u" option, mustang downloads the content directly from specified URL and use it as input source.

  $ mustang -t template.mustache -u "https://webservice.example.org/search?q=London" -f json

Also mustang can connnect to database and query records from database table/collection. Currently MySQL and MongoDB is supported for database.

Following example shows how to fetch all records in "emp" table in MySQL database.

  $ mustang -t template.mustache -d mysql://user:pass@hostname/database -c emp

You can directly pass a SQL to fetch records by "-q" option.

  $ mustang -t template.mustache -d mysql://user:pass@hostname/database -q 'SELECT * FROM emp WHERE deptno = 1234'

Following example shows how to fetch all records in "users" collection in MongoDB.

  $ mustang -t template.mustache -d mongodb://user:pass@hostname/database -c users

You can also set URL-style query string in "-q" option to specify both querying collection and filter.

  $ mustang -t template.mustache -d mongodb://user:pass@hostname/database -q 'users?type=internal&owner.name=john'

Change History

v0.1.1 (May 19, 2013):

  • Added support for MongoDB data source

v0.1.0 (May 19, 2013):

  • Initial Release
npm loves you