1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/kiwi_scp/commands/cli.py

52 lines
1.5 KiB
Python
Raw Normal View History

import os
2021-11-02 16:21:01 +00:00
from enum import Enum, auto
from typing import List
import click
2021-10-29 11:37:59 +00:00
from ..instance import Instance
class KiwiCLI(click.MultiCommand):
"""Command Line Interface spread over multiple files in this directory"""
def list_commands(self, ctx):
"""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")
)
def get_command(self, ctx, name):
"""import and return a specific command"""
try:
2021-11-03 00:12:32 +00:00
mod = __import__(f"kiwi_scp.commands.cmd_{name}", None, None, ["CMD"])
except ImportError:
return
2021-11-02 16:21:01 +00:00
return mod.CMD
class KiwiCommand:
@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-02 16:21:01 +00:00
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
2021-11-03 00:12:32 +00:00
def run_for_services(cls, instance: Instance, project_name: str, services: List[str], **kwargs) -> None:
2021-11-02 16:21:01 +00:00
pass
class KiwiCommandType(Enum):
INSTANCE = auto()
PROJECT = auto()
SERVICE = auto()