Commit 539ccf8d authored by Adrian DC's avatar Adrian DC
Browse files

feat(entrypoint): implement prompt confirmation upon deletions

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent 50073206
Loading
Loading
Loading
Loading
+42 −2
Changes for src/cli/entrypoint.py: 42 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -12,10 +12,12 @@ from gitlab.v4.objects import (
    Project as GitLabProject,
    User as GitLabUser,
)
import questionary

# Components
from ..features.gitlab import GitLabFeature
from ..prints.colors import Colors
from ..prints.themes import Themes
from ..system.platform import Platform
from ..types.namespaces import Namespaces

@@ -173,6 +175,30 @@ class Entrypoint:
        # Result
        return Entrypoint.Result.SUCCESS

    # Confirm
    @staticmethod
    def confirm(description: str, text: str = '') -> bool:

        # Header
        print(
            f'{Colors.BOLD}   - Delete {description}: Confirm "'
            f'{Colors.RED}{text}'
            f'{Colors.BOLD}" deletion:'
            f'{Colors.RESET}', end='')
        Platform.flush()

        # Get user configuration
        answer: bool = questionary.confirm(
            message='',
            default=False,
            qmark='',
            style=Themes.confirmation_style(),
            auto_enter=True,
        ).ask()

        # Result
        return answer

    # Group
    @staticmethod
    def group(
@@ -205,8 +231,15 @@ class Entrypoint:
              f'{Colors.RESET}')
        Platform.flush()

        # Delete group
        # Delete group after validation
        if options.delete_group:
            if not Entrypoint.confirm(
                    group_type,
                    group.full_path,
            ):
                return Entrypoint.Result.SUCCESS

            # Delete group
            gitlab.group_delete(criteria)
            print(f'{Colors.BOLD}   - Delete {group_type}: '
                  f'{Colors.GREEN}Success'
@@ -289,8 +322,15 @@ class Entrypoint:
              f'{Colors.RESET}')
        Platform.flush()

        # Delete project
        # Delete project after validation
        if options.delete_project:
            if not Entrypoint.confirm(
                    'project',
                    project.path_with_namespace,
            ):
                return Entrypoint.Result.SUCCESS

            # Delete project
            gitlab.project_delete(criteria)
            print(f'{Colors.BOLD}   - Delete project: '
                  f'{Colors.GREEN}Success'

src/prints/themes.py

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

# Modules libraries
try:
    from prompt_toolkit.styles.style import Style as prompt_toolkit_styles_Style
except ModuleNotFoundError: # pragma: no cover
    from typing import Any as prompt_toolkit_styles_Style # type: ignore[assignment]

# Components
from .colors import Colors

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

    # Flags
    __prepared = False

    # Members
    __bold: str = ''
    __red: str = ''
    __selected: str = ''

    # Prepare
    @staticmethod
    def __prepare() -> None:

        # Colors enabled
        if Colors.enabled():
            Themes.__bold = 'bold'
            Themes.__red = '#FF0000 bold'
            Themes.__selected = 'bold noreverse'

        # Colors disabled
        else:
            Themes.__bold = 'noinherit bold'
            Themes.__red = 'noinherit bold'
            Themes.__selected = 'noinherit bold noreverse'

        # Raise flag
        Themes.__prepared = True

    # Confirmation
    @staticmethod
    def confirmation_style() -> prompt_toolkit_styles_Style:

        # Prepare
        if not Themes.__prepared:
            Themes.__prepare()

        # Result
        return prompt_toolkit_styles_Style([
            ('answer', Themes.__red),
            ('instruction', Themes.__red),
            ('highlighted', Themes.__bold),
            ('pointer', Themes.__bold),
            ('qmark', Themes.__bold),
            ('question', Themes.__bold),
            ('selected', Themes.__selected),
            ('separator', Themes.__bold),
        ])