117 lines
2.6 KiB
Python
117 lines
2.6 KiB
Python
import os
|
|
import sys
|
|
import subprocess
|
|
|
|
try:
|
|
import typer
|
|
except ImportError:
|
|
print("Typer is not installed. Please install it using: pip install typer")
|
|
sys.exit(1)
|
|
|
|
app = typer.Typer(help="DocQube Management CLI")
|
|
|
|
|
|
def run_command(args: list[str], env_name: str):
|
|
"""Helper function to run subprocess commands with isolated environment variables."""
|
|
if not args:
|
|
return
|
|
|
|
# Use current virtual environment's python executable
|
|
if args[0] == "python":
|
|
args[0] = sys.executable
|
|
|
|
# Copy current OS environment and inject the passed env_name
|
|
env = os.environ.copy()
|
|
|
|
# Map 'local' to 'localdev' to avoid Vite .env naming conflicts
|
|
if env_name == "local":
|
|
env_name = "localdev"
|
|
|
|
env["APP_ENV"] = env_name
|
|
|
|
try:
|
|
subprocess.run(args, env=env, check=True)
|
|
except subprocess.CalledProcessError as e:
|
|
sys.exit(e.returncode)
|
|
except KeyboardInterrupt:
|
|
# Exit silently on Ctrl+C
|
|
sys.exit(0)
|
|
|
|
|
|
@app.command()
|
|
def run(
|
|
env: str = typer.Option(
|
|
None,
|
|
"--env",
|
|
"-e",
|
|
help="Target environment to run the server in",
|
|
)
|
|
):
|
|
"""
|
|
Start the FastAPI application server.
|
|
"""
|
|
|
|
actual_env = env or os.getenv("APP_ENV", "local")
|
|
|
|
if actual_env == "local":
|
|
actual_env = "localdev"
|
|
|
|
typer.echo(f"Starting server in {actual_env.upper()} mode...")
|
|
|
|
os.environ["APP_ENV"] = actual_env
|
|
|
|
from app.core.settings import settings
|
|
|
|
args = [
|
|
sys.executable,
|
|
"-m",
|
|
"uvicorn",
|
|
"app.main:app",
|
|
"--host",
|
|
settings.HOST,
|
|
"--port",
|
|
str(settings.PORT),
|
|
]
|
|
|
|
if actual_env in ["local", "development", "localdev"]:
|
|
args.append("--reload")
|
|
|
|
run_command(args, actual_env)
|
|
|
|
|
|
|
|
@app.command()
|
|
def migrate(
|
|
env: str = typer.Option(
|
|
"local", "--env", "-e", help="Target environment for migrations"
|
|
)
|
|
):
|
|
"""
|
|
Run Alembic database migrations.
|
|
"""
|
|
actual_env = "localdev" if env == "local" else env
|
|
typer.echo(f"[*] Running migrations in {actual_env.upper()} mode...")
|
|
|
|
args = [sys.executable, "-m", "alembic", "upgrade", "head"]
|
|
run_command(args, actual_env)
|
|
|
|
|
|
@app.command()
|
|
def seed(
|
|
env: str = typer.Option(
|
|
"local", "--env", "-e", help="Target environment for seeding"
|
|
)
|
|
):
|
|
"""
|
|
Run the database seed script.
|
|
"""
|
|
actual_env = "localdev" if env == "local" else env
|
|
typer.echo(f"[*] Seeding database in {actual_env.upper()} mode...")
|
|
|
|
args = [sys.executable, os.path.join("scripts", "seed.py")]
|
|
run_command(args, actual_env)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app()
|