feat: add create-invite CLI command
This commit is contained in:
parent
0c3157ea3a
commit
bcddf5d1c8
2 changed files with 94 additions and 0 deletions
41
src/porchlight/cli.py
Normal file
41
src/porchlight/cli.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import asyncio
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Optional
|
||||
|
||||
import typer
|
||||
|
||||
from porchlight.config import Settings
|
||||
from porchlight.invite.service import MagicLinkService
|
||||
from porchlight.store.sqlite.db import open_db
|
||||
from porchlight.store.sqlite.repositories import SQLiteMagicLinkRepository
|
||||
|
||||
PACKAGE_DIR = Path(__file__).resolve().parent
|
||||
MIGRATIONS_DIR = PACKAGE_DIR / "store" / "sqlite" / "migrations"
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
|
||||
async def _create_invite(settings: Settings, username: str, ttl: int, note: str | None) -> str:
|
||||
"""Create an invite link and return the full registration URL."""
|
||||
async with open_db(settings.sqlite_path, MIGRATIONS_DIR) as db:
|
||||
repo = SQLiteMagicLinkRepository(db)
|
||||
service = MagicLinkService(repo, ttl=ttl)
|
||||
link = await service.create(username=username, created_by="cli", note=note)
|
||||
return f"{settings.issuer}/register/{link.token}"
|
||||
|
||||
|
||||
@app.command()
|
||||
def create_invite(
|
||||
username: str,
|
||||
ttl: Annotated[Optional[int], typer.Option(help="Link expiration in seconds")] = None,
|
||||
note: Annotated[Optional[str], typer.Option(help="Optional note stored with the link")] = None,
|
||||
) -> None:
|
||||
"""Generate a magic link registration URL for a new user."""
|
||||
settings = Settings()
|
||||
effective_ttl = ttl if ttl is not None else settings.invite_ttl
|
||||
url = asyncio.run(_create_invite(settings, username, effective_ttl, note))
|
||||
typer.echo(url)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
app()
|
||||
Loading…
Add table
Add a link
Reference in a new issue