From 2ad7daa81f4b39402fa454dbb671eb8034ad9803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Galv=C3=A3o?= Date: Sat, 21 Dec 2019 23:29:39 -0300 Subject: [PATCH 1/2] Test variable interpolation in the YAML --- .../docker-compose-colon-question-error.yml | 8 ++++++++ .../docker-compose-question-error.yml | 8 ++++++++ tests/interpolation/docker-compose.yml | 14 ++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 tests/interpolation/docker-compose-colon-question-error.yml create mode 100644 tests/interpolation/docker-compose-question-error.yml create mode 100644 tests/interpolation/docker-compose.yml diff --git a/tests/interpolation/docker-compose-colon-question-error.yml b/tests/interpolation/docker-compose-colon-question-error.yml new file mode 100644 index 0000000..7761f45 --- /dev/null +++ b/tests/interpolation/docker-compose-colon-question-error.yml @@ -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} + diff --git a/tests/interpolation/docker-compose-question-error.yml b/tests/interpolation/docker-compose-question-error.yml new file mode 100644 index 0000000..ea07072 --- /dev/null +++ b/tests/interpolation/docker-compose-question-error.yml @@ -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} + diff --git a/tests/interpolation/docker-compose.yml b/tests/interpolation/docker-compose.yml new file mode 100644 index 0000000..c5358e0 --- /dev/null +++ b/tests/interpolation/docker-compose.yml @@ -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 + From 00840d0613f4eb689975bc214f9b1883fc15dfe4 Mon Sep 17 00:00:00 2001 From: Muayyad alsadi Date: Thu, 6 May 2021 00:49:42 +0300 Subject: [PATCH 2/2] resolve conflict --- podman_compose.py | 51 +++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 0233d00..8116863 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -146,25 +146,19 @@ def fix_mount_dict(mount_dict, proj_name, srv_name): # ${VARIABLE?err} raise error if not set # $$ means $ -var_re = re.compile(r'\$(\{(?:[^\s\$:\-\}]+)\}|(?:[^\s\$\{\}]+))') -var_def_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?-([^\}]*)\}') -var_err_re = re.compile(r'\$\{([^\s\$:\-\}]+)(:)?\?([^\}]*)\}') - -def dicts_get(dicts, key, fallback='', fallback_empty=False): - """ - 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 - """ - value = None - for d in dicts: - value = d.get(key, None) - if value is not None: break - if not value: - if fallback_empty or value is None: - value = fallback - if isinstance(value, Exception): - raise value - return value +var_re = re.compile(r""" + \$(?: + (?P\$) | + (?P[_a-zA-Z][_a-zA-Z0-9]*) | + (?:{ + (?P[_a-zA-Z][_a-zA-Z0-9]*) + (?: + (?::?-(?P[^}]+)) | + (?::?\?(?P[^}]+)) + )? + }) + ) +""", re.VERBOSE) def rec_subs(value, dicts): """ @@ -173,13 +167,18 @@ def rec_subs(value, dicts): if is_dict(value): value = dict([(k, rec_subs(v, dicts)) for k, v in value.items()]) elif is_str(value): - value = var_re.sub(lambda m: dicts_get(dicts, m.group(1).strip('{}')), value) - sub_def = lambda m: dicts_get(dicts, m.group(1), m.group(3), m.group(2) == ':') - value = var_def_re.sub(sub_def, value) - sub_err = lambda m: dicts_get(dicts, m.group(1), RuntimeError(m.group(3)), - m.group(2) == ':') - value = var_err_re.sub(sub_err, value) - value = value.replace('$$', '$') + def convert(m): + if m.group("escaped") is not None: + return "$" + name = m.group("named") or m.group("braced") + for d in dicts: + value = d.get(name) + 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__"): value = [rec_subs(i, dicts) for i in value] return value