Runner logic back into package, as singleton
This commit is contained in:
parent
65fcb91bd0
commit
3cb195fe1a
7 changed files with 71 additions and 50 deletions
|
@ -21,26 +21,13 @@ def set_verbosity(logger, handler, verbosity):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
commands = [
|
|
||||||
InitCommand,
|
|
||||||
ShowCommand,
|
|
||||||
LogsCommand
|
|
||||||
]
|
|
||||||
|
|
||||||
for cmd in commands:
|
|
||||||
cmd.setup()
|
|
||||||
|
|
||||||
args = kiwi.Parser().get_args()
|
args = kiwi.Parser().get_args()
|
||||||
|
|
||||||
log_handler = logging.StreamHandler()
|
log_handler = logging.StreamHandler()
|
||||||
logging.getLogger().addHandler(log_handler)
|
logging.getLogger().addHandler(log_handler)
|
||||||
|
|
||||||
set_verbosity(logging.getLogger(), log_handler, args.verbosity)
|
set_verbosity(logging.getLogger(), log_handler, args.verbosity)
|
||||||
|
|
||||||
for cmd in commands:
|
kiwi.Runner().run(args.command)
|
||||||
if cmd.command == args.command:
|
|
||||||
cmd.run()
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from .core import Parser
|
from .core import Parser
|
||||||
|
from .runner import Runner
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'Parser'
|
'Parser',
|
||||||
|
'Runner'
|
||||||
]
|
]
|
||||||
|
|
40
src/kiwi/runner.py
Normal file
40
src/kiwi/runner.py
Normal 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()
|
|
@ -1,11 +1,9 @@
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
from ..core import Parser
|
||||||
from ..config import LoadedConfig
|
from ..config import LoadedConfig
|
||||||
|
|
||||||
###########
|
|
||||||
# CONSTANTS
|
|
||||||
|
|
||||||
|
|
||||||
def is_executable(filename):
|
def is_executable(filename):
|
||||||
if filename is None:
|
if filename is None:
|
||||||
|
@ -28,14 +26,20 @@ def get_exe_key(exe_name):
|
||||||
|
|
||||||
|
|
||||||
class SubCommand:
|
class SubCommand:
|
||||||
command = None
|
__name = None
|
||||||
|
__parser = None
|
||||||
|
|
||||||
@classmethod
|
def __init__(self, name, **kwargs):
|
||||||
def setup(cls):
|
self.__name = name
|
||||||
pass
|
self.__parser = Parser().get_subparsers().add_parser(name, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
def __str__(self):
|
||||||
def run(cls):
|
return self.__name
|
||||||
|
|
||||||
|
def get_parser(self):
|
||||||
|
return self.__parser
|
||||||
|
|
||||||
|
def run(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,23 +32,19 @@ def user_input_exe(config, exe_name):
|
||||||
|
|
||||||
|
|
||||||
class InitCommand(SubCommand):
|
class InitCommand(SubCommand):
|
||||||
command = 'init'
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
@classmethod
|
'init',
|
||||||
def setup(cls):
|
|
||||||
parser = Parser().get_subparsers().add_parser(
|
|
||||||
cls.command,
|
|
||||||
description="Create a new kiwi-config instance"
|
description="Create a new kiwi-config instance"
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
self.get_parser().add_argument(
|
||||||
'-f', '--force',
|
'-f', '--force',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help=f"Use default values even if {KIWI_CONF_NAME} is present"
|
help=f"Use default values even if {KIWI_CONF_NAME} is present"
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
def run(self):
|
||||||
def run(cls):
|
|
||||||
logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'")
|
logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'")
|
||||||
|
|
||||||
if Parser().get_args().force and os.path.isfile(KIWI_CONF_NAME):
|
if Parser().get_args().force and os.path.isfile(KIWI_CONF_NAME):
|
||||||
|
|
|
@ -4,15 +4,11 @@ from ._utils import SubCommand, Docker
|
||||||
|
|
||||||
|
|
||||||
class LogsCommand(SubCommand):
|
class LogsCommand(SubCommand):
|
||||||
command = 'logs'
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
@classmethod
|
'logs',
|
||||||
def setup(cls):
|
|
||||||
_ = Parser().get_subparsers().add_parser(
|
|
||||||
cls.command,
|
|
||||||
description="Show logs of a project"
|
description="Show logs of a project"
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
def run(self):
|
||||||
def run(cls):
|
|
||||||
print(Docker.run_command('docker-compose', ['logs', '-tf', '--tail=10'], cwd='hello-world.project', env={'COMPOSE_PROJECT_NAME': 'hello-world'}))
|
print(Docker.run_command('docker-compose', ['logs', '-tf', '--tail=10'], cwd='hello-world.project', env={'COMPOSE_PROJECT_NAME': 'hello-world'}))
|
||||||
|
|
|
@ -5,16 +5,12 @@ from ._utils import SubCommand
|
||||||
|
|
||||||
|
|
||||||
class ShowCommand(SubCommand):
|
class ShowCommand(SubCommand):
|
||||||
command = 'show'
|
def __init__(self):
|
||||||
|
super().__init__(
|
||||||
@classmethod
|
'logs',
|
||||||
def setup(cls):
|
description="Show logs of a project"
|
||||||
_ = Parser().get_subparsers().add_parser(
|
|
||||||
cls.command,
|
|
||||||
description="Show effective kiwi.yml"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
def run(self):
|
||||||
def run(cls):
|
|
||||||
config = LoadedConfig.get()
|
config = LoadedConfig.get()
|
||||||
print(config)
|
print(config)
|
||||||
|
|
Loading…
Reference in a new issue