From 989eab8ab2fd3b8cfa8f1cda9fdfc9242f7591ab Mon Sep 17 00:00:00 2001 From: Benjamin Labrecque Date: Tue, 7 Jul 2026 11:37:11 +0200 Subject: [PATCH] cli: undo actions on exception --- cli/src/core/service_creator.py | 40 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/cli/src/core/service_creator.py b/cli/src/core/service_creator.py index 33e4176..fe0e616 100644 --- a/cli/src/core/service_creator.py +++ b/cli/src/core/service_creator.py @@ -1,3 +1,6 @@ +import shutil +from pathlib import Path + import copier import git @@ -32,14 +35,19 @@ class ServiceCreator: assert_clean_repo(repo) original_branch = repo.active_branch.name + # Keep old configs to revert in case of Exception + hla_names_old = MasterHLANames.read_from_config() + hla_names = MasterHLANames.read_from_config() + hla_names.add_hla_name(name) + registry_old = ServiceRegistry.read_from_config() + registry = ServiceRegistry.read_from_config() + service = registry.add_service(name) + + branch_name = f"feature/add-service-{service.name_lower}" + delete_local_branch(repo, branch_name) + switch_branch(repo, branch_name) + try: - registry = ServiceRegistry.read_from_config() - service = registry.add_service(name) - - branch_name = f"feature/add-service-{service.name_lower}" - delete_local_branch(repo, branch_name) - switch_branch(repo, branch_name) - self._render_new_service_templates( service=service, ioc_host=self.ioc_host, @@ -47,21 +55,23 @@ class ServiceCreator: user=self.user, ) registry.write_to_config() - self._update_master_hla_names(name=service.name_upper) + hla_names.write_to_config() # TODO: can't push if branch already exists in remote - service_dir_rel_path = f"services/{service.dir_name}/current" git_push_changes( repo=repo, branch_name=branch_name, - service_dir_rel_path=service_dir_rel_path, + service_dir_rel_path=f"services/{service.dir_name}/current", registry_file_rel_path="config/services_registry.yml", # TODO: maybe assert/get correct master service dirname from registry - master_hla_names_rel_path=f"{service_dir_rel_path}/app/config/hla_names.yml", + master_hla_names_rel_path="services/000-master/current/app/config/hla_names.yml", commit_msg=f"feature: add new service {service.name_lower}", ) switch_branch(repo, original_branch) except Exception as e: + shutil.rmtree(Path(SERVICE_DEST_DIR / service.dir_name), ignore_errors=True) + registry_old.write_to_config() + hla_names_old.write_to_config() switch_branch(repo, original_branch) raise e @@ -88,11 +98,3 @@ class ServiceCreator: "user": user, }, ) - - def _update_master_hla_names(self, name: str): - """ - Add new HLA name to MASTER so it can monitor it - """ - hla_names = MasterHLANames.read_from_config() - hla_names.add_hla_name(name) - hla_names.write_to_config()