diff --git a/yaml_convert2.py b/yaml_convert2.py index 3ff9f19..a11509b 100644 --- a/yaml_convert2.py +++ b/yaml_convert2.py @@ -1,9 +1,50 @@ import yaml import sys - +import re stack_name = sys.argv[1] INPUT_FILE = f"{stack_name}/docker-compose.yml" 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).""" + fixed_lines = [] + changed = False + + with open(filepath, "r") as f: + for line in f: + stripped = line.strip() + + # Skip empty/comment lines + if not stripped or stripped.startswith("#"): + fixed_lines.append(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) + + # Write back only if changes were needed + if changed: + with open(filepath, "w") as f: + f.writelines(fixed_lines) + print(f"[FIXED] Converted YAML env → Docker env in {filepath}") + else: + print(f"[OK] .env file already valid: {filepath}") + def convert_ports(ports): """Convert short port syntax to Swarm long syntax.""" result = [] @@ -98,6 +139,7 @@ def convert_compose_to_swarm(data): return data def main(): + fix_env_file(f"__swarm/{stack_name}/stack.env") # NEW FIX STEP with open(INPUT_FILE, "r") as f: compose = yaml.safe_load(f) #input(compose)