Skip to content
Snippets Groups Projects
Commit ed70bc0e authored by nimrod's avatar nimrod
Browse files

- Updated documentation, tests.

parent a02c13ad
Branches
No related tags found
No related merge requests found
Template Template
######## ########
A CLI tool for generating files from Jinja2 templates and environment variables. A CLI tool for generating files from Jinja2 templates and environment
variables.
Examples Examples
-------- --------
...@@ -11,6 +12,9 @@ Examples ...@@ -11,6 +12,9 @@ Examples
$ template -h $ template -h
usage: template [-h] [-o OUTPUT] [filename] usage: template [-h] [-o OUTPUT] [filename]
A CLI tool for generating files from Jinja2 templates and environment
variables.
positional arguments: positional arguments:
filename Input filename filename Input filename
...@@ -23,7 +27,7 @@ Examples ...@@ -23,7 +27,7 @@ Examples
Hello John. Hello John.
$ echo '{{ USER }}' > username.j2 $ echo '{{ USER }}' > username.j2
$ template --output username.txt username.j2 $ template --output username.txt username.j2
$ cat username $ cat username.txt
John John
...@@ -32,12 +36,12 @@ Jinja filters ...@@ -32,12 +36,12 @@ Jinja filters
The following Jinja filters were added: The following Jinja filters were added:
- to_yaml: Convert to yaml. - :code:`to_yaml`: Convert to yaml.
- from_yaml: Convert from yaml. - :code:`from_yaml`: Convert from yaml.
- to_json: Convert to json. - :code:`to_json`: Convert to json.
- from_json: Convert from json. - :code:`from_json`: Convert from json.
- pprint: Pretty print variable. - :code:`pprint`: Pretty print variable.
- combine: Combine 2 dictionaries. - :code:`combine`: Combine 2 dictionaries.
Example usage can be seen in :code:`tests.sh`. Example usage can be seen in :code:`tests.sh`.
......
...@@ -21,7 +21,9 @@ def render(template_string): ...@@ -21,7 +21,9 @@ def render(template_string):
def main(): def main():
parser = ArgumentParser() parser = ArgumentParser(
description='''A CLI tool for generating files from Jinja2 templates
and environment variables.''')
parser.add_argument('filename', parser.add_argument('filename',
help='Input filename', help='Input filename',
type=argparse.FileType('r'), type=argparse.FileType('r'),
......
...@@ -26,7 +26,7 @@ def pprint(value): ...@@ -26,7 +26,7 @@ def pprint(value):
return pformat(value) return pformat(value)
def combine(lefthand, righthand): def combine(default, override):
combined = lefthand.copy() combined = default.copy()
combined.update(righthand) combined.update(override)
return combined return combined
#!/bin/sh -e #!/bin/sh
set -eu
export infile="$(mktemp)" export infile="$(mktemp)"
export outfile="$(mktemp)" export outfile="$(mktemp)"
...@@ -36,5 +37,7 @@ echo '{{ [1, ] + [2, ] }}' > "$infile" ...@@ -36,5 +37,7 @@ echo '{{ [1, ] + [2, ] }}' > "$infile"
test "$(template $infile)" = "[1, 2]" test "$(template $infile)" = "[1, 2]"
echo Testing combining dictionaries. echo Testing combining dictionaries.
echo '{{ {"a": 1, "b": 2}|combine({"a": 11, "c": 33}) }}' > "$infile"
test "$(template $infile)" = "{'a': 11, 'c': 33, 'b': 2}"
rm "$infile" "$outfile" rm "$infile" "$outfile"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment