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

63 lines
2.5 KiB
Python
Raw Normal View History

2021-11-03 00:12:32 +00:00
from typing import List
2021-10-21 02:02:38 +00:00
import click
2021-11-02 16:21:01 +00:00
from .cli import KiwiCommandType, KiwiCommand
from .decorators import kiwi_command
2021-11-06 02:45:27 +00:00
from ..instance import Instance, Project
2021-10-29 11:38:21 +00:00
2021-10-29 11:59:57 +00:00
@click.option(
"-s/-S",
"--show/--no-show",
2021-11-03 00:12:32 +00:00
help=f"show actual config contents instead",
2021-10-29 11:59:57 +00:00
)
2021-10-29 11:38:21 +00:00
@kiwi_command(
2021-10-22 15:52:46 +00:00
"list",
2021-11-03 00:12:32 +00:00
KiwiCommandType.SERVICE,
2021-10-22 15:52:46 +00:00
short_help="Inspect a kiwi-scp instance",
)
2021-11-02 16:21:01 +00:00
class CMD(KiwiCommand):
2021-11-03 00:12:32 +00:00
"""List projects in this instance, services inside a project or service(s) inside a project"""
2021-10-29 11:38:21 +00:00
@classmethod
2021-11-03 15:32:01 +00:00
def run_for_instance(cls, instance: Instance, show: bool = None, **kwargs) -> None:
2021-11-03 00:12:32 +00:00
if show:
2021-11-03 15:32:01 +00:00
KiwiCommand.print_header(f"Showing config for kiwi-scp instance at '{instance.directory}'.")
2021-11-03 00:12:32 +00:00
click.echo_via_pager(instance.config.kiwi_yml)
else:
2021-11-03 15:32:01 +00:00
KiwiCommand.print_header(f"Projects in kiwi-scp instance at '{instance.directory}':")
KiwiCommand.print_list(
project.name + click.style(" (disabled)" if not project.enabled else "", fg="red")
for project in instance.config.projects
)
2021-11-03 00:12:32 +00:00
2021-11-03 15:32:01 +00:00
@classmethod
2021-11-06 02:45:27 +00:00
def run_for_existing_project(cls, instance: Instance, project: Project, show: bool = None, **kwargs) -> None:
2021-11-03 15:32:01 +00:00
if show:
2021-11-06 02:45:27 +00:00
KiwiCommand.print_header(f"Showing config for all services in project '{project.name}'.")
click.echo_via_pager(str(project.services))
2021-11-03 15:32:01 +00:00
else:
2021-11-06 02:45:27 +00:00
KiwiCommand.print_header(f"Services in project '{project.name}':")
KiwiCommand.print_list(service.name for service in project.services.content)
2021-10-29 11:38:21 +00:00
@classmethod
2021-11-06 02:45:27 +00:00
def run_for_new_project(cls, instance: Instance, project_name: str, **kwargs) -> None:
KiwiCommand.print_error(f"Project '{project_name}' not in kiwi-scp instance at '{instance.directory}'!")
2021-11-03 15:32:01 +00:00
2021-11-06 02:45:27 +00:00
@classmethod
def run_for_services(cls, instance: Instance, project: Project, service_names: List[str], show: bool = None,
**kwargs) -> None:
services = project.services.filter_existing(service_names)
2021-11-03 15:32:01 +00:00
if show:
2021-11-06 02:45:27 +00:00
service_names = [service.name for service in services.content]
2021-11-03 15:32:01 +00:00
KiwiCommand.print_header(
2021-11-06 02:45:27 +00:00
f"Showing config for matching services '{', '.join(service_names)}' in project '{project.name}'.")
2021-11-03 15:32:01 +00:00
click.echo_via_pager(str(services))
else:
2021-11-06 02:45:27 +00:00
KiwiCommand.print_header(f"Matching services in project '{project.name}':")
2021-11-03 15:32:01 +00:00
KiwiCommand.print_list(service.name for service in services.content)