mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-24 13:43:01 +00:00
CLI and list command for instance
This commit is contained in:
parent
b16a423a9d
commit
1f588d5364
4 changed files with 48 additions and 27 deletions
|
@ -23,7 +23,7 @@ class KiwiCLI(click.MultiCommand):
|
||||||
"""import and return a specific command"""
|
"""import and return a specific command"""
|
||||||
|
|
||||||
try:
|
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:
|
except ImportError:
|
||||||
return
|
return
|
||||||
return mod.CMD
|
return mod.CMD
|
||||||
|
@ -31,17 +31,17 @@ class KiwiCLI(click.MultiCommand):
|
||||||
|
|
||||||
class KiwiCommand:
|
class KiwiCommand:
|
||||||
@classmethod
|
@classmethod
|
||||||
def run_for_instance(cls, instance: Instance, **kwargs):
|
def run_for_instance(cls, instance: Instance, **kwargs) -> None:
|
||||||
for project in instance.config.projects:
|
for project in instance.config.projects:
|
||||||
cls.run_for_project(instance, project.name, **kwargs)
|
cls.run_for_project(instance, project.name, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@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]
|
service_names = [service.name for service in instance.get_services(project_name, None).content]
|
||||||
cls.run_for_services(instance, project_name, service_names, **kwargs)
|
cls.run_for_services(instance, project_name, service_names, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@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
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,43 @@
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
from .cli import KiwiCommandType, KiwiCommand
|
from .cli import KiwiCommandType, KiwiCommand
|
||||||
from .decorators import kiwi_command
|
from .decorators import kiwi_command
|
||||||
from ..config import ProjectConfig
|
from ..instance import Instance
|
||||||
from ..instance import Instance, Services
|
|
||||||
|
|
||||||
|
|
||||||
@click.option(
|
@click.option(
|
||||||
"-s/-S",
|
"-s/-S",
|
||||||
"--show/--no-show",
|
"--show/--no-show",
|
||||||
help=f"EXAMPLE",
|
help=f"show actual config contents instead",
|
||||||
)
|
)
|
||||||
@kiwi_command(
|
@kiwi_command(
|
||||||
"list",
|
"list",
|
||||||
KiwiCommandType.PROJECT,
|
KiwiCommandType.SERVICE,
|
||||||
short_help="Inspect a kiwi-scp instance",
|
short_help="Inspect a kiwi-scp instance",
|
||||||
)
|
)
|
||||||
class CMD(KiwiCommand):
|
class CMD(KiwiCommand):
|
||||||
@classmethod
|
"""List projects in this instance, services inside a project or service(s) inside a project"""
|
||||||
def run_for_instance(cls, instance: Instance, show: bool = None, **kwargs):
|
|
||||||
print(show)
|
|
||||||
print(instance.config.projects)
|
|
||||||
|
|
||||||
@classmethod
|
@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):
|
**kwargs):
|
||||||
print(show)
|
print(show)
|
||||||
print(services)
|
print(services)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from typing import Callable, Type, Optional, Tuple
|
from typing import Callable, Type, Optional, Tuple
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
@ -9,11 +10,13 @@ _pass_instance = click.make_pass_decorator(
|
||||||
Instance,
|
Instance,
|
||||||
ensure=True,
|
ensure=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
_project_arg = click.argument(
|
_project_arg = click.argument(
|
||||||
"project",
|
"project",
|
||||||
required=False,
|
required=False,
|
||||||
type=str,
|
type=str,
|
||||||
)
|
)
|
||||||
|
|
||||||
_services_arg = click.argument(
|
_services_arg = click.argument(
|
||||||
"services",
|
"services",
|
||||||
metavar="[SERVICE]...",
|
metavar="[SERVICE]...",
|
||||||
|
@ -21,33 +24,40 @@ _services_arg = click.argument(
|
||||||
type=str,
|
type=str,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def kiwi_command(
|
def kiwi_command(
|
||||||
name: str,
|
name: str,
|
||||||
command_type: KiwiCommandType,
|
command_type: KiwiCommandType,
|
||||||
**kwargs,
|
**decorator_kwargs,
|
||||||
) -> Callable:
|
) -> Callable:
|
||||||
def decorator(command_cls: Type[KiwiCommand]) -> Callable:
|
def decorator(command_cls: Type[KiwiCommand]) -> Callable:
|
||||||
|
|
||||||
@click.command(name, **kwargs)
|
@click.command(
|
||||||
|
name,
|
||||||
|
help=command_cls.__doc__,
|
||||||
|
**decorator_kwargs,
|
||||||
|
)
|
||||||
@_pass_instance
|
@_pass_instance
|
||||||
def cmd(ctx: Instance, project: Optional[str] = None, services: Optional[Tuple[str]] = None,
|
def cmd(ctx: Instance, project: Optional[str] = None, services: Optional[Tuple[str]] = None,
|
||||||
**cmd_kwargs) -> None:
|
**kwargs) -> None:
|
||||||
print(f"{ctx.directory!r}: {project!r}, {services!r}")
|
|
||||||
|
_logger.debug(f"{ctx.directory!r}: {project!r}, {services!r}")
|
||||||
if project is None:
|
if project is None:
|
||||||
# run for whole instance
|
# run for whole instance
|
||||||
print(f"for instance: {cmd_kwargs}")
|
_logger.debug(f"running for instance, kwargs={kwargs}")
|
||||||
command_cls.run_for_instance(ctx, **cmd_kwargs)
|
command_cls.run_for_instance(ctx, **kwargs)
|
||||||
|
|
||||||
elif not services:
|
elif not services:
|
||||||
# run for one entire project
|
# run for one entire project
|
||||||
print(f"for project {project}: {cmd_kwargs}")
|
_logger.debug(f"running for project {project}, kwargs={kwargs}")
|
||||||
command_cls.run_for_project(ctx, project, **cmd_kwargs)
|
command_cls.run_for_project(ctx, project, **kwargs)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# run for some services
|
# run for some services
|
||||||
print(f"for services {project}.{services}: {cmd_kwargs}")
|
_logger.debug(f"running for services {services} in project {project}, kwargs={kwargs}")
|
||||||
command_cls.run_for_services(ctx, project, list(services), **cmd_kwargs)
|
command_cls.run_for_services(ctx, project, list(services), **kwargs)
|
||||||
|
|
||||||
if command_type is KiwiCommandType.PROJECT:
|
if command_type is KiwiCommandType.PROJECT:
|
||||||
cmd = _project_arg(cmd)
|
cmd = _project_arg(cmd)
|
||||||
|
|
|
@ -5,10 +5,7 @@ from kiwi_scp.commands.cli import KiwiCLI
|
||||||
|
|
||||||
@click.command(cls=KiwiCLI)
|
@click.command(cls=KiwiCLI)
|
||||||
def main():
|
def main():
|
||||||
"""main entry point for command line interface"""
|
"""kiwi is the simple tool for managing container servers."""
|
||||||
|
|
||||||
click.echo("Hello main")
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue