1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/kiwi_scp/commands/cmd_list.py

97 lines
3.1 KiB
Python
Raw Normal View History

2021-10-29 11:38:21 +00:00
import typing as t
from typing import Tuple
2021-10-21 02:02:38 +00:00
import click
2021-10-29 11:38:21 +00:00
from .cli import pass_instance
from ..config import ProjectConfig
from ..instance import Instance, Services
2021-10-28 13:53:32 +00:00
from ..misc import service_command
2021-10-21 02:02:38 +00:00
2021-10-29 11:38:21 +00:00
class KiwiCommand:
@classmethod
def run_for_instance(cls, instance: Instance, **kwargs):
for project in instance.config.projects:
cls.run_for_project(instance, project, **kwargs)
@classmethod
def run_for_project(cls, instance: Instance, project: ProjectConfig, **kwargs):
cls.run_for_services(instance, project, instance.get_services(project.name, None), **kwargs)
@classmethod
def run_for_services(cls, instance: Instance, project: ProjectConfig, services: Services, **kwargs):
pass
def kiwi_command(
name: str,
**kwargs,
) -> t.Callable:
def decorator(command_cls: t.Type[KiwiCommand]) -> t.Callable:
@click.command(name, **kwargs)
@pass_instance
@service_command
def cmd(ctx: Instance, project: t.Optional[str], services: Tuple[str], **cmd_kwargs) -> None:
print(f"{ctx.directory!r}: {project!r}, {services!r}")
if project is None:
# run for whole instance
2021-10-29 11:59:57 +00:00
print(f"for instance: {cmd_kwargs}")
2021-10-29 11:38:21 +00:00
command_cls.run_for_instance(ctx, **cmd_kwargs)
elif not services:
# run for one entire project
2021-10-29 11:59:57 +00:00
print(f"for project {project}: {cmd_kwargs}")
2021-10-29 11:38:21 +00:00
for project_cfg in ctx.config.projects:
if project_cfg.name == project:
2021-10-29 11:59:57 +00:00
command_cls.run_for_project(ctx, project_cfg, **cmd_kwargs)
2021-10-29 11:38:21 +00:00
else:
# run for some services
2021-10-29 11:59:57 +00:00
print(f"for services {project}.{services}: {cmd_kwargs}")
2021-10-29 11:38:21 +00:00
for project_cfg in ctx.config.projects:
if project_cfg.name == project:
services = ctx.get_services(project_cfg.name, services)
2021-10-29 11:59:57 +00:00
command_cls.run_for_services(ctx, project_cfg, services, **cmd_kwargs)
2021-10-29 11:38:21 +00:00
return cmd
return decorator
2021-10-29 11:59:57 +00:00
@click.option(
"-s/-S",
"--show/--no-show",
help=f"EXAMPLE",
)
2021-10-29 11:38:21 +00:00
@kiwi_command(
2021-10-22 15:52:46 +00:00
"list",
short_help="Inspect a kiwi-scp instance",
)
2021-10-29 11:38:21 +00:00
class cmd(KiwiCommand):
@classmethod
2021-10-29 11:59:57 +00:00
def run_for_instance(cls, instance: Instance, show: bool = None, **kwargs):
print(show)
2021-10-29 11:38:21 +00:00
print(instance.config.projects)
@classmethod
2021-10-29 11:59:57 +00:00
def run_for_services(cls, instance: Instance, project: ProjectConfig, services: Services, show: bool = None,
**kwargs):
print(show)
2021-10-29 11:38:21 +00:00
print(services)
# @click.command(
# "list",
# short_help="Inspect a kiwi-scp instance",
# )
# @pass_instance
# @service_command
# def cmd(ctx: Instance, project: str, services: Tuple[str]):
# """List projects in this instance, services inside a project or service(s) inside a project"""
# print(f"project: {project!r}, services: {services!r}")
# if project is not None:
# print(ctx.get_services(project, services))
# else:
# print(f"projects: {ctx.config.projects}")