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

96 lines
2.9 KiB
Python
Raw Normal View History

import importlib
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
import click
2022-02-22 12:18:35 +00:00
class MissingCMDObjectError(ValueError):
"""raised if command object can't be found in its module"""
pass
class CMDObjectSubclassError(TypeError):
"""raised if a command object is not inheriting click.Command"""
pass
class CMDUnregisteredError(ValueError):
"""raised if commands have not been assigned to a command group"""
unregistered: List[str]
def __init__(self, unregistered):
self.unregistered = unregistered
super().__init__(f"Some commands were not registered in a group above: {unregistered!r}")
class KiwiCLI(click.MultiCommand):
"""Command Line Interface spread over multiple files in this directory"""
def list_commands(self, ctx: click.Context) -> List[str]:
"""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: click.Context, cmd_name: str) -> Optional[click.Command]:
"""import and return a specific command"""
try:
cmd_module = importlib.import_module(f"kiwi_scp.commands.cmd_{cmd_name}")
except ImportError:
return
2022-02-22 12:18:35 +00:00
cmd_object_name = f"{cmd_name.capitalize()}Command"
2022-01-19 14:35:33 +00:00
2022-02-22 12:18:35 +00:00
if cmd_object_name in dir(cmd_module):
cmd_object = getattr(cmd_module, cmd_object_name)
2022-01-19 14:35:33 +00:00
2022-02-22 12:18:35 +00:00
if isinstance(cmd_object, click.Command):
return cmd_object
2022-01-19 14:35:33 +00:00
else:
2022-02-22 12:18:35 +00:00
raise CMDObjectSubclassError()
2022-01-19 14:35:33 +00:00
else:
2022-02-22 12:18:35 +00:00
raise MissingCMDObjectError()
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:
2022-02-22 12:18:35 +00:00
raise CMDUnregisteredError(cmd_names)