This commit is contained in:
2025-11-30 17:12:46 +01:00
parent befd931165
commit 5ef7c025f4
3 changed files with 62 additions and 50 deletions

View File

@@ -9,41 +9,57 @@ OUTPUT_FILE = f"__swarm/{stack_name}/{stack_name}-swarm.yml"
def fix_env_file(filepath):
"""Convert YAML-style env (KEY: value) into Docker env (KEY=value)."""
"""Convert YAML-style env (KEY: value) Docker env (KEY=value)."""
fixed_lines = []
changed = False
with open(filepath, "r") as f:
for line in f:
for raw_line in f:
line = raw_line.rstrip("\n")
stripped = line.strip()
# Skip empty/comment lines
# Preserve comments and blank lines
if not stripped or stripped.startswith("#"):
fixed_lines.append(line)
fixed_lines.append(raw_line)
continue
# Match YAML-style: KEY: value
m = re.match(r"^([A-Za-z0-9_]+):\s*(.*)$", stripped)
if m:
key, value = m.groups()
fixed = f"{key}={value}\n"
fixed_lines.append(fixed)
changed = True
else:
# Validate Docker env format
if " " in stripped:
raise ValueError(f"Invalid env line (contains spaces): {stripped}")
if ":" in stripped:
raise ValueError(f"Invalid env line (contains colon): {stripped}")
fixed_lines.append(line)
# Detect YAML-style: KEY: value
# MUST convert
if ":" in stripped and "=" not in stripped.split(":")[0]:
key, value = stripped.split(":", 1)
key = key.strip()
value = value.strip()
# Write back only if changes were needed
# Validate env key
if not re.match(r"^[A-Za-z0-9_]+$", key):
raise ValueError(f"Invalid variable name: {key}")
fixed_lines.append(f"{key}={value}\n")
changed = True
continue
# Detect valid Docker-style: KEY=value
if "=" in stripped:
key, value = stripped.split("=", 1)
# Validate key
if not re.match(r"^[A-Za-z0-9_]+$", key):
raise ValueError(f"Invalid environment variable name: {key}")
# Value may contain anything
fixed_lines.append(raw_line)
continue
# Anything else is invalid
raise ValueError(f"Invalid env line: {stripped}")
# Write file if modified
if changed:
with open(filepath, "w") as f:
f.writelines(fixed_lines)
print(f"[FIXED] Converted YAML env → Docker env in {filepath}")
print(f"[FIXED] Converted YAML → Docker env format in {filepath}")
else:
print(f"[OK] .env file already valid: {filepath}")
print(f"[OK] .env file valid: {filepath}")
def convert_ports(ports):
"""Convert short port syntax to Swarm long syntax."""