/Jinja-Recurse

Jinja Recursive Templating for the CLI

Primary LanguagePythonMIT LicenseMIT

jinjarecurse CLI tool


PyPI GitHub Workflow Status GitHub PyPI - Downloads


Jinja Recursive Templating for the CLI. Recursively template one file or many folders of many files like a config management languages allow, without the whole config management language. Useful if you're switching from managing an application from config management to just docker and need some simple templating logic.

Installation

pip install jinjarecurse

Example Usage

$ jinjarecurse --help
 jinjarecurse (CLI)

Usage:
    jinjarecurse --vars=VARS_FILE --input=INPUT_PATH --output=OUTPUT_PATH

Options:
    -v <file>, --vars   <file>
    -i <file>, --input  <file>
    -o <file>, --output <file>

Single file

Given a config file containing your variables e.g. vars.yaml:

#comment: not available
root: /
number: 1
dictionary:
    street: 123 North Ave
    city: New York
    state: New York
list:
    - ABC
    - DEF
    - HJK
layer_1:
    layer_2:
        layer_3: last

And an input file jinja2 template e.g. i_file:

# Top level
{{root}}
{{number}}
{{dictionary}}
{{list}}
{{layer_1}}

# Nested data

{{dictionary.street}}
{{dictionary.city}}
{{dictionary.state}}
{{layer_1.layer_2.layer_3}}

You can populate it and specify an output filepath e.g. o_file:

$ jinjarecurse -v example/vars.yaml -i example/i_file -o example/o_file
WARNING: example/o_file (output) exists and any conflicting files will be overwritten
Writing from example/i_file to example/o_file

Contents of the output file e.g. o_file:

# Top level
/
1
{'street': '123 North Ave', 'city': 'New York', 'state': 'New York'}
['ABC', 'DEF', 'HJK']
{'layer_2': {'layer_3': 'last'}}

# Nested data

123 North Ave
New York
New York
last

Directory

You can also template an entire directory e.g. i_dir at once. Note the output files in the output directory will maintain the filenames from the input directory:

$ jinjarecurse -v example/vars.yaml -i example/i_dir -o example/o_dir
Writing from example/i_dir/i_file to example/o_dir/i_file
Writing from example/i_dir/i_file_1 to example/o_dir/i_file_1
Writing from example/i_dir/i_file_2 to example/o_dir/i_file_2

Tests

To run the unit tests, first install the dependencies:

$ pipenv install --dev .

Then invoke pytest:

$ pipenv run py.test -vvvs

Changelog

Please see the Releases and CHANGELOG.md.