Init project
This commit is contained in:
53
birthday_pool_bot/cli.py
Normal file
53
birthday_pool_bot/cli.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import asyncio
|
||||
import pathlib
|
||||
|
||||
import typer
|
||||
|
||||
from .notifications import get_cli as get_notifications_cli
|
||||
from .repositories import RepositoriesContainer
|
||||
from .repositories.migrations import get_cli as get_migrations_cli
|
||||
from .service import BirthdayPoolBotService
|
||||
from .settings import Settings
|
||||
from .telegram_bot import get_cli as get_telegram_bot_cli
|
||||
|
||||
|
||||
def callback(
|
||||
ctx: typer.Context,
|
||||
env: pathlib.Path | None = typer.Option(
|
||||
None,
|
||||
"--env", "-e",
|
||||
help="`.env` config file location",
|
||||
),
|
||||
):
|
||||
ctx.obj = ctx.obj or {}
|
||||
|
||||
if (settings := ctx.obj.get("settings")) is None:
|
||||
if env is not None:
|
||||
ctx.obj["settings"] = Settings(_env_file=env)
|
||||
else:
|
||||
ctx.obj["settings"] = Settings()
|
||||
settings = ctx.obj["settings"]
|
||||
|
||||
ctx.obj["repositories_container"] = RepositoriesContainer(settings=settings.repository)
|
||||
|
||||
|
||||
def run(ctx: typer.Context):
|
||||
settings: Settings = ctx.obj["settings"]
|
||||
|
||||
birthday_pool_bot_service = BirthdayPoolBotService(
|
||||
settings=settings,
|
||||
)
|
||||
|
||||
asyncio.run(birthday_pool_bot_service.run())
|
||||
|
||||
|
||||
def get_cli() -> typer.Typer:
|
||||
cli = typer.Typer()
|
||||
|
||||
cli.callback()(callback)
|
||||
cli.add_typer(get_migrations_cli(), name="migrations")
|
||||
cli.add_typer(get_telegram_bot_cli(), name="telegram-bot")
|
||||
cli.add_typer(get_notifications_cli(), name="notifications")
|
||||
cli.command(name="run")(run)
|
||||
|
||||
return cli
|
||||
Reference in New Issue
Block a user