Verified Commit a6182b4b authored by Adrian DC's avatar Adrian DC
Browse files

refactor(flags): implement 'Flags' class and refactor flags handlings

Issue: #295
---

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 628cd425
Loading
Loading
Loading
Loading
+8 −12
Changes for src/jobs/runner.py: 8 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ from typing import cast, Dict, List, Optional

# Components
from ..engines.engine import Engine, privileged_default
from ..models.flags import Flags
from ..models.images import Entrypoint
from ..models.jobs import Job
from ..package.bundle import Bundle
@@ -735,18 +736,13 @@ class Runner:
            variables[Bundle.FLAG_NETWORK] = network

        # Prepare privileged
        privileged = privileged_default()
        if self.__options.privileged:
            privileged = str(self.__options.privileged).lower() in [
                'true',
                '1',
            ]
        elif Bundle.FLAG_PRIVILEGED in variables:
            privileged = str(variables[Bundle.FLAG_PRIVILEGED]).lower() in [
                'true',
                '1',
            ]
        variables[Bundle.FLAG_PRIVILEGED] = 'true' if privileged else 'false'
        privileged = Flags.check_bool(
            option=self.__options.privileged,
            flag=Bundle.FLAG_PRIVILEGED,
            variables=variables,
            default=privileged_default(),
        )
        variables[Bundle.FLAG_PRIVILEGED] = Flags.string(privileged)

        # Prepare user variables
        environ[Bundle.ENV_USER_HOST_GID] = str(Platform.getgid())

src/models/flags.py

0 → 100644
+88 −0
Changes for src/models/flags.py: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/usr/bin/env python3

# Standard libraries
from typing import Optional, Union

# Components
from .variables import Variables

# Flags class, pylint: disable=too-few-public-methods
class Flags:

    # Enabled
    @staticmethod
    def isenabled(value: str) -> bool:
        return value.lower() in [
            'true',
            '1',
        ]

    # Exists
    @staticmethod
    def exists(flag: str, variables: Variables) -> bool:
        return flag in variables

    # Enabled
    @staticmethod
    def enabled(flag: str, variables: Variables) -> bool:
        return Flags.exists(flag, variables) and Flags.isenabled(str(variables[flag]))

    # String
    @staticmethod
    def string(value: Union[bool, str]) -> str:
        if isinstance(value, bool):
            return 'true' if value else 'false'
        return value

    # Has
    @staticmethod
    def has(
        option: Optional[str],
        flag: str,
        variables: Variables,
    ) -> bool:
        return option is not None or Flags.exists(flag, variables)

    # Check (bool)
    @staticmethod
    def check_bool(
        option: Optional[Union[bool, str]],
        flag: str,
        variables: Variables,
        default: bool,
    ) -> bool:

        # Check option (bool)
        if option is not None and isinstance(option, bool):
            return option

        # Check option (str)
        if option is not None and isinstance(option, str):
            return Flags.isenabled(option)

        # Check variables
        if Flags.exists(flag, variables):
            return Flags.isenabled(str(variables[flag]))

        # Default
        return default

    # Check (str)
    @staticmethod
    def check_str(
        option: Optional[str],
        flag: str,
        variables: Variables,
        default: str,
    ) -> str:

        # Check option
        if option is not None and option != '':
            return option

        # Check variables
        if Flags.exists(flag, variables):
            return str(variables[flag])

        # Default
        return default
+2 −5
Changes for src/parsers/gitlab.py: 2 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ from packaging.version import parse as parse_version

# Components
from ..features.menus import MenusFeature
from ..models.flags import Flags
from ..models.images import Image, Images
from ..models.jobs import Job, JobData
from ..models.pipelines import Pipeline
@@ -1054,11 +1055,7 @@ class GitLab:
        if 'extends' in job_data and job_data['extends']:

            # Allowed incomplete extended job
            if Bundle.FLAG_EXTENDS_INCOMPLETE in job.variables and str(
                    job.variables[Bundle.FLAG_EXTENDS_INCOMPLETE]).lower() in [
                        'true',
                        '1',
                    ]:
            if Flags.enabled(Bundle.FLAG_EXTENDS_INCOMPLETE, job.variables):
                pass

            # Detect incomplete extended job