diff --git a/src/kiwi/subcommands/_subcommand.py b/src/kiwi/subcommands/_subcommand.py index 2ff6d20..66687c6 100644 --- a/src/kiwi/subcommands/_subcommand.py +++ b/src/kiwi/subcommands/_subcommand.py @@ -1,5 +1,5 @@ # local -from .utils.project import get_project_name, list_projects +from .utils._misc import get_project_name, get_services, list_projects # parent from ..parser import Parser @@ -69,6 +69,8 @@ class ServiceCommand(ProjectCommand): class FlexCommand(ServiceCommand): + """this command concerns the entire instance, a whole project or just service(s) in a project""" + def __init__(self, name, **kwargs): super().__init__( name, num_projects='?', num_services='*', @@ -84,21 +86,23 @@ class FlexCommand(ServiceCommand): return result - def _run_project(self, runner, config, args): + def _run_project(self, runner, config, args, project_name): pass - def _run_services(self, runner, config, args): + def _run_services(self, runner, config, args, project_name, services): pass def run(self, runner, config, args): - if 'projects' not in args or args.projects is None: - # command for entire instance + project_name = get_project_name(args) + services = get_services(args) + + if project_name is None: + # no project given, run for entire instance return self._run_instance(runner, config, args) - elif 'services' not in args or not args.services: - # command for whole project - return self._run_project(runner, config, args) + if services is None: + # no services given, run for whole project + return self._run_project(runner, config, args, project_name) - else: - # command for service(s) inside project - return self._run_services(runner, config, args) + # run for service(s) inside project + return self._run_services(runner, config, args, project_name, services) diff --git a/src/kiwi/subcommands/conf.py b/src/kiwi/subcommands/conf.py index 9c0682d..ea2981d 100644 --- a/src/kiwi/subcommands/conf.py +++ b/src/kiwi/subcommands/conf.py @@ -5,7 +5,7 @@ import subprocess # local from ._subcommand import SubCommand -from .utils.project import list_projects, get_project_dir +from .utils._misc import list_projects, get_project_dir from .utils.rootkit import Rootkit, prefix_path_mnt # parent diff --git a/src/kiwi/subcommands/down.py b/src/kiwi/subcommands/down.py index 34c8681..6a9fc7d 100644 --- a/src/kiwi/subcommands/down.py +++ b/src/kiwi/subcommands/down.py @@ -4,8 +4,7 @@ import logging # local from ._subcommand import FlexCommand from .utils.dockercommand import DockerCommand -from .utils.project import get_project_name, list_projects -from .utils.user_input import are_you_sure +from .utils._misc import are_you_sure class DownCommand(FlexCommand): @@ -18,25 +17,25 @@ class DownCommand(FlexCommand): def _run_instance(self, runner, config, args): if are_you_sure("This will bring down the entire instance."): - super()._run_instance(runner, config, args) + return super()._run_instance(runner, config, args) return False - def _run_project(self, runner, config, args): - logging.info(f"Bringing down project '{get_project_name(args)}'") + def _run_project(self, runner, config, args, project_name): + logging.info(f"Bringing down project '{project_name}'") DockerCommand('docker-compose').run( config, args, ['down'] ) return True - def _run_services(self, runner, config, args): - logging.info(f"Bringing down services {args.services} in project '{get_project_name(args)}'") + def _run_services(self, runner, config, args, project_name, services): + logging.info(f"Bringing down services {services} in project '{project_name}'") DockerCommand('docker-compose').run( - config, args, ['stop', *args.services] + config, args, ['stop', *services] ) DockerCommand('docker-compose').run( - config, args, ['rm', '-f', *args.services] + config, args, ['rm', '-f', *services] ) return True diff --git a/src/kiwi/subcommands/net.py b/src/kiwi/subcommands/net.py index 4a5e488..10bd92c 100644 --- a/src/kiwi/subcommands/net.py +++ b/src/kiwi/subcommands/net.py @@ -5,7 +5,7 @@ import subprocess # local from ._subcommand import SubCommand from .utils.dockercommand import DockerCommand -from .utils.user_input import are_you_sure +from .utils._misc import are_you_sure def _find_net(config, args): diff --git a/src/kiwi/subcommands/up.py b/src/kiwi/subcommands/up.py index 1a9b61a..a1dedbb 100644 --- a/src/kiwi/subcommands/up.py +++ b/src/kiwi/subcommands/up.py @@ -4,7 +4,6 @@ import logging # local from ._subcommand import FlexCommand from .utils.dockercommand import DockerCommand -from .utils.project import get_project_name, list_projects class UpCommand(FlexCommand): @@ -15,18 +14,18 @@ class UpCommand(FlexCommand): 'up', description="Bring up the whole instance, a project or service(s) inside a project" ) - def _run_project(self, runner, config, args): - logging.info(f"Bringing up project '{get_project_name(args)}'") + def _run_project(self, runner, config, args, project_name): + logging.info(f"Bringing up project '{project_name}'") DockerCommand('docker-compose').run( config, args, ['up', '-d'] ) return True - def _run_services(self, runner, config, args): - logging.info(f"Bringing up services {args.services} in project '{get_project_name(args)}'") + def _run_services(self, runner, config, args, project_name, services): + logging.info(f"Bringing up services {services} in project '{project_name}'") DockerCommand('docker-compose').run( - config, args, ['up', '-d', *args.services] + config, args, ['up', '-d', *services] ) return True diff --git a/src/kiwi/subcommands/utils/project.py b/src/kiwi/subcommands/utils/_misc.py similarity index 53% rename from src/kiwi/subcommands/utils/project.py rename to src/kiwi/subcommands/utils/_misc.py index 12f9a9a..c9aeee3 100644 --- a/src/kiwi/subcommands/utils/project.py +++ b/src/kiwi/subcommands/utils/_misc.py @@ -13,6 +13,15 @@ def get_project_name(args): return None +def get_services(args): + """get services list from CLI args""" + + if args is not None and 'services' in args: + return args.services + + return None + + def get_project_dir(config, project_name): """get project directory""" @@ -41,3 +50,33 @@ def list_projects(config): projects = [p[:-len(config['markers:project'])] for p in project_dirs] return projects + + +def _surround(string, bang): + midlane = f"{bang * 3} {string} {bang * 3}" + sidelane = bang*len(midlane) + + return f"{sidelane}\n{midlane}\n{sidelane}" + + +def are_you_sure(prompt, default="no"): + if default.lower() == 'yes': + suffix = "[YES|no]" + else: + suffix = "[yes|NO]" + + answer = input( + f"{_surround('CAREFULING IN PROGRESS', '!')}\n" + f"\n" + f">>> {prompt} <<<\n" + f"\n" + f"Are you sure you want to proceed? {suffix} " + ).strip().lower() + + if answer == '': + answer = default + + while answer not in ['yes', 'no']: + answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower() + + return answer == 'yes' diff --git a/src/kiwi/subcommands/utils/dockercommand.py b/src/kiwi/subcommands/utils/dockercommand.py index 6ef40b6..5b1bebe 100644 --- a/src/kiwi/subcommands/utils/dockercommand.py +++ b/src/kiwi/subcommands/utils/dockercommand.py @@ -1,10 +1,11 @@ # system import logging +import os import subprocess # local from .executable import Executable -from .project import * +from ._misc import get_project_dir, get_project_name # parent from ..._constants import CONF_DIRECTORY_NAME diff --git a/src/kiwi/subcommands/utils/user_input.py b/src/kiwi/subcommands/utils/user_input.py deleted file mode 100644 index 51b71e5..0000000 --- a/src/kiwi/subcommands/utils/user_input.py +++ /dev/null @@ -1,29 +0,0 @@ - -def _surround(string, bang): - midlane = f"{bang * 3} {string} {bang * 3}" - sidelane = bang*len(midlane) - - return f"{sidelane}\n{midlane}\n{sidelane}" - - -def are_you_sure(prompt, default="no"): - if default.lower() == 'yes': - suffix = "[YES|no]" - else: - suffix = "[yes|NO]" - - answer = input( - f"{_surround('CAREFULING IN PROGRESS', '!')}\n" - f"\n" - f">>> {prompt} <<<\n" - f"\n" - f"Are you sure you want to proceed? {suffix} " - ).strip().lower() - - if answer == '': - answer = default - - while answer not in ['yes', 'no']: - answer = input("Please type 'yes' or 'no' explicitly: ").strip().lower() - - return answer == 'yes'