diff --git a/cli/src/core/service_creator.py b/cli/src/core/service_creator.py index c1975b2..9602792 100644 --- a/cli/src/core/service_creator.py +++ b/cli/src/core/service_creator.py @@ -1,4 +1,5 @@ import logging +import re import shutil import subprocess from pathlib import Path @@ -22,6 +23,9 @@ REPO_ROOT = get_git_root(__file__) SERVICE_TEMPLATES_DIR = REPO_ROOT / "templates" / "service" SERVICE_DEST_DIR = REPO_ROOT / "services" +MASTER_SERVICE_DIR = REPO_ROOT / "services" / "000-master" / "current" +MASTER_IOC_SUBS_FILE = MASTER_SERVICE_DIR / "ioc" / "AGEBD-CPCL-MASTER_main.subs" + # TODO: lots of hardcoded paths. Create config object? @@ -64,6 +68,12 @@ class ServiceCreator: registry.write_to_config() hla_names.write_to_config() + self._append_to_master_ioc_subs( + service_name=service.name_upper, + starton="1", + autooff="0", + ) + # TODO: can't push if branch already exists in remote git_push_changes( repo=repo, @@ -134,3 +144,32 @@ class ServiceCreator: logger.error(f"Exit Code: {e.returncode}") logger.error(f"Details:\n{e.stderr}") raise UVError(e.stderr) from e + + def _append_to_master_ioc_subs( + self, + service_name: str, + starton: str = "0", + autooff: str = "0", + ) -> str: + """ + Parses the configuration text and appends a new row before the closing bracket. + """ + with open(MASTER_IOC_SUBS_FILE) as f: + file_content = f.read() + + new_row = f' {{ "{{{{ agebd_env }}}}" , "{service_name:<20}" , "{starton}", "{autooff}" }}' + + # Use regex to find the last closing curly brace of the configuration block + # This targets the line that contains only a lone closing brace, optional spaces, and maybe a comma/semicolon. + pattern = r"(\s*\n\s*\}(?:;|,)?\s*$)" + + if re.search(pattern, file_content, re.MULTILINE): + # Insert our new row right before that closing brace line + updated_content = re.sub( + pattern, f"\n{new_row}\n\\1", file_content, count=1, flags=re.MULTILINE + ) + return updated_content + else: + raise ValueError( + "Could not locate the closing structure format '}' inside the configuration." + )