1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 21:52:59 +00:00
kiwi-scp/kiwi_scp/commands/decorators.py

72 lines
1.9 KiB
Python
Raw Normal View History

2021-11-03 00:12:32 +00:00
import logging
2021-11-02 16:21:01 +00:00
from typing import Callable, Type, Optional, Tuple
import click
from .cli import KiwiCommandType, KiwiCommand
from ..instance import Instance
_pass_instance = click.make_pass_decorator(
Instance,
ensure=True,
)
2021-11-03 00:12:32 +00:00
2021-11-02 16:21:01 +00:00
_project_arg = click.argument(
"project",
required=False,
type=str,
)
2021-11-03 00:12:32 +00:00
2021-11-02 16:21:01 +00:00
_services_arg = click.argument(
"services",
metavar="[SERVICE]...",
nargs=-1,
type=str,
)
2021-11-03 00:12:32 +00:00
_logger = logging.getLogger(__name__)
2021-11-02 16:21:01 +00:00
def kiwi_command(
name: str,
command_type: KiwiCommandType,
2021-11-03 00:12:32 +00:00
**decorator_kwargs,
2021-11-02 16:21:01 +00:00
) -> Callable:
def decorator(command_cls: Type[KiwiCommand]) -> Callable:
2021-11-03 00:12:32 +00:00
@click.command(
name,
help=command_cls.__doc__,
**decorator_kwargs,
)
2021-11-02 16:21:01 +00:00
@_pass_instance
def cmd(ctx: Instance, project: Optional[str] = None, services: Optional[Tuple[str]] = None,
2021-11-03 00:12:32 +00:00
**kwargs) -> None:
_logger.debug(f"{ctx.directory!r}: {project!r}, {services!r}")
2021-11-02 16:21:01 +00:00
if project is None:
# run for whole instance
2021-11-03 00:12:32 +00:00
_logger.debug(f"running for instance, kwargs={kwargs}")
command_cls.run_for_instance(ctx, **kwargs)
2021-11-02 16:21:01 +00:00
elif not services:
# run for one entire project
2021-11-03 00:12:32 +00:00
_logger.debug(f"running for project {project}, kwargs={kwargs}")
command_cls.run_for_project(ctx, project, **kwargs)
2021-11-02 16:21:01 +00:00
else:
# run for some services
2021-11-03 00:12:32 +00:00
_logger.debug(f"running for services {services} in project {project}, kwargs={kwargs}")
command_cls.run_for_services(ctx, project, list(services), **kwargs)
2021-11-02 16:21:01 +00:00
if command_type is KiwiCommandType.PROJECT:
cmd = _project_arg(cmd)
elif command_type is KiwiCommandType.SERVICE:
cmd = _project_arg(cmd)
cmd = _services_arg(cmd)
return cmd
2021-11-02 16:21:31 +00:00
return decorator