From 99a50f14600747506f9f84d644f41ec56a95637a Mon Sep 17 00:00:00 2001 From: ldericher <40151420+ldericher@users.noreply.github.com> Date: Tue, 30 Nov 2021 19:25:53 +0100 Subject: [PATCH] remove run logic from kiwi_command --- kiwi_scp/commands/cli.py | 34 +++++++++++++++++++++++++++++++++ kiwi_scp/commands/decorators.py | 32 +++---------------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/kiwi_scp/commands/cli.py b/kiwi_scp/commands/cli.py index c06f682..8616068 100644 --- a/kiwi_scp/commands/cli.py +++ b/kiwi_scp/commands/cli.py @@ -1,4 +1,5 @@ import importlib +import logging import os import sys from enum import Enum, auto @@ -9,6 +10,8 @@ import click from ..instance import Instance, Project, Services from ..wstring import WParagraph, WAlignment +_logger = logging.getLogger(__name__) + class KiwiCLI(click.MultiCommand): """Command Line Interface spread over multiple files in this directory""" @@ -118,6 +121,37 @@ class KiwiCommand: return answer == 'yes' + @classmethod + def run(cls, instance: Instance, project_name: Optional[str] = None, service_names: Optional[List[str]] = None, + **kwargs): + + _logger.debug(f"{instance.directory!r}: {project_name!r}, {service_names!r}") + if project_name is None: + # run for whole instance + _logger.debug(f"running for instance, kwargs={kwargs}") + cls.run_for_instance(instance, **kwargs) + + elif not service_names: + # run for one entire project + project = instance.get_project(project_name) + if project is None: + _logger.debug(f"running for new project {project_name}, kwargs={kwargs}") + cls.run_for_new_project(instance, project_name, **kwargs) + + else: + _logger.debug(f"running for project {project.name}, kwargs={kwargs}") + cls.run_for_project(instance, project, **kwargs) + + else: + # run for some services + project = instance.get_project(project_name) + if project is not None: + _logger.debug(f"running for services {service_names} in project {project}, kwargs={kwargs}") + cls.run_for_services(instance, project, service_names, **kwargs) + + else: + cls.print_error(f"Project '{project_name}' not in kiwi-scp instance at '{instance.directory}'!") + @classmethod def run_for_instance(cls, instance: Instance, **kwargs) -> None: for project_config in instance.config.projects: diff --git a/kiwi_scp/commands/decorators.py b/kiwi_scp/commands/decorators.py index 22e5aec..04e470f 100644 --- a/kiwi_scp/commands/decorators.py +++ b/kiwi_scp/commands/decorators.py @@ -1,4 +1,3 @@ -import logging from typing import Callable, Type, Optional, Tuple import click @@ -25,8 +24,6 @@ _services_arg = click.argument( type=str, ) -_logger = logging.getLogger(__name__) - def kiwi_command( cmd_type: KiwiCommandType = KiwiCommandType.SERVICE, @@ -41,33 +38,10 @@ def kiwi_command( @_pass_instance def cmd(ctx: Instance, project_name: Optional[str] = None, service_names: Optional[Tuple[str]] = None, **kwargs) -> None: + if service_names is not None: + service_names = list(service_names) - _logger.debug(f"{ctx.directory!r}: {project_name!r}, {service_names!r}") - if project_name is None: - # run for whole instance - _logger.debug(f"running for instance, kwargs={kwargs}") - command_cls.run_for_instance(ctx, **kwargs) - - elif not service_names: - # run for one entire project - project = ctx.get_project(project_name) - if project is not None: - _logger.debug(f"running for existing project {project}, kwargs={kwargs}") - command_cls.run_for_project(ctx, project, **kwargs) - - else: - _logger.debug(f"running for new project {project_name}, kwargs={kwargs}") - command_cls.run_for_new_project(ctx, project_name, **kwargs) - - else: - # run for some services - project = ctx.get_project(project_name) - if project is not None: - _logger.debug(f"running for services {service_names} in project {project}, kwargs={kwargs}") - command_cls.run_for_services(ctx, project, list(service_names), **kwargs) - - else: - KiwiCommand.print_error(f"Project '{project_name}' not in kiwi-scp instance at '{ctx.directory}'!") + command_cls.run(ctx, project_name, service_names, **kwargs) if cmd_type is KiwiCommandType.PROJECT: cmd = _project_arg(cmd)