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

84 lines
2 KiB
Python
Raw Normal View History

2021-11-02 16:21:01 +00:00
from typing import Callable, Type, Optional, Tuple
import click
2021-12-02 16:19:14 +00:00
from .cmd import KiwiCommandType, KiwiCommand
2021-11-02 16:21:01 +00:00
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_name",
metavar="PROJECT",
type=str,
)
_projects_arg = click.argument(
"project_names",
metavar="[PROJECT]...",
nargs=-1,
type=str,
)
_services_arg_p = 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
_services_arg_s = 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(
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
def cmd(ctx: Instance, project_name: Optional[str] = None, project_names: Optional[Tuple[str]] = None,
service_names: Optional[Tuple[str]] = None, **kwargs) -> None:
if command_cls.type is KiwiCommandType.INSTANCE:
project_names = []
2021-11-03 00:12:32 +00:00
elif command_cls.type is KiwiCommandType.PROJECTS:
project_names = list(project_names)
2021-11-02 16:21:01 +00:00
else:
if project_name is None:
project_names = []
else:
project_names = [project_name]
if command_cls.type is KiwiCommandType.SERVICES:
service_names = list(service_names)
command_cls.run(ctx, project_names, service_names, **kwargs)
if command_cls.type is KiwiCommandType.PROJECT:
2021-11-02 16:21:01 +00:00
cmd = _project_arg(cmd)
elif command_cls.type is KiwiCommandType.PROJECTS:
cmd = _projects_arg(cmd)
elif command_cls.type is KiwiCommandType.SERVICES:
cmd = _services_arg_p(cmd)
cmd = _services_arg_s(cmd)
2021-11-02 16:21:01 +00:00
return cmd
2021-11-02 16:21:31 +00:00
return decorator