j2cli — Templating Shell Scripts
j2cli is a simple but awesome tool that provides an easy way to template shell scripts, it uses jinja2 as a templating engine
$ sudo pip install j2cli
j2cli supports templating from various sources like
- INI files
- JSON
- Environment Variables
- YAML
I will not provide examples on how to use the various template sources, since excellent documentation can be found at the projects homepage: https://github.com/kolypto/j2cli
Instead i will provide an example with good practices on How to temporary modify some configuration file parameters using environment variables just before execution, this can be considered as way to convert a tool that does not accept command line arguments, but only a configuration file to a flexible tool that accepts command line arguments.
Scenario: The program we use accepts username and password in configuration file and requires each user to modify the contents of the file before they use it, plus that its a security risk, someone might forget to delete his password after using the tool, the configuration file (myconf.ini) has the following format
[CREDENTIALS]
USER: myusername
PASS: mypassword
Solution:
- Copy myconf.ini to myconf.j2
$ cp myconf.ini myconf.j2
- Edit myconf.j2 and replace myusername and…