2021-11-17 15:23:55 +00:00
|
|
|
import importlib
|
2021-10-20 06:31:36 +00:00
|
|
|
import os
|
2022-01-20 09:55:47 +00:00
|
|
|
from gettext import gettext as _
|
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
|
|
|
|
2022-01-19 14:35:33 +00:00
|
|
|
member_name = f"{cmd_name.capitalize()}Command"
|
|
|
|
|
|
|
|
if member_name in dir(cmd_module):
|
|
|
|
member = getattr(cmd_module, member_name)
|
|
|
|
|
2021-11-17 15:23:55 +00:00
|
|
|
if isinstance(member, click.Command):
|
|
|
|
return member
|
|
|
|
|
2022-01-19 14:35:33 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Fail class")
|
2021-11-17 15:23:55 +00:00
|
|
|
|
2022-01-19 14:35:33 +00:00
|
|
|
else:
|
|
|
|
raise Exception("Fail member name")
|
2022-01-20 09:55:47 +00:00
|
|
|
|
|
|
|
def format_commands(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
|
|
|
|
commands = {
|
|
|
|
"Operation": [
|
|
|
|
"up", "down", "restart", "update",
|
|
|
|
],
|
|
|
|
"Instance Management": [
|
|
|
|
"init", "list",
|
|
|
|
],
|
|
|
|
"Project and Service Management": [
|
|
|
|
"new", "enable", "disable", "logs", "shell", "cmd",
|
|
|
|
],
|
|
|
|
"Image Handling": [
|
|
|
|
"build", "pull", "push",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
# allow for 3 times the default spacing
|
|
|
|
cmd_names = set(self.list_commands(ctx))
|
|
|
|
limit = formatter.width - 6 - max(len(cmd_name) for cmd_name in cmd_names)
|
|
|
|
|
|
|
|
for purpose, cmd_list in commands.items():
|
|
|
|
with formatter.section(_(f"Commands for {purpose}")):
|
|
|
|
formatter.write_dl([
|
|
|
|
(cmd_name, self.get_command(ctx, cmd_name).get_short_help_str(limit))
|
|
|
|
for cmd_name in cmd_list
|
|
|
|
])
|
|
|
|
|
|
|
|
cmd_names -= set(cmd_list)
|
|
|
|
|
|
|
|
if len(cmd_names) > 0:
|
|
|
|
raise Exception(f"Some commands were not registered in a group above: {cmd_names}")
|