2021-03-03 19:37:35 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-03-03 18:37:54 +01:00
|
|
|
from cpl.application.application_abc import ApplicationABC
|
2021-03-19 14:15:53 +01:00
|
|
|
from cpl.configuration.configuration_abc import ConfigurationABC
|
2021-03-15 18:25:53 +01:00
|
|
|
from cpl.console.console import Console
|
2021-03-19 14:15:53 +01:00
|
|
|
from cpl.dependency_injection import ServiceProviderABC
|
2021-04-11 15:36:04 +02:00
|
|
|
from cpl_cli.command.add_service import AddService
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl_cli.command.build_service import BuildService
|
|
|
|
from cpl_cli.command.generate_service import GenerateService
|
2021-03-13 22:53:28 +01:00
|
|
|
from cpl_cli.command.install_service import InstallService
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl_cli.command.new_service import NewService
|
|
|
|
from cpl_cli.command.publish_service import PublishService
|
2021-04-11 12:33:05 +02:00
|
|
|
from cpl_cli.command.remove_service import RemoveService
|
2021-03-12 22:53:02 +01:00
|
|
|
from cpl_cli.command.start_service import StartService
|
2021-03-14 10:58:59 +01:00
|
|
|
from cpl_cli.command.uninstall_service import UninstallService
|
2021-03-13 21:39:59 +01:00
|
|
|
from cpl_cli.command.update_service import UpdateService
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl_cli.command_handler_service import CommandHandler
|
2021-03-04 19:06:53 +01:00
|
|
|
from cpl_cli.command_model import CommandModel
|
2021-03-04 06:42:40 +01:00
|
|
|
from cpl_cli.error import Error
|
2021-03-12 16:06:30 +01:00
|
|
|
from cpl_cli.command.help_service import HelpService
|
|
|
|
from cpl_cli.command.version_service import VersionService
|
2021-03-03 18:37:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CLI(ApplicationABC):
|
|
|
|
|
2021-03-29 08:56:18 +02:00
|
|
|
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
CPL CLI
|
|
|
|
"""
|
2021-03-29 08:56:18 +02:00
|
|
|
ApplicationABC.__init__(self, config, services)
|
2021-03-03 18:37:54 +01:00
|
|
|
|
2021-03-03 19:37:35 +01:00
|
|
|
self._command_handler: Optional[CommandHandler] = None
|
2021-04-12 19:43:55 +02:00
|
|
|
self._options: list[str] = []
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-03 18:37:54 +01:00
|
|
|
def configure(self):
|
2021-03-03 19:37:35 +01:00
|
|
|
self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
|
2021-03-03 18:37:54 +01:00
|
|
|
|
2021-04-11 15:36:04 +02:00
|
|
|
self._command_handler.add_command(CommandModel('add', ['a', 'a'], AddService, False, False, False))
|
|
|
|
self._command_handler.add_command(CommandModel('build', ['b', 'B'], BuildService, False, True, True))
|
2021-04-11 13:00:26 +02:00
|
|
|
self._command_handler.add_command(CommandModel('generate', ['g', 'G'], GenerateService, False, True, False))
|
|
|
|
self._command_handler.add_command(CommandModel('help', ['h', 'H'], HelpService, False, False, False))
|
|
|
|
self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, False, True, True))
|
|
|
|
self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, False, True))
|
|
|
|
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, False, True, True))
|
|
|
|
self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, True, False))
|
|
|
|
self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, False, True, True))
|
|
|
|
self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, False, True, True))
|
|
|
|
self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, False, True, True))
|
|
|
|
self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False, False, False))
|
2021-03-03 18:37:54 +01:00
|
|
|
|
2021-04-12 19:43:55 +02:00
|
|
|
self._command_handler.add_command(CommandModel('--help', ['-h', '-H'], HelpService, False, False, False))
|
|
|
|
self._options.append('--help')
|
|
|
|
|
2021-03-03 18:37:54 +01:00
|
|
|
def main(self):
|
2021-03-14 16:01:15 +01:00
|
|
|
"""
|
|
|
|
Entry point of the CPL CLI
|
|
|
|
:return:
|
|
|
|
"""
|
2021-03-15 18:25:53 +01:00
|
|
|
try:
|
|
|
|
command = None
|
|
|
|
args = []
|
|
|
|
if len(self._configuration.additional_arguments) > 0:
|
2021-04-12 19:43:55 +02:00
|
|
|
is_option = False
|
|
|
|
for opt in self._options:
|
|
|
|
if opt in self._configuration.additional_arguments:
|
|
|
|
is_option = True
|
|
|
|
command = opt
|
|
|
|
args = self._configuration.additional_arguments
|
|
|
|
args.remove(opt)
|
|
|
|
|
|
|
|
if not is_option:
|
|
|
|
command = self._configuration.additional_arguments[0]
|
|
|
|
if len(self._configuration.additional_arguments) > 1:
|
|
|
|
args = self._configuration.additional_arguments[1:]
|
2021-03-15 18:25:53 +01:00
|
|
|
else:
|
|
|
|
for cmd in self._command_handler.commands:
|
|
|
|
result = self._configuration.get_configuration(cmd.name)
|
2021-04-12 20:22:28 +02:00
|
|
|
result_args: list[str] = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments')
|
|
|
|
is_option = False
|
|
|
|
for opt in self._options:
|
|
|
|
if opt == result:
|
|
|
|
is_option = True
|
|
|
|
command = opt
|
|
|
|
|
|
|
|
elif result_args is not None and opt in result_args:
|
|
|
|
is_option = True
|
|
|
|
command = opt
|
|
|
|
result_args.remove(opt)
|
|
|
|
|
|
|
|
if is_option:
|
|
|
|
args.append(cmd.name)
|
2021-04-13 15:27:19 +02:00
|
|
|
if result_args is not None:
|
|
|
|
for arg in result_args:
|
|
|
|
args.append(arg)
|
2021-04-12 20:22:28 +02:00
|
|
|
|
|
|
|
elif result is not None:
|
2021-03-15 18:25:53 +01:00
|
|
|
command = cmd.name
|
|
|
|
args.append(result)
|
2021-03-13 22:53:28 +01:00
|
|
|
|
2021-04-11 15:36:04 +02:00
|
|
|
for arg in result_args:
|
|
|
|
args.append(arg)
|
|
|
|
|
2021-03-15 18:25:53 +01:00
|
|
|
if command is None:
|
|
|
|
Error.error(f'Expected command')
|
|
|
|
return
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-15 18:25:53 +01:00
|
|
|
self._command_handler.handle(command, args)
|
|
|
|
except KeyboardInterrupt:
|
2021-03-15 18:28:33 +01:00
|
|
|
Console.write_line()
|
2021-03-15 18:25:53 +01:00
|
|
|
exit()
|