feat: Refactor image handling in Docker Compose file processing for improved indentation management
This commit is contained in:
+35
-20
@@ -69,45 +69,60 @@ with open(compose_file, "r", encoding="utf-8") as f:
|
|||||||
|
|
||||||
out = []
|
out = []
|
||||||
in_app = False
|
in_app = False
|
||||||
in_build = False
|
app_indent = None
|
||||||
image_set = False
|
app_service_indent = None
|
||||||
|
skip_build_block = False
|
||||||
|
|
||||||
|
def leading_spaces(text):
|
||||||
|
return len(text) - len(text.lstrip(" "))
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
stripped = line.lstrip(" ")
|
stripped = line.lstrip(" ")
|
||||||
indent = len(line) - len(stripped)
|
indent = leading_spaces(line)
|
||||||
|
|
||||||
if not in_app and re.match(r"^\s{2}app:\s*$", line):
|
if not in_app and re.match(r"^\s*app:\s*$", line):
|
||||||
in_app = True
|
in_app = True
|
||||||
image_set = False
|
app_indent = indent
|
||||||
|
app_service_indent = None
|
||||||
|
skip_build_block = False
|
||||||
out.append(line)
|
out.append(line)
|
||||||
out.append(f" image: {target_image}\n")
|
|
||||||
image_set = True
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if in_app:
|
if in_app:
|
||||||
if indent == 2 and re.match(r"^[A-Za-z0-9_-]+:\s*$", stripped):
|
if app_service_indent is None and indent > app_indent:
|
||||||
|
app_service_indent = indent
|
||||||
|
|
||||||
|
if indent <= app_indent and re.match(r"^[A-Za-z0-9_-]+:\s*$", stripped):
|
||||||
in_app = False
|
in_app = False
|
||||||
in_build = False
|
app_indent = None
|
||||||
|
app_service_indent = None
|
||||||
|
skip_build_block = False
|
||||||
|
|
||||||
if in_app:
|
if in_app:
|
||||||
if in_build:
|
if app_service_indent is None:
|
||||||
if indent > 4:
|
app_service_indent = indent
|
||||||
continue
|
|
||||||
in_build = False
|
|
||||||
|
|
||||||
if re.match(r"^\s{4}build:\s*$", line):
|
if skip_build_block:
|
||||||
in_build = True
|
if indent > app_service_indent:
|
||||||
|
continue
|
||||||
|
skip_build_block = False
|
||||||
|
|
||||||
|
if re.match(rf"^\s{{{app_service_indent}}}build:\s*$", line):
|
||||||
|
skip_build_block = True
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if re.match(r"^\s{4}image:\s*", line):
|
if re.match(rf"^\s{{{app_service_indent}}}image:\s*", line):
|
||||||
if image_set:
|
|
||||||
continue
|
|
||||||
out.append(f" image: {target_image}\n")
|
|
||||||
image_set = True
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if re.match(rf"^\s{{{app_service_indent}}}[A-Za-z0-9_-]+:\s*$", line):
|
||||||
|
out.append(f"{' ' * app_service_indent}image: {target_image}\n")
|
||||||
|
app_service_indent = None
|
||||||
|
|
||||||
out.append(line)
|
out.append(line)
|
||||||
|
|
||||||
|
if in_app and app_service_indent is not None:
|
||||||
|
out.append(f"{' ' * app_service_indent}image: {target_image}\n")
|
||||||
|
|
||||||
with open(compose_file, "w", encoding="utf-8") as f:
|
with open(compose_file, "w", encoding="utf-8") as f:
|
||||||
f.writelines(out)
|
f.writelines(out)
|
||||||
PY
|
PY
|
||||||
|
|||||||
Reference in New Issue
Block a user