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

71 lines
2.6 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-03 00:12:32 +00:00
from ..instance import Instance
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
def run_for_project(cls, instance: Instance, project_name: str, show: bool = None, **kwargs) -> None:
project = instance.get_project(project_name)
if project is None:
KiwiCommand.print_error(f"No project '{project_name}' in kiwi-scp instance at '{instance.directory}'.")
return
services = project.get_services()
if show:
KiwiCommand.print_header(f"Showing config for all services in project '{project_name}'.")
click.echo_via_pager(str(services))
else:
KiwiCommand.print_header(f"Services in project '{project_name}':")
KiwiCommand.print_list(service.name for service in services.content)
2021-10-29 11:38:21 +00:00
@classmethod
2021-11-03 15:32:01 +00:00
def run_for_services(cls, instance: Instance, project_name: str, service_names: List[str], show: bool = None,
**kwargs) -> None:
project = instance.get_project(project_name)
if project is None:
KiwiCommand.print_error(f"No project '{project_name}' in kiwi-scp instance at '{instance.directory}'.")
return
services = project.get_services(service_names)
if show:
KiwiCommand.print_header(
f"Showing config for services '{', '.join(service_names)}' in project '{project_name}'.")
click.echo_via_pager(str(services))
else:
KiwiCommand.print_header(f"Matching services in project '{project_name}':")
KiwiCommand.print_list(service.name for service in services.content)