| 1 |
[](https://travis-ci.org/kolypto/j2cli) |
| 2 |
[](.travis.yml) |
| 3 |
|
| 4 |
j2cli - Jinja2 Command-Line Tool |
| 5 |
================================ |
| 6 |
|
| 7 |
`j2cli` is a command-line tool for templating in shell-scripts, |
| 8 |
leveraging the [Jinja2](http://jinja.pocoo.org/docs/) library. |
| 9 |
|
| 10 |
Features: |
| 11 |
|
| 12 |
* Jinja2 templating |
| 13 |
* INI, YAML, JSON data sources supported |
| 14 |
* Allows the use of environment variables in templates! Hello [Docker](http://www.docker.com/) :) |
| 15 |
|
| 16 |
Inspired by [mattrobenolt/jinja2-cli](https://github.com/mattrobenolt/jinja2-cli) |
| 17 |
|
| 18 |
## Installation |
| 19 |
|
| 20 |
``` |
| 21 |
pip install j2cli |
| 22 |
``` |
| 23 |
|
| 24 |
To enable the YAML support with [pyyaml](http://pyyaml.org/): |
| 25 |
|
| 26 |
``` |
| 27 |
pip install j2cli[yaml] |
| 28 |
``` |
| 29 |
|
| 30 |
## Tutorial |
| 31 |
|
| 32 |
Suppose, you want to have an nginx configuration file template, `nginx.j2`: |
| 33 |
|
| 34 |
{% raw %}```jinja2 |
| 35 |
server { |
| 36 |
listen 80; |
| 37 |
server_name {{ nginx.hostname }}; |
| 38 |
|
| 39 |
root {{ nginx.webroot }}; |
| 40 |
index index.htm; |
| 41 |
} |
| 42 |
```{% endraw %} |
| 43 |
|
| 44 |
And you have a JSON file with the data, `nginx.json`: |
| 45 |
|
| 46 |
```json |
| 47 |
{ |
| 48 |
"nginx":{ |
| 49 |
"hostname": "localhost", |
| 50 |
"webroot": "/var/www/project" |
| 51 |
} |
| 52 |
} |
| 53 |
``` |
| 54 |
|
| 55 |
This is how you render it into a working configuration file: |
| 56 |
|
| 57 |
```bash |
| 58 |
$ j2 -f json nginx.j2 nginx.json > nginx.conf |
| 59 |
``` |
| 60 |
|
| 61 |
The output is saved to `nginx.conf`: |
| 62 |
|
| 63 |
``` |
| 64 |
server { |
| 65 |
listen 80; |
| 66 |
server_name localhost; |
| 67 |
|
| 68 |
root /var/www/project; |
| 69 |
index index.htm; |
| 70 |
} |
| 71 |
``` |
| 72 |
|
| 73 |
Alternatively, you can use the `-o nginx.conf` option. |
| 74 |
|
| 75 |
## Tutorial with environment variables |
| 76 |
|
| 77 |
Suppose, you have a very simple template, `person.xml`: |
| 78 |
|
| 79 |
{% raw %}```jinja2 |
| 80 |
<data><name>{{ name }}</name><age>{{ age }}</age></data> |
| 81 |
```{% endraw %} |
| 82 |
|
| 83 |
What is the easiest way to use j2 here? |
| 84 |
Use environment variables in your bash script: |
| 85 |
|
| 86 |
```bash |
| 87 |
$ export name=Andrew |
| 88 |
$ export age=31 |
| 89 |
$ j2 /tmp/person.xml |
| 90 |
<data><name>Andrew</name><age>31</age></data> |
| 91 |
``` |
| 92 |
|
| 93 |
## Using environment variables |
| 94 |
|
| 95 |
Even when you use yaml or json as the data source, you can always access environment variables |
| 96 |
using the `env()` function: |
| 97 |
|
| 98 |
{% raw %}```jinja2 |
| 99 |
Username: {{ login }} |
| 100 |
Password: {{ env("APP_PASSWORD") }} |
| 101 |
```{% endraw %} |
| 102 |
|
| 103 |
|
| 104 |
## Usage |
| 105 |
|
| 106 |
Compile a template using INI-file data source: |
| 107 |
|
| 108 |
$ j2 config.j2 data.ini |
| 109 |
|
| 110 |
Compile using JSON data source: |
| 111 |
|
| 112 |
$ j2 config.j2 data.json |
| 113 |
|
| 114 |
Compile using YAML data source (requires PyYAML): |
| 115 |
|
| 116 |
$ j2 config.j2 data.yaml |
| 117 |
|
| 118 |
Compile using JSON data on stdin: |
| 119 |
|
| 120 |
$ curl http://example.com/service.json | j2 --format=json config.j2 |
| 121 |
|
| 122 |
Compile using environment variables (hello Docker!): |
| 123 |
|
| 124 |
$ j2 config.j2 |
| 125 |
|
| 126 |
Or even read environment variables from a file: |
| 127 |
|
| 128 |
$ j2 --format=env config.j2 data.env |
| 129 |
|
| 130 |
Or pipe it: (note that you'll have to use the "-" in this particular case): |
| 131 |
|
| 132 |
$ j2 --format=env config.j2 - < data.env |
| 133 |
|
| 134 |
|
| 135 |
# Reference |
| 136 |
`j2` accepts the following arguments: |
| 137 |
|
| 138 |
* `template`: Jinja2 template file to render |
| 139 |
* `data`: (optional) path to the data used for rendering. |
| 140 |
The default is `-`: use stdin. Specify it explicitly when using env! |
| 141 |
|
| 142 |
Options: |
| 143 |
|
| 144 |
* `--format, -f`: format for the data file. The default is `?`: guess from file extension. |
| 145 |
* `--import-env VAR, -e EVAR`: import all environment variables into the template as `VAR`. |
| 146 |
To import environment variables into the global scope, give it an empty string: `--import-env=`. |
| 147 |
(This will overwrite any existing variables!) |
| 148 |
* `-o outfile`: Write rendered template to a file |
| 149 |
* `--undefined`: Allow undefined variables to be used in templates (no error will be raised) |
| 150 |
|
| 151 |
* `--filters filters.py`: Load custom Jinja2 filters and tests from a Python file. |
| 152 |
Will load all top-level functions and register them as filters. |
| 153 |
This option can be used multiple times to import several files. |
| 154 |
* `--tests tests.py`: Load custom Jinja2 filters and tests from a Python file. |
| 155 |
* `--customize custom.py`: A Python file that implements hooks to fine-tune the j2cli behavior. |
| 156 |
This is fairly advanced stuff, use it only if you really need to customize the way Jinja2 is initialized. |
| 157 |
See [Customization](#customization) for more info. |
| 158 |
|
| 159 |
There is some special behavior with environment variables: |
| 160 |
|
| 161 |
* When `data` is not provided (data is `-`), `--format` defaults to `env` and thus reads environment variables |
| 162 |
* When `--format=env`, it can read a special "environment variables" file made like this: `env > /tmp/file.env` |
| 163 |
|
| 164 |
## Formats |
| 165 |
|
| 166 |
{% for name, format in formats|dictsort() %} |
| 167 |
### {{ name }} |
| 168 |
{{ format.doc }} |
| 169 |
{% endfor %} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
Extras |
| 174 |
====== |
| 175 |
|
| 176 |
## Filters |
| 177 |
|
| 178 |
{% for name, filter in extras.filters|dictsort() %} |
| 179 |
### `{{ filter.qsignature }}` |
| 180 |
{{ filter.doc }} |
| 181 |
{% endfor %} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
Customization |
| 186 |
============= |
| 187 |
|
| 188 |
j2cli now allows you to customize the way the application is initialized: |
| 189 |
|
| 190 |
* Pass additional keywords to Jinja2 environment |
| 191 |
* Modify the context before it's used for rendering |
| 192 |
* Register custom filters and tests |
| 193 |
|
| 194 |
This is done through *hooks* that you implement in a customization file in Python language. |
| 195 |
Just plain functions at the module level. |
| 196 |
|
| 197 |
The following hooks are available: |
| 198 |
|
| 199 |
* `j2_environment_params() -> dict`: returns a `dict` of additional parameters for |
| 200 |
[Jinja2 Environment](http://jinja.pocoo.org/docs/2.10/api/#jinja2.Environment). |
| 201 |
* `j2_environment(env: Environment) -> Environment`: lets you customize the `Environment` object. |
| 202 |
* `alter_context(context: dict) -> dict`: lets you modify the context variables that are going to be |
| 203 |
used for template rendering. You can do all sorts of pre-processing here. |
| 204 |
* `extra_filters() -> dict`: returns a `dict` with extra filters for Jinja2 |
| 205 |
* `extra_tests() -> dict`: returns a `dict` with extra tests for Jinja2 |
| 206 |
|
| 207 |
All of them are optional. |
| 208 |
|
| 209 |
The example customization.py file for your reference: |
| 210 |
|
| 211 |
```python |
| 212 |
{% include "tests/resources/customize.py" %} |
| 213 |
``` |
| 214 |
|