1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-23 05:13:01 +00:00
kiwi-scp/kiwi_scp/commands/cmd_list.py
2021-12-02 17:08:14 +01:00

59 lines
2.2 KiB
Python

from typing import List
import click
from .cli import KiwiCommandType, KiwiCommand
from .decorators import kiwi_command
from ..instance import Instance
from ..project import Project
@click.option(
"-s/-S",
"--show/--no-show",
help=f"show actual config contents instead",
)
@kiwi_command(
short_help="Inspect a kiwi-scp instance",
)
class ListCommand(KiwiCommand):
"""List projects in this instance, services inside a project or service(s) inside a project"""
type = KiwiCommandType.SERVICES
@classmethod
def run_for_instance(cls, instance: Instance, show: bool = None) -> None:
if show:
KiwiCommand.print_header(f"Showing config for kiwi-scp instance at '{instance.directory}'.")
click.echo_via_pager(instance.config.kiwi_yml)
else:
KiwiCommand.print_header(f"Projects in kiwi-scp instance at '{instance.directory}':")
KiwiCommand.print_list(
project.name + (click.style(" (disabled)", fg="red") if not project.enabled else "")
for project in instance.config.projects
)
@classmethod
def run_for_project(cls, instance: Instance, project: Project, show: bool = None) -> None:
if show:
KiwiCommand.print_header(f"Showing config for all services in project '{project.name}'.")
click.echo_via_pager(str(project.services))
else:
KiwiCommand.print_header(f"Services in project '{project.name}':")
KiwiCommand.print_list(service.name for service in project.services.content)
@classmethod
def run_for_services(cls, instance: Instance, project: Project, service_names: List[str],
show: bool = None) -> None:
services = project.services.filter_existing(service_names)
if show:
service_names = [service.name for service in services.content]
KiwiCommand.print_header(
f"Showing config for matching 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)