kiwi-scp/kiwi_scp/commands/decorators.py

56 lines
1.3 KiB
Python
Raw Normal View History

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(
2021-11-06 02:45:27 +00:00
"project_name",
metavar="[PROJECT]",
2021-11-02 16:21:01 +00:00
required=False,
type=str,
)
2021-11-03 00:12:32 +00:00
2021-11-02 16:21:01 +00:00
_services_arg = click.argument(
2021-11-06 02:45:27 +00:00
"service_names",
2021-11-02 16:21:01 +00:00
metavar="[SERVICE]...",
nargs=-1,
type=str,
)
def kiwi_command(
cmd_type: KiwiCommandType = KiwiCommandType.SERVICE,
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(
help=command_cls.__doc__,
**decorator_kwargs,
)
2021-11-02 16:21:01 +00:00
@_pass_instance
2021-11-06 02:45:27 +00:00
def cmd(ctx: Instance, project_name: Optional[str] = None, service_names: Optional[Tuple[str]] = None,
2021-11-03 00:12:32 +00:00
**kwargs) -> None:
2021-11-30 18:25:53 +00:00
if service_names is not None:
service_names = list(service_names)
2021-11-03 00:12:32 +00:00
2021-11-30 18:25:53 +00:00
command_cls.run(ctx, project_name, service_names, **kwargs)
2021-11-02 16:21:01 +00:00
if cmd_type is KiwiCommandType.PROJECT:
2021-11-02 16:21:01 +00:00
cmd = _project_arg(cmd)
elif cmd_type is KiwiCommandType.SERVICE:
2021-11-02 16:21:01 +00:00
cmd = _project_arg(cmd)
cmd = _services_arg(cmd)
return cmd
2021-11-02 16:21:31 +00:00
return decorator