Skip to content

Commit a3486d2

Browse files
committed
Added variables to command line utility #39
1 parent bb00efd commit a3486d2

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

bin/netjsonconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
import os
34
import sys
45
import six
56
import argparse
@@ -143,6 +144,7 @@ def print_output(output):
143144
args = parser.parse_args()
144145
config = _load(args.config)
145146
templates = [_load(template) for template in args.templates]
147+
context = dict(os.environ)
146148
method = args.method
147149
method_arguments = parse_method_arguments(args.args)
148150

@@ -153,7 +155,7 @@ backends = {
153155

154156
backend_class = backends[args.backend]
155157
try:
156-
instance = backend_class(config, templates=templates)
158+
instance = backend_class(config, templates=templates, context=context)
157159
except TypeError as e:
158160
print('netjsonconfig: invalid JSON passed in config or templates')
159161
sys.exit(2)

docs/source/general/basics.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ it's possible to pass multiple templates that will be added one on top of the
271271
other to build the resulting *configuration dictionary*, allowing to reduce or
272272
even eliminate repetitions.
273273

274+
.. _configuration_variables:
275+
274276
Context: configuration variables
275277
--------------------------------
276278

docs/source/general/commandline_utility.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,17 @@ Here's the common use cases explained::
6767
Using templates::
6868

6969
netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m render
70+
71+
Environment variables
72+
---------------------
73+
74+
*Environment variables* are automatically passed to the ``context`` argument (if you don't
75+
know what this argument does please read ":ref:`configuration_variables`"), therefore
76+
you can reference environment variables inside *configurations* and *templates*::
77+
78+
export HOSTNAME=freedom
79+
netjsonconfig -c '{"general": { "hostname": "${HOSTNAME}" }}' -b openwrt -m render
80+
81+
You can also avoid using ``export`` and write everything in a one line command::
82+
83+
PORT=2009; netjsonconfig -c config.json -t template1.json -b openwrt -m render

tests/test_bin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ def test_generate_redirection(self):
121121
tar = tarfile.open('test.tar.gz', 'r')
122122
self.assertEqual(len(tar.getmembers()), 1)
123123
tar.close()
124+
125+
def test_context(self):
126+
config = json.dumps({'general': {'description': '${DESC}'}})
127+
command = "export DESC=testdesc; netjsonconfig --config '{0}' -b openwrt -m render".format(config)
128+
output = subprocess.check_output(command, shell=True).decode()
129+
self.assertNotIn('${DESC}', output)
130+
self.assertIn('testdesc', output)

0 commit comments

Comments
 (0)