Skip to content

Commit d07ce54

Browse files
authored
Quote all string values to reduce probability of compose syntax errors (#51)
* Fix a bug where strings with special characters are not quoted Before: ``` ... services: <service>: .... logging: options: tag: {{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}} max-file: 3 ... ``` and docker-compose up fails after: ``` ... services: <service>: .... logging: options: tag: "{{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}}" max-file: "3" ... ``` and docker-compose up works * Remove no longer necessary workarounds
1 parent 0aa4522 commit d07ce54

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

autocompose.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,15 @@ def render(struct, args, networks, volumes):
117117
if args.version == 1:
118118
pyaml.p(OrderedDict(struct))
119119
else:
120-
ans = {"version": '"3.6"', "services": struct}
120+
ans = {"version": '3.6', "services": struct}
121121

122122
if networks is not None:
123123
ans["networks"] = networks
124124

125125
if volumes is not None:
126126
ans["volumes"] = volumes
127127

128-
pyaml.p(OrderedDict(ans))
129-
130-
131-
def is_date_or_time(s: str):
132-
for parse_func in [datetime.date.fromisoformat, datetime.datetime.fromisoformat]:
133-
try:
134-
parse_func(s.rstrip("Z"))
135-
return True
136-
except ValueError:
137-
pass
138-
return False
139-
140-
141-
def fix_label(label: str):
142-
return f"'{label}'" if is_date_or_time(label) else label
128+
pyaml.p(OrderedDict(ans), string_val_style='"')
143129

144130

145131
def generate(cname, createvolumes=False):
@@ -171,7 +157,7 @@ def generate(cname, createvolumes=False):
171157
"environment": cattrs.get("Config", {}).get("Env", None),
172158
"extra_hosts": cattrs.get("HostConfig", {}).get("ExtraHosts", None),
173159
"image": cattrs.get("Config", {}).get("Image", None),
174-
"labels": {label: fix_label(value) for label, value in cattrs.get("Config", {}).get("Labels", {}).items()},
160+
"labels": cattrs.get("Config", {}).get("Labels", {}),
175161
"links": cattrs.get("HostConfig", {}).get("Links"),
176162
#'log_driver': cattrs.get('HostConfig']['LogConfig']['Type'],
177163
#'log_opt': cattrs.get('HostConfig']['LogConfig']['Config'],

0 commit comments

Comments
 (0)