openenv/echo_env
6
1# SPDX-License-Identifier: BSD-3-Clause2 3"""Shared helpers for reading environment server configuration."""4 5from __future__ import annotations6 7import json8import re9 10import yaml11 12 13def parse_openenv_app_field(yaml_content: str) -> str | None:14 """Extract the top-level ``app`` value from raw ``openenv.yaml`` content."""15 try:16 data = yaml.safe_load(yaml_content) or {}17 except Exception:18 return None19 20 if not isinstance(data, dict):21 return None22 23 value = data.get("app")24 if isinstance(value, str):25 value = value.strip()26 return value if value else None27 return None28 29 30def parse_dockerfile_cmd(dockerfile_content: str) -> str | None:31 """Extract the server command from the last Dockerfile ``CMD``."""32 last_cmd: str | None = None33 for line in dockerfile_content.splitlines():34 stripped = line.strip()35 if stripped.startswith("#"):36 continue37 match = re.match(r"CMD\s+(.+)", stripped, flags=re.IGNORECASE)38 if match:39 last_cmd = match.group(1).strip()40 41 if last_cmd is None:42 return None43 44 if last_cmd.startswith("["):45 try:46 parts = json.loads(last_cmd)47 if isinstance(parts, list) and all(isinstance(p, str) for p in parts):48 return " ".join(parts)49 except (json.JSONDecodeError, TypeError):50 # Invalid exec-form JSON falls back to the raw CMD below.51 pass52 53 return last_cmd if last_cmd else None54 