diff --git a/kiwi_scp/commands/cli.py b/kiwi_scp/commands/cli.py index cf30a1e..c06f682 100644 --- a/kiwi_scp/commands/cli.py +++ b/kiwi_scp/commands/cli.py @@ -6,7 +6,7 @@ from typing import List, Tuple, Iterable, Type, Optional, TypeVar import click -from ..instance import Instance, Project +from ..instance import Instance, Project, Services from ..wstring import WParagraph, WAlignment @@ -135,6 +135,20 @@ class KiwiCommand: @classmethod def run_for_services(cls, instance: Instance, project: Project, service_names: List[str], **kwargs) -> None: + services = project.services.filter_existing(service_names) + + new_service_names = [ + service_name + for service_name + in service_names + if service_name not in list(services.names) + ] + + cls.run_for_filtered_services(instance, project, services, new_service_names, **kwargs) + + @classmethod + def run_for_filtered_services(cls, instance: Instance, project: Project, services: Services, + new_service_names: List[str], **kwargs) -> None: raise Exception diff --git a/kiwi_scp/commands/cmd_down.py b/kiwi_scp/commands/cmd_down.py index 28ab477..80e4bec 100644 --- a/kiwi_scp/commands/cmd_down.py +++ b/kiwi_scp/commands/cmd_down.py @@ -5,7 +5,7 @@ import click from .cli import KiwiCommand, KiwiCommandType from .decorators import kiwi_command from ..executable import COMPOSE_EXE -from ..instance import Instance, Project +from ..instance import Instance, Project, Services @click.option( @@ -40,15 +40,9 @@ class DownCommand(KiwiCommand): COMPOSE_EXE.run(["down"], **project.process_kwargs) @classmethod - def run_for_services(cls, instance: Instance, project: Project, service_names: List[str], **kwargs) -> None: - services = project.services.filter_existing(service_names) - existing_service_names = [ - service.name - for service in services.content - ] - all_service_names_exist = len(existing_service_names) == len(service_names) - - if not existing_service_names and not all_service_names_exist: + def run_for_filtered_services(cls, instance: Instance, project: Project, services: Services, + new_service_names: List[str], **kwargs) -> None: + if not services: if not click.confirm( "Did not find any of those services. \n" f"Bring down the entire project {project.name} instead?", @@ -56,5 +50,5 @@ class DownCommand(KiwiCommand): ): return - COMPOSE_EXE.run(["stop", *existing_service_names], **project.process_kwargs) - COMPOSE_EXE.run(["rm", "-f", *existing_service_names], **project.process_kwargs) + COMPOSE_EXE.run(["stop", *services.names], **project.process_kwargs) + COMPOSE_EXE.run(["rm", "-f", *services.names], **project.process_kwargs) diff --git a/kiwi_scp/commands/cmd_up.py b/kiwi_scp/commands/cmd_up.py index a548543..193cc77 100644 --- a/kiwi_scp/commands/cmd_up.py +++ b/kiwi_scp/commands/cmd_up.py @@ -5,7 +5,7 @@ import click from .cli import KiwiCommand, KiwiCommandType from .decorators import kiwi_command from ..executable import COMPOSE_EXE -from ..instance import Instance, Project +from ..instance import Instance, Project, Services @kiwi_command( @@ -16,18 +16,12 @@ class UpCommand(KiwiCommand): """Bring up the whole instance, a project or service(s) inside a project""" @classmethod - def run_for_services(cls, instance: Instance, project: Project, service_names: List[str], **kwargs) -> None: + def run_for_filtered_services(cls, instance: Instance, project: Project, services: Services, + new_service_names: List[str], **kwargs) -> None: # TODO conf-copy # TODO net-up - services = project.services.filter_existing(service_names) - existing_service_names = [ - service.name - for service in services.content - ] - all_service_names_exist = len(existing_service_names) == len(service_names) - - if not existing_service_names and not all_service_names_exist: + if not services: if not click.confirm( "Did not find any of those services. \n" f"Bring up the entire project {project.name} instead?", @@ -35,4 +29,4 @@ class UpCommand(KiwiCommand): ): return - COMPOSE_EXE.run(["up", "-d", *existing_service_names], **project.process_kwargs) + COMPOSE_EXE.run(["up", "-d", *services.names], **project.process_kwargs) diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index 7b358b0..47453c9 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -46,6 +46,13 @@ class Services: def __bool__(self) -> bool: return bool(self.content) + @property + def names(self) -> Generator[str, None, None]: + return ( + service.name + for service in self.content + ) + def filter_existing(self, service_names: List[str]) -> "Services": return Services([ service