From 0f6f348daf7a66b9b74b7c2e0f99efeebe4a1458 Mon Sep 17 00:00:00 2001 From: funkecoder23 Date: Mon, 27 May 2024 11:36:51 -0400 Subject: [PATCH] add _process_image for image:name --- docs/configuration.rst | 13 +++++++++++++ scuba/config.py | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 4569a86..41e9a6f 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -90,6 +90,19 @@ The ``image`` node is usually necessary but, as of scuba 2.5, can be omitted for ``.scuba.yml`` files in which only the ``aliases`` are intended to be used. +Example: + +.. code-block:: yaml + + # .gitlab-ci.yml + image: + name: debian:8.2 + + # .scuba.yml + image: !from_yaml \\.gitlab-ci.yml image + +The ``image`` node can also + .. _conf_environment: ``environment`` diff --git a/scuba/config.py b/scuba/config.py index ba4a507..775ff2e 100644 --- a/scuba/config.py +++ b/scuba/config.py @@ -274,6 +274,34 @@ def _process_environment(node: CfgNode, name: str) -> Environment: return result +def _process_image(node: CfgNode) -> Optional[str]: + """Process an image-type node + + Args: + node: A node of image data retrieved from a YAML document. + Should be an "image schema" node as described in docs/configuration.rst. + + Returns: + An image name + """ + if isinstance(node, str): + # The image is just the text itself + return node + + if isinstance(node, dict): + # There must be a "name" key, which must be a string + image = node.get("name") + if not image: + raise ConfigError(f"image: must have a 'name' subkey") + + if isinstance(image, str): + return image + + raise ConfigError(f"image.name: must be a string") + + return None + + def _get_nullable_str(data: Dict[str, Any], key: str) -> Optional[str]: # N.B. We can't use data.get() here, because that might return # None, leading to ambiguity between the key being absent or set @@ -329,13 +357,11 @@ def _get_typed_val( @overload # When default is None, can return None (Optional). -def _get_str(data: CfgData, key: str, default: None = None) -> Optional[str]: - ... +def _get_str(data: CfgData, key: str, default: None = None) -> Optional[str]: ... @overload # When default is non-None, cannot return None. -def _get_str(data: CfgData, key: str, default: str) -> str: - ... +def _get_str(data: CfgData, key: str, default: str) -> str: ... def _get_str(data: CfgData, key: str, default: Optional[str] = None) -> Optional[str]: @@ -567,7 +593,7 @@ def __init__( + ", ".join(extra) ) - self._image = _get_str(data, "image") + self._image = _process_image(data.get("image")) self.shell = _get_str(data, "shell", DEFAULT_SHELL) self.entrypoint = _get_entrypoint(data) self.docker_args = _get_docker_args(data)