import os

from ..exceptions import ConfigurationError


def resolve_from_env(
    value: str | None,
    env_var: str,
) -> str:
    """Resolve a configuration value from an explicit argument or environment variable.

    Args:
        value: Explicit value; takes precedence over the environment variable.
        env_var: Name of the fallback environment variable.

    Returns:
        The resolved string value.

    Raises:
        ConfigurationError: If neither value nor the environment variable is set.
    """
    value = value or os.getenv(env_var)
    if value:
        return value

    raise ConfigurationError(
        f"{env_var} could not be resolved. Pass value=... or set env variable {env_var}."
    )
