kiwi-scp/src/kiwi/runner.py

70 lines
1.6 KiB
Python
Raw Normal View History

2020-08-12 15:46:50 +00:00
# system
2020-08-11 12:03:00 +00:00
import logging
2020-08-12 15:46:50 +00:00
# local
2020-08-11 12:03:00 +00:00
from .config import LoadedConfig
from .parser import Parser
from .subcommands import *
###########
# CONSTANTS
2020-08-13 08:48:01 +00:00
# all available subcommands
SUBCOMMANDS = [
InitCommand,
ShowCommand,
2020-08-13 11:00:32 +00:00
LogsCommand,
CmdCommand,
ShellCommand
]
class Runner:
2020-08-13 08:48:01 +00:00
"""Singleton: Subcommands setup and run"""
class __Runner:
2020-08-13 08:48:01 +00:00
"""Singleton type"""
__parser = None
__commands = []
def __init__(self):
2020-08-13 08:48:01 +00:00
# setup all subcommands
for cmd in SUBCOMMANDS:
self.__commands.append(cmd())
2020-08-11 12:03:00 +00:00
def run(self):
2020-08-13 08:48:01 +00:00
"""run the desired subcommand"""
2020-08-11 12:03:00 +00:00
args = Parser().get_args()
for cmd in self.__commands:
2020-08-11 12:03:00 +00:00
if str(cmd) == args.command:
2020-08-13 08:48:01 +00:00
# command found
2020-08-11 12:03:00 +00:00
logging.debug(f"Running '{cmd}' with args: {args}")
2020-08-13 12:26:49 +00:00
try:
cmd.run(LoadedConfig.get(), args)
except KeyboardInterrupt:
print()
logging.warning(f"'{cmd}' aborted, inputs may have been discarded.")
return True
2020-08-13 08:48:01 +00:00
# command not found
logging.error(f"kiwi command '{args.command}' unknown")
return False
__instance = None
def __init__(self):
if Runner.__instance is None:
2020-08-13 08:48:01 +00:00
# create singleton
Runner.__instance = Runner.__Runner()
def __getattr__(self, item):
2020-08-13 08:48:01 +00:00
"""Inner singleton direct access"""
return getattr(self.__instance, item)