1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 04:43:00 +00:00

Runner logic back into package, as singleton

This commit is contained in:
Jörn-Michael Miehe 2020-08-10 15:48:15 +02:00
parent 65fcb91bd0
commit 3cb195fe1a
7 changed files with 71 additions and 50 deletions

View file

@ -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__":

View file

@ -1,5 +1,7 @@
from .core import Parser
from .runner import Runner
__all__ = [
'Parser'
'Parser',
'Runner'
]

40
src/kiwi/runner.py Normal file
View file

@ -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()

View file

@ -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

View file

@ -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):

View file

@ -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'}))

View file

@ -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)