From 46f78a88966da3e1a2746b866d044bb7e66747a1 Mon Sep 17 00:00:00 2001 From: Moritz Date: Thu, 9 Nov 2023 12:35:18 +0100 Subject: [PATCH] add comment function --- alakazam.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/alakazam.py b/alakazam.py index 39ee6d1..af37db2 100755 --- a/alakazam.py +++ b/alakazam.py @@ -216,6 +216,8 @@ def new_app(recipe, domain, server): def update_configs(path, config): if uncomment_keys := config.get("uncomment"): uncomment(uncomment_keys, path, True) + if comment_keys := config.get("comment"): + comment(comment_keys, path, True) if envs := config.get("env"): uncomment(envs.keys(), path) for key, value in envs.items(): @@ -334,6 +336,21 @@ def uncomment(keys, path, match_all=False): file.write(line) +def comment(keys, path, match_all=False): + logging.debug(f'Comment {keys} in {path}') + with open(path, "r") as file: + lines = file.readlines() + with open(path, "w") as file: + for line in lines: + line_match = line.split("=")[0] # Match only keys + if match_all: + line_match = line + if any(key in line_match for key in keys): + line = line.lstrip("#").lstrip() + line = f"#{line}" + file.write(line) + + def exchange_domains(instance, instance_config, path): for app in instance_config: old_app_domain = f'{app}.example.com'