Skip to content

Commit

Permalink
Merge pull request #3 from dinuta/code-improvements
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
dinuta authored May 30, 2019
2 parents dd5e681 + c1f27f1 commit a008e12
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
15 changes: 14 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,17 @@ before_install:

script:
- docker exec jinja2docker python3 -m unittest discover $SCRIPTS_DIR "*_test.py"
- docker-compose down -v
- docker-compose down -v

- docker run -i
-v $TRAVIS_BUILD_DIR/templates:/data
-v $TRAVIS_BUILD_DIR/variables:/variables -e TEMPLATE=standalone.j2
-e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest

- docker run --entrypoint jinja2
-v $TRAVIS_BUILD_DIR/templates:/data
-v $TRAVIS_BUILD_DIR/variables:/variables
dinutac/jinja2docker:latest
/data/json.j2 /variables/json.json --format=json


17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ docker run -i -v <your_jinja2_template_folder>:/data \

Example:
```
run -i -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \
docker run -i -v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \
-v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables -e TEMPLATE=standalone.j2 \
-e VARIABLES=variables.yml -e DATABASE=mysql56 dinutac/jinja2docker:latest > docker-compose.yml
-e VARIABLES=variables.yml -e DATABASE=mysql56 -e IMAGE=latest dinutac/jinja2docker:latest > docker-compose.yml
```

### Example template ```json-template.j2```
Expand Down Expand Up @@ -73,10 +73,12 @@ Example {{yourYamlVariableHere | yaml | safe }}
The recommendation is either paste selectively smaller chunks of yaml or use json whenever possible.

## Latest updates

### Integrated Jinja2 Cli

https://github.com/mattrobenolt/jinja2-cli

#### 1. Generate template with container up
- run the docker compose: ``docker-compose up``
- run the docker exec command with the jinja2-cli params as per documentation: https://github.com/mattrobenolt/jinja2-cli by specifying template and variables folders.

Expand All @@ -87,3 +89,14 @@ Example:
```
docker exec -e DATABASE=mysql56 -e IMAGE=latest jinja2docker jinja2 /data/standalone.j2 /variables/variables.yml --format=yml > docker-compose.yml
```

#### 2. Hybrid call
```
docker run --entrypoint jinja2 \
-v C:\Users\cdinuta\IdeaProjects\jinja2docker\templates:/data \
-v C:\Users\cdinuta\IdeaProjects\jinja2docker\variables:/variables \
dinutac/jinja2docker:latest \
/data/json.j2 /variables/json.json --format=json
```

! observe that jinja2 is called before image name and the arguments after
3 changes: 2 additions & 1 deletion render.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def env_override(self, value, key):
return os.getenv(key, value)

def rend_template(self, argv):
data = yaml.load(open(self.VARS_DIR + "/" + self.variables, closefd=True), Loader=yaml.Loader)
with open(self.VARS_DIR + "/" + self.variables, closefd=True) as f:
data = yaml.load(f, Loader=yaml.Loader)

self.env.filters['yaml'] = self.yaml_filter
self.env.globals["environ"] = lambda key: os.environ.get(key)
Expand Down
8 changes: 4 additions & 4 deletions render_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_json(self):
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES'])

template = yaml.load(r.rend_template("dummy"), Loader=yaml.Loader)

data = yaml.load(open(r.VARS_DIR + "/" + r.variables, closefd=True), Loader=yaml.Loader)
with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f:
data = yaml.load(f, Loader=yaml.Loader)
self.assertEqual(template.get("os"), data.get("os"), )
self.assertEqual(template.get("version"), data.get("version"))
self.assertEqual(template.get("installed_apps"), data.get("installed_apps"))
Expand All @@ -24,8 +24,8 @@ def test_yml(self):
r = Render(os.environ['TEMPLATE'], os.environ['VARIABLES'])

template = yaml.load(r.rend_template("dummy"), Loader=yaml.Loader)

data = yaml.load(open(r.VARS_DIR + "/" + r.variables, closefd=True), Loader=yaml.Loader)
with open(r.VARS_DIR + "/" + r.variables, closefd=True) as f:
data = yaml.load(f, Loader=yaml.Loader)
self.assertEqual(template, data)


Expand Down
3 changes: 2 additions & 1 deletion variables/json.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"os" : "windows",
"version": 10
"version": 10,
"installed_apps": "json"
}
2 changes: 1 addition & 1 deletion variables/yml.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
installed_apps: "Intellij, Chrome"
installed_apps: "yml"
os: windows
version: 10

0 comments on commit a008e12

Please sign in to comment.