Resolve ${VAR} environment variable reference in a string value. The pattern must match the entire string (e.g., "${OPENAI_API_KEY}"), not be embedded within other text. Args: value: The string value that may contain ${VAR} syntax. Returns: The resolved environ
(value: Optional[str])
| 23 | |
| 24 | |
| 25 | def _resolve_env_var(value: Optional[str]) -> Optional[str]: |
| 26 | """ |
| 27 | Resolve ${VAR} environment variable reference in a string value. |
| 28 | The pattern must match the entire string (e.g., "${OPENAI_API_KEY}"), |
| 29 | not be embedded within other text. |
| 30 | |
| 31 | Args: |
| 32 | value: The string value that may contain ${VAR} syntax. |
| 33 | |
| 34 | Returns: |
| 35 | The resolved environment variable value, or the original value if it doesn't match the pattern. |
| 36 | |
| 37 | Raises: |
| 38 | ValueError: If the environment variable is referenced but not set. |
| 39 | """ |
| 40 | if value is None: |
| 41 | return None |
| 42 | |
| 43 | match = _ENV_VAR_PATTERN.match(value) |
| 44 | if not match: |
| 45 | return value |
| 46 | |
| 47 | var_name = match.group(1) |
| 48 | env_value = os.environ.get(var_name) |
| 49 | if env_value is None: |
| 50 | raise ValueError(f"Environment variable {var_name} is not set") |
| 51 | return env_value |
| 52 | |
| 53 | |
| 54 | @dataclass |