Initial upload: EnvSchema v0.1.0 with CI/CD workflow
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
57
envschema/generator.py
Normal file
57
envschema/generator.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
"""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)
|
||||||
Reference in New Issue
Block a user