2020-08-18 12:18:54 +00:00
|
|
|
# system
|
|
|
|
import logging
|
2020-08-19 14:10:56 +00:00
|
|
|
import os
|
2020-08-18 12:18:54 +00:00
|
|
|
|
2020-08-19 15:21:38 +00:00
|
|
|
# local
|
|
|
|
from .parser import Parser
|
2020-08-19 15:22:40 +00:00
|
|
|
from .projects import Projects
|
2020-08-12 14:43:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SubCommand:
|
2020-08-13 08:48:01 +00:00
|
|
|
"""represents kiwi [anything] command"""
|
|
|
|
|
|
|
|
# actual command string
|
2020-08-12 14:43:13 +00:00
|
|
|
__name = None
|
2020-08-13 08:48:01 +00:00
|
|
|
# command parser
|
2020-08-12 14:43:13 +00:00
|
|
|
_sub_parser = None
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
_action = None
|
|
|
|
|
2020-08-19 14:23:52 +00:00
|
|
|
def __init__(self, name, action, add_parser=True, **kwargs):
|
2020-08-12 14:43:13 +00:00
|
|
|
self.__name = name
|
2020-08-19 15:25:39 +00:00
|
|
|
self._action = action
|
|
|
|
|
2020-08-19 09:58:13 +00:00
|
|
|
if add_parser:
|
|
|
|
self._sub_parser = Parser().get_subparsers().add_parser(
|
|
|
|
name,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-08-12 14:43:13 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.__name
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
2020-08-12 14:43:13 +00:00
|
|
|
pass
|
2020-08-13 09:32:54 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def run(self, runner, args):
|
|
|
|
"""actually run command with parsed CLI args"""
|
|
|
|
|
|
|
|
# run for entire instance
|
|
|
|
logging.info(f"{self._action} kiwi-config instance at '{os.getcwd()}'")
|
|
|
|
return self._run_instance(runner, args)
|
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
|
|
|
|
class ProjectCommand(SubCommand):
|
|
|
|
"""this command concerns a project in current instance"""
|
|
|
|
|
2020-08-19 14:23:52 +00:00
|
|
|
def __init__(self, name, num_projects, action, add_parser=True, **kwargs):
|
2020-08-13 09:32:54 +00:00
|
|
|
super().__init__(
|
2020-08-19 14:10:56 +00:00
|
|
|
name, action=action, add_parser=add_parser,
|
2020-08-13 09:32:54 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
if num_projects == 1:
|
|
|
|
projects = "a project"
|
|
|
|
else:
|
2020-08-18 13:56:22 +00:00
|
|
|
projects = "project(s)"
|
2020-08-17 10:58:18 +00:00
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
self._sub_parser.add_argument(
|
2020-08-17 10:58:18 +00:00
|
|
|
'projects', metavar='project', nargs=num_projects, type=str,
|
|
|
|
help=f"select {projects} in this instance"
|
2020-08-13 09:32:54 +00:00
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_instance(self, runner, args):
|
|
|
|
# default: run for all enabled projects
|
|
|
|
return self._run_projects(runner, args, Projects.from_dir().filter_enabled())
|
|
|
|
|
|
|
|
def _run_projects(self, runner, args, projects):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self, runner, args):
|
|
|
|
projects = Projects.from_args(args)
|
|
|
|
|
|
|
|
if not projects.empty():
|
|
|
|
# project(s) given
|
|
|
|
logging.info(f"{self._action} projects {projects}")
|
|
|
|
return self._run_projects(runner, args, projects)
|
|
|
|
|
|
|
|
else:
|
|
|
|
return super().run(runner, args)
|
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
|
|
|
|
class ServiceCommand(ProjectCommand):
|
2020-08-13 12:26:49 +00:00
|
|
|
"""this command concerns service(s) in a project"""
|
2020-08-13 09:32:54 +00:00
|
|
|
|
2020-08-19 14:23:52 +00:00
|
|
|
def __init__(self, name, num_projects, num_services, action, add_parser=True, **kwargs):
|
2020-08-13 09:32:54 +00:00
|
|
|
super().__init__(
|
2020-08-19 14:10:56 +00:00
|
|
|
name, num_projects=num_projects, action=action, add_parser=add_parser,
|
2020-08-13 09:32:54 +00:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
if (isinstance(num_projects, str) and num_projects == '*') \
|
|
|
|
or (isinstance(num_projects, int) and num_projects > 1):
|
2020-08-19 14:23:52 +00:00
|
|
|
raise ValueError(f"Invalid choice for project count: {num_projects}")
|
2020-08-13 12:26:49 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
if num_services == 1:
|
|
|
|
services = "a service"
|
|
|
|
else:
|
2020-08-18 13:56:22 +00:00
|
|
|
services = "service(s)"
|
2020-08-13 12:26:49 +00:00
|
|
|
|
2020-08-13 09:32:54 +00:00
|
|
|
self._sub_parser.add_argument(
|
2020-08-17 10:58:18 +00:00
|
|
|
'services', metavar='service', nargs=num_services, type=str,
|
2020-08-13 12:26:49 +00:00
|
|
|
help=f"select {services} in a project"
|
2020-08-13 09:32:54 +00:00
|
|
|
)
|
2020-08-18 11:50:21 +00:00
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_projects(self, runner, args, projects):
|
2020-08-18 11:50:21 +00:00
|
|
|
result = True
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
# default: run without services for all given
|
|
|
|
for project in projects:
|
|
|
|
result &= self._run_services(runner, args, project, [])
|
2020-08-18 11:50:21 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def _run_services(self, runner, args, project, services):
|
2020-08-18 11:50:21 +00:00
|
|
|
pass
|
|
|
|
|
2020-08-19 14:10:56 +00:00
|
|
|
def run(self, runner, args):
|
|
|
|
if 'services' in args and args.services:
|
|
|
|
project = Projects.from_args(args)[0]
|
|
|
|
|
|
|
|
# run for service(s) inside project
|
|
|
|
logging.info(f"{self._action} project '{project.get_name()}', services {args.services}")
|
|
|
|
return self._run_services(runner, args, project, args.services)
|
|
|
|
|
|
|
|
else:
|
|
|
|
return super().run(runner, args)
|