2021-11-17 15:23:55 +00:00
|
|
|
import importlib
|
2021-10-20 06:31:36 +00:00
|
|
|
import os
|
2021-12-02 16:19:14 +00:00
|
|
|
from typing import List, Optional
|
2021-10-20 06:31:36 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
|
|
|
|
class KiwiCLI(click.MultiCommand):
|
2021-10-20 08:54:41 +00:00
|
|
|
"""Command Line Interface spread over multiple files in this directory"""
|
|
|
|
|
2021-11-17 15:23:55 +00:00
|
|
|
def list_commands(self, ctx: click.Context) -> List[str]:
|
2021-10-20 08:54:41 +00:00
|
|
|
"""list all the commands defined by cmd_*.py files in this directory"""
|
|
|
|
|
2021-11-17 15:23:55 +00:00
|
|
|
return [
|
2021-10-20 08:54:41 +00:00
|
|
|
filename[4:-3]
|
|
|
|
for filename in os.listdir(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
if filename.startswith("cmd_") and filename.endswith(".py")
|
2021-11-17 15:23:55 +00:00
|
|
|
]
|
2021-10-20 06:31:36 +00:00
|
|
|
|
2021-11-17 15:23:55 +00:00
|
|
|
def get_command(self, ctx: click.Context, cmd_name: str) -> Optional[click.Command]:
|
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-17 15:23:55 +00:00
|
|
|
cmd_module = importlib.import_module(f"kiwi_scp.commands.cmd_{cmd_name}")
|
|
|
|
|
2021-10-20 06:31:36 +00:00
|
|
|
except ImportError:
|
|
|
|
return
|
2021-11-17 15:23:55 +00:00
|
|
|
|
|
|
|
for cmd_name in dir(cmd_module):
|
|
|
|
member = getattr(cmd_module, cmd_name)
|
|
|
|
if isinstance(member, click.Command):
|
|
|
|
return member
|
|
|
|
|
|
|
|
|