2021-10-20 06:31:36 +00:00
|
|
|
import os
|
2021-11-03 15:32:01 +00:00
|
|
|
import sys
|
2021-11-02 16:21:01 +00:00
|
|
|
from enum import Enum, auto
|
2021-11-03 16:58:18 +00:00
|
|
|
from typing import List, Tuple, Iterable, Any, Type
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
|
2021-10-29 11:37:59 +00:00
|
|
|
from ..instance import Instance
|
|
|
|
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
class KiwiCLI(click.MultiCommand):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""Command Line Interface spread over multiple files in this directory"""
|
|
|
|
|
2021-10-20 06:31:36 +00:00
|
|
|
def list_commands(self, ctx):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""list all the commands defined by cmd_*.py files in this directory"""
|
|
|
|
|
|
|
|
return (
|
|
|
|
filename[4:-3]
|
|
|
|
for filename in os.listdir(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
if filename.startswith("cmd_") and filename.endswith(".py")
|
|
|
|
)
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
def get_command(self, ctx, name):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""import and return a specific command"""
|
|
|
|
|
2021-10-20 06:31:36 +00:00
|
|
|
try:
|
2021-11-03 00:12:32 +00:00
|
|
|
mod = __import__(f"kiwi_scp.commands.cmd_{name}", None, None, ["CMD"])
|
2021-10-20 06:31:36 +00:00
|
|
|
except ImportError:
|
|
|
|
return
|
2021-11-02 16:21:01 +00:00
|
|
|
return mod.CMD
|
|
|
|
|
|
|
|
|
|
|
|
class KiwiCommand:
|
2021-11-03 15:32:01 +00:00
|
|
|
@staticmethod
|
|
|
|
def print_multi_color(*content: Tuple[str, str]):
|
|
|
|
for message, color in content:
|
|
|
|
click.secho(message, fg=color, nl=False)
|
|
|
|
click.echo()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_header(header: str):
|
|
|
|
click.secho(header, fg="green", bold=True)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_error(header: str):
|
|
|
|
click.secho(header, file=sys.stderr, fg="red", bold=True)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def print_list(content: Iterable[str]):
|
|
|
|
for item in content:
|
|
|
|
KiwiCommand.print_multi_color(
|
|
|
|
(" - ", "green"),
|
|
|
|
(item, "blue"),
|
|
|
|
)
|
|
|
|
|
2021-11-03 16:58:18 +00:00
|
|
|
@staticmethod
|
|
|
|
def user_query(description: str, default: Any, cast_to: Type[Any] = str):
|
|
|
|
# prompt user as per argument
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
prompt = \
|
|
|
|
click.style(f"Enter {description} [", fg="green") + \
|
|
|
|
click.style(default, fg="blue") + \
|
|
|
|
click.style("] ", fg="green")
|
|
|
|
str_value = input(prompt).strip()
|
|
|
|
if str_value:
|
|
|
|
return cast_to(str_value)
|
|
|
|
else:
|
|
|
|
return default
|
|
|
|
|
|
|
|
except EOFError:
|
|
|
|
click.echo("Input aborted.")
|
|
|
|
return default
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
click.echo(f"Invalid input: {e}")
|
|
|
|
|
2021-11-02 16:21:01 +00:00
|
|
|
@classmethod
|
2021-11-03 00:12:32 +00:00
|
|
|
def run_for_instance(cls, instance: Instance, **kwargs) -> None:
|
2021-11-02 16:21:01 +00:00
|
|
|
for project in instance.config.projects:
|
|
|
|
cls.run_for_project(instance, project.name, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
2021-11-03 00:12:32 +00:00
|
|
|
def run_for_project(cls, instance: Instance, project_name: str, **kwargs) -> None:
|
2021-11-03 15:32:01 +00:00
|
|
|
project = instance.get_project(project_name)
|
|
|
|
|
|
|
|
if project is None:
|
|
|
|
click.secho(f"No project '{project_name}' in kiwi-scp instance at '{instance.directory}'.", fg="red", bold=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
service_names = [service.name for service in project.get_services().content]
|
|
|
|
|
2021-11-02 16:21:01 +00:00
|
|
|
cls.run_for_services(instance, project_name, service_names, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
2021-11-03 15:32:01 +00:00
|
|
|
def run_for_services(cls, instance: Instance, project_name: str, service_names: List[str], **kwargs) -> None:
|
2021-11-02 16:21:01 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class KiwiCommandType(Enum):
|
|
|
|
INSTANCE = auto()
|
|
|
|
PROJECT = auto()
|
|
|
|
SERVICE = auto()
|