Add env_pro command modules
This commit is contained in:
62
app/env_pro/commands/validate_cmd.py
Normal file
62
app/env_pro/commands/validate_cmd.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
"""Validation commands for env-pro."""
|
||||||
|
|
||||||
|
import click
|
||||||
|
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.option("--schema", "-s", help="Path to schema file")
|
||||||
|
@click.option("--verbose/--quiet", default=False)
|
||||||
|
def validate(schema: str, verbose: bool):
|
||||||
|
"""Validate environment variables against schema."""
|
||||||
|
from pathlib import Path
|
||||||
|
from env_pro.core.validator import Validator
|
||||||
|
from env_pro.core.profile import get_active_profile, get_profile_vars
|
||||||
|
|
||||||
|
schema_path = Path(schema) if schema else Path.cwd() / ".env.schema.yaml"
|
||||||
|
|
||||||
|
if not schema_path.exists():
|
||||||
|
if verbose:
|
||||||
|
click.echo("No schema file found. Create .env.schema.yaml to enable validation.")
|
||||||
|
else:
|
||||||
|
click.echo("ok")
|
||||||
|
return
|
||||||
|
|
||||||
|
validator = Validator.from_file(schema_path)
|
||||||
|
profile = get_active_profile() or "default"
|
||||||
|
vars = get_profile_vars(profile)
|
||||||
|
|
||||||
|
errors = validator.validate(vars)
|
||||||
|
|
||||||
|
if errors:
|
||||||
|
click.echo(f"Validation failed with {len(errors)} error(s):")
|
||||||
|
for error in errors:
|
||||||
|
click.echo(f" {error}")
|
||||||
|
raise click.ClickException("Validation failed")
|
||||||
|
else:
|
||||||
|
click.echo("Validation passed")
|
||||||
|
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
def check():
|
||||||
|
"""Check for required variables."""
|
||||||
|
from env_pro.core.profile import get_active_profile, get_profile_vars
|
||||||
|
from env_pro.core.validator import Validator
|
||||||
|
|
||||||
|
profile = get_active_profile() or "default"
|
||||||
|
vars = get_profile_vars(profile)
|
||||||
|
|
||||||
|
validator = Validator.from_file()
|
||||||
|
|
||||||
|
if validator.schema is None:
|
||||||
|
click.echo("No schema defined")
|
||||||
|
return
|
||||||
|
|
||||||
|
errors = validator.validate(vars)
|
||||||
|
required_errors = [e for e in errors if "missing" in e.message.lower()]
|
||||||
|
|
||||||
|
if required_errors:
|
||||||
|
click.echo(f"Missing required variables:")
|
||||||
|
for e in required_errors:
|
||||||
|
click.echo(f" {e.key}")
|
||||||
|
else:
|
||||||
|
click.echo("All required variables are set")
|
||||||
Reference in New Issue
Block a user