diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 8a659df2..285ca600 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -9,6 +9,7 @@ from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.install_service import InstallService from cpl_cli.command.new_service import NewService from cpl_cli.command.publish_service import PublishService +from cpl_cli.command.remove_service import RemoveService from cpl_cli.command.start_service import StartService from cpl_cli.command.uninstall_service import UninstallService from cpl_cli.command.update_service import UpdateService @@ -38,6 +39,7 @@ class CLI(ApplicationABC): self._command_handler.add_command(CommandModel('install', ['i', 'I'], InstallService, True, True)) self._command_handler.add_command(CommandModel('new', ['n', 'N'], NewService, False, True)) self._command_handler.add_command(CommandModel('publish', ['p', 'P'], PublishService, True, True)) + self._command_handler.add_command(CommandModel('remove', ['r', 'R'], RemoveService, True, False)) self._command_handler.add_command(CommandModel('start', ['s', 'S'], StartService, True, True)) self._command_handler.add_command(CommandModel('uninstall', ['ui', 'UI'], UninstallService, True, True)) self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, True, True)) diff --git a/src/cpl_cli/command/remove_service.py b/src/cpl_cli/command/remove_service.py new file mode 100644 index 00000000..89457e57 --- /dev/null +++ b/src/cpl_cli/command/remove_service.py @@ -0,0 +1,88 @@ +import os +import shutil +import json + +from cpl.configuration.configuration_abc import ConfigurationABC +from cpl.console.console import Console +from cpl.console.foreground_color_enum import ForegroundColorEnum +from cpl.environment.application_environment_abc import ApplicationEnvironmentABC +from cpl_cli.command_abc import CommandABC +from cpl_cli.configuration import WorkspaceSettings, WorkspaceSettingsNameEnum + + +class RemoveService(CommandABC): + + def __init__(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + """ + Service for CLI command remove + :param config: + :param env: + """ + CommandABC.__init__(self) + + self._config = config + self._env = env + + self._workspace: WorkspaceSettings = self._config.get_configuration(WorkspaceSettings) + + @staticmethod + def _create_file(file_name: str, content: dict): + if not os.path.isabs(file_name): + file_name = os.path.abspath(file_name) + + path = os.path.dirname(file_name) + if not os.path.isdir(path): + os.makedirs(path) + + with open(file_name, 'w') as project_json: + project_json.write(json.dumps(content, indent=2)) + project_json.close() + + @staticmethod + def _remove_sources(path: str): + shutil.rmtree(path) + + def _create_workspace(self, path: str): + ws_dict = { + WorkspaceSettings.__name__: { + WorkspaceSettingsNameEnum.default_project.value: self._workspace.default_project, + WorkspaceSettingsNameEnum.projects.value: self._workspace.projects + } + } + + self._create_file(path, ws_dict) + + def run(self, args: list[str]): + """ + Entry point of command + :param args: + :return: + """ + + project_name = args[0] + if project_name not in self._workspace.projects: + Console.error(f'Project {project_name} not found in workspace.') + return + + if project_name == self._workspace.default_project: + Console.error(f'Project {project_name} is the default project.') + return + + src_path = os.path.abspath(os.path.dirname(self._workspace.projects[project_name])) + Console.spinner( + f'Removing {src_path}', + self._remove_sources, + src_path, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + del self._workspace.projects[project_name] + path = 'cpl-workspace.json' + Console.spinner( + f'Changing {path}', + self._create_workspace, + path, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index 25c7ebde..d2ad9ec6 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -10,6 +10,7 @@ from cpl_cli.command.generate_service import GenerateService from cpl_cli.command.install_service import InstallService from cpl_cli.command.new_service import NewService from cpl_cli.command.publish_service import PublishService +from cpl_cli.command.remove_service import RemoveService from cpl_cli.command.start_service import StartService from cpl_cli.command.uninstall_service import UninstallService from cpl_cli.command.update_service import UpdateService @@ -58,6 +59,7 @@ class Startup(StartupABC): ConsoleArgument('', 'library', ['l', 'L'], ' ') ])) self._configuration.add_console_argument(ConsoleArgument('', 'publish', ['p', 'P'], '')) + self._configuration.add_console_argument(ConsoleArgument('', 'remove', ['r', 'R'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'start', ['s', 'S'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'uninstall', ['ui', 'UI'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], '')) @@ -78,6 +80,7 @@ class Startup(StartupABC): self._services.add_transient(InstallService) self._services.add_transient(NewService) self._services.add_transient(PublishService) + self._services.add_transient(RemoveService) self._services.add_transient(StartService) self._services.add_transient(UninstallService) self._services.add_transient(UpdateService)