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

60 lines
2.2 KiB
Python
Raw Permalink 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-12-02 16:19:14 +00:00
from .cmd import KiwiCommandType, KiwiCommand
2021-11-02 16:21:01 +00:00
from .decorators import kiwi_command
2021-12-02 16:08:14 +00:00
from ..instance import Instance
from ..project import 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
short_help="Inspect a kiwi-scp instance",
)
class ListCommand(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-12-01 16:49:19 +00:00
type = KiwiCommandType.SERVICES
2021-10-29 11:38:21 +00:00
@classmethod
2021-11-13 00:12:27 +00:00
def run_for_instance(cls, instance: Instance, show: bool = None) -> 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(
2021-12-01 12:02:49 +00:00
project.name + (click.style(" (disabled)", fg="red") if not project.enabled else "")
2021-11-03 15:32:01 +00:00
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: Project, show: bool = None) -> 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-12-01 16:49:19 +00:00
def run_for_services(cls, instance: Instance, project: Project, service_names: List[str],
show: bool = None) -> None:
2021-11-06 02:45:27 +00:00
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)