From 6819eabb6739ace3afc4ecac18e2fe04badef781 Mon Sep 17 00:00:00 2001 From: ldericher Date: Thu, 20 Aug 2020 15:09:15 +0200 Subject: [PATCH] QoL: Added _run_project overridable with automatic call for multiple projects --- src/kiwi/subcommand.py | 18 ++++++++++-------- src/kiwi/subcommands/cmd.py | 4 ++-- src/kiwi/subcommands/disable.py | 7 ++----- src/kiwi/subcommands/down.py | 6 ++---- src/kiwi/subcommands/enable.py | 7 ++----- src/kiwi/subcommands/inspect.py | 3 +-- src/kiwi/subcommands/new.py | 22 +++++++++------------- 7 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/kiwi/subcommand.py b/src/kiwi/subcommand.py index 150a222..072ce3f 100644 --- a/src/kiwi/subcommand.py +++ b/src/kiwi/subcommand.py @@ -65,6 +65,13 @@ class ProjectCommand(SubCommand): return self._run_projects(runner, args, Projects.from_dir().filter_enabled()) def _run_projects(self, runner, args, projects): + # default: run for all given projects + return all([ + self._run_project(runner, args, project) + for project in projects + ]) + + def _run_project(self, runner, args, project): pass def run(self, runner, args): @@ -102,14 +109,9 @@ class ServiceCommand(ProjectCommand): help=f"select {services} in a project" ) - def _run_projects(self, runner, args, projects): - result = True - - # default: run without services for all given - for project in projects: - result &= self._run_services(runner, args, project, []) - - return result + def _run_project(self, runner, args, project): + # default: run with empty service list + return self._run_services(runner, args, project, []) def _run_services(self, runner, args, project, services): pass diff --git a/src/kiwi/subcommands/cmd.py b/src/kiwi/subcommands/cmd.py index da90fc9..c890edf 100644 --- a/src/kiwi/subcommands/cmd.py +++ b/src/kiwi/subcommands/cmd.py @@ -27,7 +27,7 @@ class CmdCommand(ProjectCommand): help="arguments for 'docker-compose' commands" ) - def _run_projects(self, runner, args, projects): + def _run_project(self, runner, args, project): if args.unknowns: args.compose_args = [*args.compose_args, *args.unknowns] args.unknowns = [] @@ -35,6 +35,6 @@ class CmdCommand(ProjectCommand): logging.debug(f"Updated args: {args}") # run with split compose_cmd argument - projects[0].compose_run([args.compose_cmd, *args.compose_args]) + project.compose_run([args.compose_cmd, *args.compose_args]) return True diff --git a/src/kiwi/subcommands/disable.py b/src/kiwi/subcommands/disable.py index c10eec9..d4d762d 100644 --- a/src/kiwi/subcommands/disable.py +++ b/src/kiwi/subcommands/disable.py @@ -12,8 +12,5 @@ class DisableCommand(ProjectCommand): description="Disable project(s) in this instance" ) - def _run_projects(self, runner, args, projects): - return all([ - project.disable() - for project in projects - ]) + def _run_project(self, runner, args, project): + return project.disable() diff --git a/src/kiwi/subcommands/down.py b/src/kiwi/subcommands/down.py index 213eca9..cd53b3e 100644 --- a/src/kiwi/subcommands/down.py +++ b/src/kiwi/subcommands/down.py @@ -24,10 +24,8 @@ class DownCommand(ServiceCommand): return False - def _run_projects(self, runner, args, projects): - for project in projects: - project.compose_run(['down']) - + def _run_project(self, runner, args, project): + project.compose_run(['down']) return True def _run_services(self, runner, args, project, services): diff --git a/src/kiwi/subcommands/enable.py b/src/kiwi/subcommands/enable.py index 730fb69..1262a7f 100644 --- a/src/kiwi/subcommands/enable.py +++ b/src/kiwi/subcommands/enable.py @@ -12,8 +12,5 @@ class EnableCommand(ProjectCommand): description="Enable project(s) in this instance" ) - def _run_projects(self, runner, args, projects): - return all([ - project.enable() - for project in projects - ]) + def _run_project(self, runner, args, project): + return project.enable() diff --git a/src/kiwi/subcommands/inspect.py b/src/kiwi/subcommands/inspect.py index 1a36b03..a7c139e 100644 --- a/src/kiwi/subcommands/inspect.py +++ b/src/kiwi/subcommands/inspect.py @@ -51,8 +51,7 @@ class InspectCommand(ServiceCommand): return True - def _run_projects(self, runner, args, projects): - project = projects[0] + def _run_project(self, runner, args, project): if not project.exists(): logging.warning(f"Project '{project.get_name()}' not found") return False diff --git a/src/kiwi/subcommands/new.py b/src/kiwi/subcommands/new.py index cfac9b1..5fc5243 100644 --- a/src/kiwi/subcommands/new.py +++ b/src/kiwi/subcommands/new.py @@ -18,17 +18,13 @@ class NewCommand(ProjectCommand): description="Create new empty project(s) in this instance" ) - def _run_projects(self, runner, args, projects): - result = True + def _run_project(self, runner, args, project): + if project.exists(): + logging.error(f"Project '{project.get_name()}' exists in this instance!") + return False - for project in projects: - if project.exists(): - logging.error(f"Project '{project.get_name()}' exists in this instance!") - result = False - - else: - logging.info(f"Creating project '{project.get_name()}'") - os.mkdir(project.disabled_dir_name()) - shutil.copy(DEFAULT_DOCKER_COMPOSE_NAME, project.compose_file_name()) - - return result + else: + os.mkdir(project.disabled_dir_name()) + shutil.copy(DEFAULT_DOCKER_COMPOSE_NAME, project.compose_file_name()) + logging.debug(f"Project '{project.get_name()}' created") + return True