mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-22 12:53:00 +00:00
"Runner" into main function, rework Parser and InitCommand
This commit is contained in:
parent
f188d05e56
commit
93643de8fb
6 changed files with 84 additions and 61 deletions
|
@ -1,10 +1,40 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import logging
|
||||||
|
|
||||||
import kiwi
|
import kiwi
|
||||||
|
from kiwi.subcommands import *
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
kiwi.Runner.setup_all()
|
logging.basicConfig(
|
||||||
kiwi.Runner.run()
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||||
|
level=logging.NOTSET
|
||||||
|
)
|
||||||
|
|
||||||
|
commands = [
|
||||||
|
InitCommand,
|
||||||
|
ShowCommand,
|
||||||
|
LogsCommand
|
||||||
|
]
|
||||||
|
|
||||||
|
for cmd in commands:
|
||||||
|
cmd.setup()
|
||||||
|
|
||||||
|
args = kiwi.Parser.get_args()
|
||||||
|
|
||||||
|
if args.verbose >= 2:
|
||||||
|
log_level = logging.DEBUG
|
||||||
|
elif args.verbose >= 1:
|
||||||
|
log_level = logging.INFO
|
||||||
|
else:
|
||||||
|
log_level = logging.WARNING
|
||||||
|
|
||||||
|
logging.getLogger().setLevel(log_level)
|
||||||
|
|
||||||
|
for cmd in commands:
|
||||||
|
if cmd.command == args.command:
|
||||||
|
cmd.run()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
from .runner import Runner
|
from .core import Parser
|
||||||
|
|
||||||
__all__ = ['Runner']
|
__all__ = [
|
||||||
|
'Parser'
|
||||||
|
]
|
||||||
|
|
|
@ -9,32 +9,35 @@ KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml")
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
__instance = None
|
__parser = None
|
||||||
__subparsers = None
|
__subparsers = None
|
||||||
__args = None
|
__args = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_instance(cls):
|
def get_parser(cls):
|
||||||
if not cls.__instance:
|
if cls.__parser is None:
|
||||||
cls.__instance = argparse.ArgumentParser(description='kiwi-config')
|
cls.__parser = argparse.ArgumentParser(description='kiwi-config')
|
||||||
|
|
||||||
cls.__subparsers = Parser.__instance.add_subparsers()
|
cls.__parser.add_argument(
|
||||||
cls.__subparsers.required = True
|
'-v', '--verbose',
|
||||||
cls.__subparsers.dest = 'command'
|
action='count', default=0
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
return cls.__parser
|
||||||
def get_instance(cls):
|
|
||||||
cls.__init_instance()
|
|
||||||
return cls.__instance
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_subparsers(cls):
|
def get_subparsers(cls):
|
||||||
cls.__init_instance()
|
if cls.__subparsers is None:
|
||||||
|
cls.__subparsers = cls.get_parser().add_subparsers()
|
||||||
|
cls.__subparsers.required = True
|
||||||
|
cls.__subparsers.dest = 'command'
|
||||||
|
|
||||||
return cls.__subparsers
|
return cls.__subparsers
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_args(cls):
|
def get_args(cls):
|
||||||
if not cls.__args:
|
if cls.__args is None:
|
||||||
cls.__args = cls.get_instance().parse_args()
|
cls.__args = cls.get_parser().parse_args()
|
||||||
|
|
||||||
return cls.__args
|
return cls.__args
|
||||||
|
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
from typing import List
|
|
||||||
|
|
||||||
from .core import Parser
|
|
||||||
from .subcommands import *
|
|
||||||
|
|
||||||
|
|
||||||
class Runner:
|
|
||||||
__commands: List[SubCommand] = [
|
|
||||||
InitCommand,
|
|
||||||
ShowCommand,
|
|
||||||
LogsCommand
|
|
||||||
]
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setup_all(cls):
|
|
||||||
for cmd in cls.__commands:
|
|
||||||
cmd.setup()
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def run(cls):
|
|
||||||
args = Parser.get_args()
|
|
||||||
|
|
||||||
for cmd in cls.__commands:
|
|
||||||
if cmd.command == args.command:
|
|
||||||
cmd.run()
|
|
||||||
return
|
|
|
@ -1,11 +1,8 @@
|
||||||
from ._utils import SubCommand
|
|
||||||
|
|
||||||
from .init import InitCommand
|
from .init import InitCommand
|
||||||
from .show import ShowCommand
|
from .show import ShowCommand
|
||||||
from .logs import LogsCommand
|
from .logs import LogsCommand
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'SubCommand',
|
|
||||||
'InitCommand',
|
'InitCommand',
|
||||||
'ShowCommand',
|
'ShowCommand',
|
||||||
'LogsCommand'
|
'LogsCommand'
|
||||||
|
|
|
@ -16,23 +16,35 @@ def user_input(config, key, prompt):
|
||||||
config[key] = result
|
config[key] = result
|
||||||
|
|
||||||
|
|
||||||
|
def is_executable(filename):
|
||||||
|
if filename is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return os.path.isfile(filename) and os.access(filename, os.X_OK)
|
||||||
|
|
||||||
|
|
||||||
def find_exe(program_name):
|
def find_exe(program_name):
|
||||||
for path in os.environ["PATH"].split(os.pathsep):
|
for path in os.environ["PATH"].split(os.pathsep):
|
||||||
exe_file = os.path.join(path, program_name)
|
exe_file = os.path.join(path, program_name)
|
||||||
if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
|
if is_executable(exe_file):
|
||||||
return exe_file
|
return exe_file
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def user_input_exe(config, program_name):
|
def user_input_exe(config, key):
|
||||||
exe_file = find_exe(program_name)
|
exe_file = config[key]
|
||||||
key = 'executables:' + program_name
|
program_name = key.split(':')[1]
|
||||||
|
|
||||||
if exe_file is not None:
|
if not is_executable(exe_file):
|
||||||
config[key] = exe_file
|
logging.warning("Reconfiguring '%s' executable path.", program_name)
|
||||||
else:
|
exe_file = find_exe(program_name)
|
||||||
user_input(config, key, "Enter path to '{}' executable".format(program_name))
|
|
||||||
|
if exe_file is not None:
|
||||||
|
logging.info("Found executable at '%s'.", exe_file)
|
||||||
|
config[key] = exe_file
|
||||||
|
else:
|
||||||
|
user_input(config, key, f"Enter path to '{program_name}' executable")
|
||||||
|
|
||||||
|
|
||||||
class InitCommand(SubCommand):
|
class InitCommand(SubCommand):
|
||||||
|
@ -48,12 +60,12 @@ class InitCommand(SubCommand):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-f', '--force',
|
'-f', '--force',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Use default values even if {} is present".format(KIWI_CONF_NAME)
|
help=f"Use default values even if {KIWI_CONF_NAME} is present"
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def run(cls):
|
def run(cls):
|
||||||
logging.info("Initializing kiwi-config instance in '%s'", 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):
|
||||||
logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME)
|
logging.warning("Overwriting existing '%s'!", KIWI_CONF_NAME)
|
||||||
|
@ -61,18 +73,23 @@ class InitCommand(SubCommand):
|
||||||
else:
|
else:
|
||||||
config = LoadedConfig.get()
|
config = LoadedConfig.get()
|
||||||
|
|
||||||
|
# version
|
||||||
user_input(config, 'version', "Enter kiwi-config version for this instance")
|
user_input(config, 'version', "Enter kiwi-config version for this instance")
|
||||||
|
|
||||||
|
# runtime
|
||||||
user_input(config, 'runtime:storage', "Enter local directory for service data")
|
user_input(config, 'runtime:storage', "Enter local directory for service data")
|
||||||
|
|
||||||
|
# markers
|
||||||
user_input(config, 'markers:project', "Enter marker string for project directories")
|
user_input(config, 'markers:project', "Enter marker string for project directories")
|
||||||
user_input(config, 'markers:down', "Enter marker string for disabled projects")
|
user_input(config, 'markers:down', "Enter marker string for disabled projects")
|
||||||
|
|
||||||
|
# network
|
||||||
user_input(config, 'network:name', "Enter name for local docker network")
|
user_input(config, 'network:name', "Enter name for local docker network")
|
||||||
user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
|
user_input(config, 'network:cidr', "Enter CIDR block for local docker network")
|
||||||
|
|
||||||
user_input_exe(config, 'docker')
|
# executables
|
||||||
user_input_exe(config, 'docker-compose')
|
user_input_exe(config, 'executables:docker')
|
||||||
user_input_exe(config, 'sudo')
|
user_input_exe(config, 'executables:docker-compose')
|
||||||
|
user_input_exe(config, 'executables:sudo')
|
||||||
|
|
||||||
config.save()
|
config.save()
|
||||||
|
|
Loading…
Reference in a new issue