58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
"""Generator for .env.example files from schema definitions."""
|
|
|
|
from envschema.schema import Schema
|
|
|
|
|
|
def generate_env_example(schema: Schema, include_descriptions: bool = True) -> str:
|
|
"""Generate an .env.example file content from a schema.
|
|
|
|
Args:
|
|
schema: The schema to generate from.
|
|
include_descriptions: Whether to include description comments.
|
|
|
|
Returns:
|
|
Formatted .env.example content.
|
|
"""
|
|
lines = []
|
|
|
|
lines.append("# Environment Variables Schema")
|
|
if schema.version:
|
|
lines.append(f"# Version: {schema.version}")
|
|
lines.append("")
|
|
|
|
for var in schema.envvars:
|
|
if include_descriptions and var.description:
|
|
lines.append(f"# {var.description}")
|
|
|
|
if var.required:
|
|
lines.append(f"# REQUIRED")
|
|
elif var.default is not None:
|
|
lines.append(f"# Default: {var.default}")
|
|
|
|
default_part = f"# {var.default}" if var.default else ""
|
|
type_part = f"[{var.type.value}]"
|
|
|
|
if var.required:
|
|
lines.append(f"{var.name}=")
|
|
elif var.default is not None:
|
|
lines.append(f"{var.name}={var.default}")
|
|
else:
|
|
lines.append(f"{var.name}=")
|
|
|
|
lines.append("")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
def generate_env_example_to_file(schema: Schema, output_path: str, include_descriptions: bool = True) -> None:
|
|
"""Generate and write an .env.example file.
|
|
|
|
Args:
|
|
schema: The schema to generate from.
|
|
output_path: Path to write the .env.example file.
|
|
include_descriptions: Whether to include description comments.
|
|
"""
|
|
content = generate_env_example(schema, include_descriptions)
|
|
with open(output_path, "w") as f:
|
|
f.write(content)
|