import os import subprocess import sys import typer from typing import Optional app = typer.Typer(help="Management script for SaaS Backend") seed_app = typer.Typer(help="Seeding commands") app.add_typer(seed_app, name="seed") def run_command(args: list[str], env_name: str): """ Helper to run commands with a specific APP_ENV. Replaces "python" with sys.executable to ensure the same interpreter is used. """ env = os.environ.copy() env["APP_ENV"] = env_name if args[0] == "python": args[0] = sys.executable try: subprocess.run(args, env=env, check=True) except subprocess.CalledProcessError as e: sys.exit(e.returncode) except KeyboardInterrupt: sys.exit(0) @app.command() def run( env: str = typer.Option("local", "--env", "-e", help="Environment to run in (local, development, production, testing)") ): """Start the FastAPI server.""" run_command(["python", "run.py"], env) @app.command() def migrate( env: str = typer.Option("local", "--env", "-e", help="Environment to run in (local, development, production, testing)") ): """Run Alembic migrations (upgrade head).""" run_command(["python", "-m", "alembic", "upgrade", "head"], env) @seed_app.command("palettes") def seed_palettes( env: str = typer.Option("local", "--env", "-e", help="Environment to run in (local, development, production, testing)") ): """Seed palettes database.""" run_command(["python", "scripts/seed_palettes.py"], env) @seed_app.command("superadmin") def seed_superadmin( env: str = typer.Option("local", "--env", "-e", help="Environment to run in (local, development, production, testing)") ): """Seed superadmin user.""" run_command(["python", "scripts/seed_superadmin.py"], env) if __name__ == "__main__": app()