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
This commit is contained in:
Konstantin Köhring
2022-11-14 07:07:13 +01:00
committed by GitHub
parent 0aa4522143
commit d07ce54dd0

View File

@@ -117,7 +117,7 @@ def render(struct, args, networks, volumes):
if args.version == 1:
pyaml.p(OrderedDict(struct))
else:
ans = {"version": '"3.6"', "services": struct}
ans = {"version": '3.6', "services": struct}
if networks is not None:
ans["networks"] = networks
@@ -125,21 +125,7 @@ def render(struct, args, networks, volumes):
if volumes is not None:
ans["volumes"] = volumes
pyaml.p(OrderedDict(ans))
def is_date_or_time(s: str):
for parse_func in [datetime.date.fromisoformat, datetime.datetime.fromisoformat]:
try:
parse_func(s.rstrip("Z"))
return True
except ValueError:
pass
return False
def fix_label(label: str):
return f"'{label}'" if is_date_or_time(label) else label
pyaml.p(OrderedDict(ans), string_val_style='"')
def generate(cname, createvolumes=False):
@@ -171,7 +157,7 @@ def generate(cname, createvolumes=False):
"environment": cattrs.get("Config", {}).get("Env", None),
"extra_hosts": cattrs.get("HostConfig", {}).get("ExtraHosts", None),
"image": cattrs.get("Config", {}).get("Image", None),
"labels": {label: fix_label(value) for label, value in cattrs.get("Config", {}).get("Labels", {}).items()},
"labels": cattrs.get("Config", {}).get("Labels", {}),
"links": cattrs.get("HostConfig", {}).get("Links"),
#'log_driver': cattrs.get('HostConfig']['LogConfig']['Type'],
#'log_opt': cattrs.get('HostConfig']['LogConfig']['Config'],