Skip to content

Commit 6381090

Browse files
committed
Format properly date/datetime labels
1 parent e32c9d4 commit 6381090

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

autocompose.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /usr/bin/env python
2-
2+
import datetime
33
import sys, argparse, pyaml, docker
44
from collections import OrderedDict
55

@@ -26,7 +26,21 @@ def render(struct, args, networks):
2626
pyaml.p(OrderedDict(struct))
2727
else:
2828
pyaml.p(OrderedDict({'version': '"3"', 'services': struct, 'networks': networks}))
29-
29+
30+
31+
def is_date_or_time(s: str):
32+
for parse_func in [datetime.date.fromisoformat, datetime.datetime.fromisoformat]:
33+
try:
34+
parse_func(s.rstrip('Z'))
35+
return True
36+
except ValueError:
37+
pass
38+
return False
39+
40+
41+
def fix_label(label: str):
42+
return f"'{label}'" if is_date_or_time(label) else label
43+
3044

3145
def generate(cname):
3246
c = docker.from_env()
@@ -57,7 +71,7 @@ def generate(cname):
5771
'environment': cattrs['Config']['Env'],
5872
'extra_hosts': cattrs['HostConfig']['ExtraHosts'],
5973
'image': cattrs['Config']['Image'],
60-
'labels': cattrs['Config']['Labels'],
74+
'labels': {label: fix_label(value) for label, value in cattrs['Config']['Labels'].items()},
6175
'links': cattrs['HostConfig']['Links'],
6276
#'log_driver': cattrs['HostConfig']['LogConfig']['Type'],
6377
#'log_opt': cattrs['HostConfig']['LogConfig']['Config'],

0 commit comments

Comments
 (0)