1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-12-25 10:33:01 +00:00

CLI and list command for instance

This commit is contained in:
Jörn-Michael Miehe 2021-11-03 01:12:32 +01:00
parent b16a423a9d
commit 1f588d5364
4 changed files with 48 additions and 27 deletions

View file

@ -23,7 +23,7 @@ class KiwiCLI(click.MultiCommand):
"""import and return a specific command"""
try:
mod = __import__(f"kiwi_scp.commands.cmd_{name}", None, None, ["cmd"])
mod = __import__(f"kiwi_scp.commands.cmd_{name}", None, None, ["CMD"])
except ImportError:
return
return mod.CMD
@ -31,17 +31,17 @@ class KiwiCLI(click.MultiCommand):
class KiwiCommand:
@classmethod
def run_for_instance(cls, instance: Instance, **kwargs):
def run_for_instance(cls, instance: Instance, **kwargs) -> None:
for project in instance.config.projects:
cls.run_for_project(instance, project.name, **kwargs)
@classmethod
def run_for_project(cls, instance: Instance, project_name: str, **kwargs):
def run_for_project(cls, instance: Instance, project_name: str, **kwargs) -> None:
service_names = [service.name for service in instance.get_services(project_name, None).content]
cls.run_for_services(instance, project_name, service_names, **kwargs)
@classmethod
def run_for_services(cls, instance: Instance, project_name: str, services: List[str], **kwargs):
def run_for_services(cls, instance: Instance, project_name: str, services: List[str], **kwargs) -> None:
pass

View file

@ -1,29 +1,43 @@
from typing import List
import click
from .cli import KiwiCommandType, KiwiCommand
from .decorators import kiwi_command
from ..config import ProjectConfig
from ..instance import Instance, Services
from ..instance import Instance
@click.option(
"-s/-S",
"--show/--no-show",
help=f"EXAMPLE",
help=f"show actual config contents instead",
)
@kiwi_command(
"list",
KiwiCommandType.PROJECT,
KiwiCommandType.SERVICE,
short_help="Inspect a kiwi-scp instance",
)
class CMD(KiwiCommand):
@classmethod
def run_for_instance(cls, instance: Instance, show: bool = None, **kwargs):
print(show)
print(instance.config.projects)
"""List projects in this instance, services inside a project or service(s) inside a project"""
@classmethod
def run_for_services(cls, instance: Instance, project: ProjectConfig, services: Services, show: bool = None,
def run_for_instance(cls, instance: Instance, show: bool = None, **kwargs):
if show:
click.secho(f"Showing config for kiwi-scp instance at '{instance.directory}'.", fg="green", bold=True)
click.echo_via_pager(instance.config.kiwi_yml)
else:
click.secho(f"Projects in kiwi-scp instance at '{instance.directory}':", fg="green", bold=True)
for project in instance.config.projects:
click.echo(
click.style(" - ", fg="green") +
click.style(project.name, fg="blue") +
click.style(' (disabled)' if not project.enabled else '', fg="red")
)
@classmethod
def run_for_services(cls, instance: Instance, project_name: str, services: List[str], show: bool = None,
**kwargs):
print(show)
print(services)

View file

@ -1,3 +1,4 @@
import logging
from typing import Callable, Type, Optional, Tuple
import click
@ -9,11 +10,13 @@ _pass_instance = click.make_pass_decorator(
Instance,
ensure=True,
)
_project_arg = click.argument(
"project",
required=False,
type=str,
)
_services_arg = click.argument(
"services",
metavar="[SERVICE]...",
@ -21,33 +24,40 @@ _services_arg = click.argument(
type=str,
)
_logger = logging.getLogger(__name__)
def kiwi_command(
name: str,
command_type: KiwiCommandType,
**kwargs,
**decorator_kwargs,
) -> Callable:
def decorator(command_cls: Type[KiwiCommand]) -> Callable:
@click.command(name, **kwargs)
@click.command(
name,
help=command_cls.__doc__,
**decorator_kwargs,
)
@_pass_instance
def cmd(ctx: Instance, project: Optional[str] = None, services: Optional[Tuple[str]] = None,
**cmd_kwargs) -> None:
print(f"{ctx.directory!r}: {project!r}, {services!r}")
**kwargs) -> None:
_logger.debug(f"{ctx.directory!r}: {project!r}, {services!r}")
if project is None:
# run for whole instance
print(f"for instance: {cmd_kwargs}")
command_cls.run_for_instance(ctx, **cmd_kwargs)
_logger.debug(f"running for instance, kwargs={kwargs}")
command_cls.run_for_instance(ctx, **kwargs)
elif not services:
# run for one entire project
print(f"for project {project}: {cmd_kwargs}")
command_cls.run_for_project(ctx, project, **cmd_kwargs)
_logger.debug(f"running for project {project}, kwargs={kwargs}")
command_cls.run_for_project(ctx, project, **kwargs)
else:
# run for some services
print(f"for services {project}.{services}: {cmd_kwargs}")
command_cls.run_for_services(ctx, project, list(services), **cmd_kwargs)
_logger.debug(f"running for services {services} in project {project}, kwargs={kwargs}")
command_cls.run_for_services(ctx, project, list(services), **kwargs)
if command_type is KiwiCommandType.PROJECT:
cmd = _project_arg(cmd)

View file

@ -5,10 +5,7 @@ from kiwi_scp.commands.cli import KiwiCLI
@click.command(cls=KiwiCLI)
def main():
"""main entry point for command line interface"""
click.echo("Hello main")
pass
"""kiwi is the simple tool for managing container servers."""
if __name__ == "__main__":