1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00
kiwi-scp/src/kiwi/subcommand.py

125 lines
3.6 KiB
Python
Raw Normal View History

# system
import logging
import os
2020-08-19 15:21:38 +00:00
# local
from .parser import Parser
from .project 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
_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
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
def _run_instance(self, runner, args):
2020-08-12 14:43:13 +00:00
pass
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)
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):
super().__init__(
name, action=action, add_parser=add_parser,
**kwargs
)
if num_projects == 1:
projects = "a project"
else:
2020-08-18 13:56:22 +00:00
projects = "project(s)"
self._sub_parser.add_argument(
'projects', metavar='project', nargs=num_projects, type=str,
help=f"select {projects} in this instance"
)
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)
class ServiceCommand(ProjectCommand):
2020-08-13 12:26:49 +00:00
"""this command concerns service(s) in a project"""
2020-08-19 14:23:52 +00:00
def __init__(self, name, num_projects, num_services, action, add_parser=True, **kwargs):
super().__init__(
name, num_projects=num_projects, action=action, add_parser=add_parser,
**kwargs
)
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
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
self._sub_parser.add_argument(
'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-18 11:50:21 +00:00
def _run_projects(self, runner, args, projects):
2020-08-18 11:50:21 +00:00
result = True
# 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
def _run_services(self, runner, args, project, services):
2020-08-18 11:50:21 +00:00
pass
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)