diff --git a/src/kiwi/subcommands/__init__.py b/src/kiwi/subcommands/__init__.py index e3b6ed4..b265b46 100644 --- a/src/kiwi/subcommands/__init__.py +++ b/src/kiwi/subcommands/__init__.py @@ -5,6 +5,7 @@ from .logs import LogsCommand from .net import NetUpCommand, NetDownCommand from .sh import ShCommand from .show import ShowCommand +from .up import UpCommand __all__ = [ 'CmdCommand', @@ -14,4 +15,5 @@ __all__ = [ 'NetDownCommand', 'ShCommand', 'ShowCommand', + 'UpCommand', ] diff --git a/src/kiwi/subcommands/up.py b/src/kiwi/subcommands/up.py new file mode 100644 index 0000000..f3e3753 --- /dev/null +++ b/src/kiwi/subcommands/up.py @@ -0,0 +1,33 @@ +# system +import logging + +# local +from ._subcommand import ServiceCommand +from .utils.dockercommand import DockerCommand +from .utils.project import list_projects + + +class UpCommand(ServiceCommand): + """kiwi up""" + + def __init__(self): + super().__init__( + 'up', num_projects='?', num_services='*', + description="Start the whole instance, a project or service(s) inside a project" + ) + + def run(self, runner, config, args): + if args.projects is None: + # "up" for all projects + for project_name in list_projects(config): + logging.info(f"Bringing up project '{project_name}'") + args.projects = project_name + + runner.run('up') + + return + + runner.run('net-up') + DockerCommand('docker-compose').run( + config, args, ['up', '-d', *args.services] + ) diff --git a/src/kiwi/subcommands/utils/project.py b/src/kiwi/subcommands/utils/project.py new file mode 100644 index 0000000..ae02225 --- /dev/null +++ b/src/kiwi/subcommands/utils/project.py @@ -0,0 +1,37 @@ +import os + + +def get_project_name(args): + """get project name from CLI args""" + + if args is not None and 'projects' in args: + if isinstance(args.projects, list) and len(args.projects) > 0: + return args.projects[0] + else: + return args.projects + + return None + + +def get_project_dir(config, project_name): + """get project directory""" + + return f"{project_name}{config['markers:project']}" + + +def list_projects(config): + """list projects in current instance""" + + # complete dir listing + content = os.listdir() + + # filter directories + dirs = [d for d in content if os.path.isdir(d)] + + # filter suffix + project_dirs = [p for p in dirs if p.endswith(config['markers:project'])] + + # remove suffix + projects = [p[:-len(config['markers:project'])] for p in project_dirs] + + return projects