Permalink
Browse files

Added variables to command line utility #39

  • Loading branch information...
nemesisdesign committed Feb 16, 2016
1 parent bb00efd commit a3486d29185bb5eb8ed5954897707b525894cf1b
Showing with 26 additions and 1 deletion.
  1. +3 −1 bin/netjsonconfig
  2. +2 −0 docs/source/general/basics.rst
  3. +14 −0 docs/source/general/commandline_utility.rst
  4. +7 −0 tests/test_bin.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import os
import sys
import six
import argparse
@@ -143,6 +144,7 @@ def print_output(output):
args = parser.parse_args()
config = _load(args.config)
templates = [_load(template) for template in args.templates]
context = dict(os.environ)
method = args.method
method_arguments = parse_method_arguments(args.args)
@@ -153,7 +155,7 @@ backends = {
backend_class = backends[args.backend]
try:
instance = backend_class(config, templates=templates)
instance = backend_class(config, templates=templates, context=context)
except TypeError as e:
print('netjsonconfig: invalid JSON passed in config or templates')
sys.exit(2)
@@ -271,6 +271,8 @@ it's possible to pass multiple templates that will be added one on top of the
other to build the resulting *configuration dictionary*, allowing to reduce or
even eliminate repetitions.
.. _configuration_variables:
Context: configuration variables
--------------------------------
@@ -67,3 +67,17 @@ Here's the common use cases explained::
Using templates::
netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m render
Environment variables
---------------------
*Environment variables* are automatically passed to the ``context`` argument (if you don't
know what this argument does please read ":ref:`configuration_variables`"), therefore
you can reference environment variables inside *configurations* and *templates*::
export HOSTNAME=freedom
netjsonconfig -c '{"general": { "hostname": "${HOSTNAME}" }}' -b openwrt -m render
You can also avoid using ``export`` and write everything in a one line command::
PORT=2009; netjsonconfig -c config.json -t template1.json -b openwrt -m render
@@ -121,3 +121,10 @@ def test_generate_redirection(self):
tar = tarfile.open('test.tar.gz', 'r')
self.assertEqual(len(tar.getmembers()), 1)
tar.close()
def test_context(self):
config = json.dumps({'general': {'description': '${DESC}'}})
command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render".format(config)
output = subprocess.check_output(command, shell=True).decode()
self.assertNotIn('${DESC}', output)
self.assertIn('testdesc', output)

0 comments on commit a3486d2

Please sign in to comment.