Merge branch 'garotosopa-devel' into devel

This commit is contained in:
Muayyad alsadi 2021-05-06 00:49:54 +03:00
commit 905914b0dc
4 changed files with 55 additions and 26 deletions

View File

@ -146,25 +146,19 @@ def fix_mount_dict(mount_dict, proj_name, srv_name):
# ${VARIABLE?err} raise error if not set # ${VARIABLE?err} raise error if not set
# $$ means $ # $$ means $
var_re = re.compile(r'\$(\{(?:[^\s\$:\-\}]+)\}|(?:[^\s\$\{\}]+))') var_re = re.compile(r"""
var_def_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?-([^\}]*)\}') \$(?:
var_err_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?\?([^\}]*)\}') (?P<escaped>\$) |
(?P<named>[_a-zA-Z][_a-zA-Z0-9]*) |
def dicts_get(dicts, key, fallback='', fallback_empty=False): (?:{
""" (?P<braced>[_a-zA-Z][_a-zA-Z0-9]*)
get the given key from any dict in dicts, trying them one by one (?:
if not found in any, then use fallback, if fallback is Exception raise is (?::?-(?P<default>[^}]+)) |
""" (?::?\?(?P<err>[^}]+))
value = None )?
for d in dicts: })
value = d.get(key, None) )
if value is not None: break """, re.VERBOSE)
if not value:
if fallback_empty or value is None:
value = fallback
if isinstance(value, Exception):
raise value
return value
def rec_subs(value, dicts): def rec_subs(value, dicts):
""" """
@ -173,13 +167,18 @@ def rec_subs(value, dicts):
if is_dict(value): if is_dict(value):
value = dict([(k, rec_subs(v, dicts)) for k, v in value.items()]) value = dict([(k, rec_subs(v, dicts)) for k, v in value.items()])
elif is_str(value): elif is_str(value):
value = var_re.sub(lambda m: dicts_get(dicts, m.group(1).strip('{}')), value) def convert(m):
sub_def = lambda m: dicts_get(dicts, m.group(1), m.group(3), m.group(2) == ':') if m.group("escaped") is not None:
value = var_def_re.sub(sub_def, value) return "$"
sub_err = lambda m: dicts_get(dicts, m.group(1), RuntimeError(m.group(3)), name = m.group("named") or m.group("braced")
m.group(2) == ':') for d in dicts:
value = var_err_re.sub(sub_err, value) value = d.get(name)
value = value.replace('$$', '$') if value is not None:
return "%s" % value
if m.group("err") is not None:
raise RuntimeError(m.group("err"))
return m.group("default") or ""
value = var_re.sub(convert, value)
elif hasattr(value, "__iter__"): elif hasattr(value, "__iter__"):
value = [rec_subs(i, dicts) for i in value] value = [rec_subs(i, dicts) for i in value]
return value return value

View File

@ -0,0 +1,8 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_COLON_QUESTION_ERROR: ${NOT_A_VARIABLE:?Missing variable}

View File

@ -0,0 +1,8 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_QUESTION_ERROR: ${NOT_A_VARIABLE?Missing variable}

View File

@ -0,0 +1,14 @@
version: "3.7"
services:
variables:
image: busybox
command: ["/bin/busybox", "sh", "-c", "export | grep EXAMPLE"]
environment:
EXAMPLE_VARIABLE: "Host user: $USER"
EXAMPLE_BRACES: "Host user: ${USER}"
EXAMPLE_COLON_DASH_DEFAULT: ${NOT_A_VARIABLE:-My default}
EXAMPLE_DASH_DEFAULT: ${NOT_A_VARIABLE-My other default}
EXAMPLE_DOT_ENV: $DOT_ENV_VARIABLE
EXAMPLE_LITERAL: This is a $$literal
EXAMPLE_EMPTY: $NOT_A_VARIABLE