Skip to content

Commit 2755246

Browse files
committed
allow additional data files
1 parent 39ec648 commit 2755246

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ access to environment variables.
2222
- Call `meta-compose` and it will create a docker-compose.yml in the current
2323
directory
2424

25+
```
26+
usage: meta-compose [-h] [-d DATAFILE]
27+
28+
optional arguments:
29+
-h, --help show this help message and exit
30+
-d DATAFILE, --datafile DATAFILE
31+
Use to specify data files in addition to meta-compose-
32+
data.yml. They must be JSON or YAML files.
33+
```
34+
2535
## Syntax of meta-compose.yml
2636

2737
- Everything in basic [Jinja2](http://jinja.pocoo.org/) is allowed.

metacompose/cli/main.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@
22
import os
33
import yaml
44
from jinja2 import Environment, FileSystemLoader, StrictUndefined
5+
import argparse
56

67
def env_override(value, key):
78
return os.getenv(key, value)
89

910
def main():
1011

12+
parser = argparse.ArgumentParser()
13+
1114
template_file_name = "meta-compose.yml"
12-
data_file_name = "meta-compose-data.yml"
15+
data_file_names = ["meta-compose-data.yml"]
1316

14-
jinja = Environment(loader=FileSystemLoader("."), undefined=StrictUndefined)
17+
parser.add_argument(
18+
"-d", "--datafile", action='append',
19+
help="Use to specify data files in addition to meta-compose-data.yml."
20+
" They must be JSON or YAML files.")
1521

16-
jinja.filters['env'] = env_override
22+
args = parser.parse_args()
1723

24+
data_file_names += args.datafile
25+
26+
jinja = Environment(loader=FileSystemLoader("."), undefined=StrictUndefined)
27+
jinja.filters['env'] = env_override
1828
template = jinja.get_template(template_file_name)
1929

30+
data = {}
31+
for data_file_name in data_file_names:
32+
if os.path.isfile(data_file_name):
33+
with open(data_file_name, "r") as fh:
34+
data.update(yaml.safe_load(fh))
2035

21-
if os.path.isfile(data_file_name):
22-
with open(data_file_name, "r") as fh:
23-
data = yaml.safe_load(fh)
24-
else:
25-
data = {}
2636

2737
composition = template.render(data)
2838

0 commit comments

Comments
 (0)