diff --git a/src/kiwi-config.py b/src/kiwi-config.py index 61fa52a..55effc2 100755 --- a/src/kiwi-config.py +++ b/src/kiwi-config.py @@ -21,26 +21,13 @@ def set_verbosity(logger, handler, verbosity): def main(): - commands = [ - InitCommand, - ShowCommand, - LogsCommand - ] - - for cmd in commands: - cmd.setup() - args = kiwi.Parser().get_args() log_handler = logging.StreamHandler() logging.getLogger().addHandler(log_handler) - set_verbosity(logging.getLogger(), log_handler, args.verbosity) - for cmd in commands: - if cmd.command == args.command: - cmd.run() - return + kiwi.Runner().run(args.command) if __name__ == "__main__": diff --git a/src/kiwi/__init__.py b/src/kiwi/__init__.py index ded6eea..f5decab 100644 --- a/src/kiwi/__init__.py +++ b/src/kiwi/__init__.py @@ -1,5 +1,7 @@ from .core import Parser +from .runner import Runner __all__ = [ - 'Parser' + 'Parser', + 'Runner' ] diff --git a/src/kiwi/runner.py b/src/kiwi/runner.py new file mode 100644 index 0000000..981529a --- /dev/null +++ b/src/kiwi/runner.py @@ -0,0 +1,40 @@ +from .subcommands import * + +########### +# CONSTANTS + +SUBCOMMANDS = [ + InitCommand, + ShowCommand, + LogsCommand +] + + +class Runner: + class __Runner: + __commands = [] + + def __init__(self): + for cmd in SUBCOMMANDS: + self.__commands.append(cmd()) + + def run(self, command_name): + for cmd in self.__commands: + if str(cmd) == command_name: + cmd.run() + return True + + return False + + __instance = None + + def __init__(self): + if Runner.__instance is None: + Runner.__instance = Runner.__Runner() + + def __getattr__(self, item): + return getattr(self.__instance, item) + + +if __name__ == 'kiwi.runner': + Runner().setup_all() diff --git a/src/kiwi/subcommands/_utils.py b/src/kiwi/subcommands/_utils.py index adf5b45..6123945 100644 --- a/src/kiwi/subcommands/_utils.py +++ b/src/kiwi/subcommands/_utils.py @@ -1,11 +1,9 @@ import os import subprocess +from ..core import Parser from ..config import LoadedConfig -########### -# CONSTANTS - def is_executable(filename): if filename is None: @@ -28,14 +26,20 @@ def get_exe_key(exe_name): class SubCommand: - command = None + __name = None + __parser = None - @classmethod - def setup(cls): - pass + def __init__(self, name, **kwargs): + self.__name = name + self.__parser = Parser().get_subparsers().add_parser(name, **kwargs) - @classmethod - def run(cls): + def __str__(self): + return self.__name + + def get_parser(self): + return self.__parser + + def run(self): pass diff --git a/src/kiwi/subcommands/init.py b/src/kiwi/subcommands/init.py index 00c6f9a..f7924d8 100644 --- a/src/kiwi/subcommands/init.py +++ b/src/kiwi/subcommands/init.py @@ -32,23 +32,19 @@ def user_input_exe(config, exe_name): class InitCommand(SubCommand): - command = 'init' - - @classmethod - def setup(cls): - parser = Parser().get_subparsers().add_parser( - cls.command, + def __init__(self): + super().__init__( + 'init', description="Create a new kiwi-config instance" ) - parser.add_argument( + self.get_parser().add_argument( '-f', '--force', action='store_true', help=f"Use default values even if {KIWI_CONF_NAME} is present" ) - @classmethod - def run(cls): + def run(self): logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'") if Parser().get_args().force and os.path.isfile(KIWI_CONF_NAME): diff --git a/src/kiwi/subcommands/logs.py b/src/kiwi/subcommands/logs.py index 824d735..6f2fb2e 100644 --- a/src/kiwi/subcommands/logs.py +++ b/src/kiwi/subcommands/logs.py @@ -4,15 +4,11 @@ from ._utils import SubCommand, Docker class LogsCommand(SubCommand): - command = 'logs' - - @classmethod - def setup(cls): - _ = Parser().get_subparsers().add_parser( - cls.command, + def __init__(self): + super().__init__( + 'logs', description="Show logs of a project" ) - @classmethod - def run(cls): + def run(self): print(Docker.run_command('docker-compose', ['logs', '-tf', '--tail=10'], cwd='hello-world.project', env={'COMPOSE_PROJECT_NAME': 'hello-world'})) diff --git a/src/kiwi/subcommands/show.py b/src/kiwi/subcommands/show.py index 30d582e..1819d0d 100644 --- a/src/kiwi/subcommands/show.py +++ b/src/kiwi/subcommands/show.py @@ -5,16 +5,12 @@ from ._utils import SubCommand class ShowCommand(SubCommand): - command = 'show' - - @classmethod - def setup(cls): - _ = Parser().get_subparsers().add_parser( - cls.command, - description="Show effective kiwi.yml" + def __init__(self): + super().__init__( + 'logs', + description="Show logs of a project" ) - @classmethod - def run(cls): + def run(self): config = LoadedConfig.get() print(config)