|
6 | 6 |
|
7 | 7 | import contextlib
|
8 | 8 | from dataclasses import dataclass, field
|
| 9 | +from importlib.util import find_spec |
9 | 10 | from typing import TYPE_CHECKING, Any, Callable, Optional, cast
|
10 | 11 |
|
11 |
| -from click import echo |
12 | 12 | from sqlalchemy.exc import OperationalError
|
13 | 13 | from starlette.concurrency import run_in_threadpool
|
14 | 14 | from starlette.middleware.base import BaseHTTPMiddleware
|
|
30 | 30 | from starlette.responses import Response
|
31 | 31 |
|
32 | 32 |
|
| 33 | +FASTAPI_CLI_INSTALLED = bool(find_spec("fastapi_cli")) |
| 34 | + |
| 35 | + |
| 36 | +def _echo(message: str) -> None: # pragma: no cover |
| 37 | + """Echo a message using either rich toolkit or click echo.""" |
| 38 | + if FASTAPI_CLI_INSTALLED: |
| 39 | + from fastapi_cli.utils.cli import get_rich_toolkit |
| 40 | + |
| 41 | + with get_rich_toolkit() as toolkit: |
| 42 | + toolkit.print(message, tag="INFO") |
| 43 | + else: |
| 44 | + from click import echo |
| 45 | + |
| 46 | + echo(message) |
| 47 | + |
| 48 | + |
33 | 49 | def _make_unique_state_key(app: "Starlette", key: str) -> str: # pragma: no cover
|
34 | 50 | """Generates a unique state key for the Starlette application.
|
35 | 51 |
|
@@ -116,9 +132,9 @@ async def create_all_metadata(self) -> None: # pragma: no cover
|
116 | 132 | )
|
117 | 133 | await conn.commit()
|
118 | 134 | except OperationalError as exc:
|
119 |
| - echo(f" * Could not create target metadata. Reason: {exc}") |
| 135 | + _echo(f" * Could not create target metadata. Reason: {exc}") |
120 | 136 | else:
|
121 |
| - echo(" * Created target metadata.") |
| 137 | + _echo(" * Created target metadata.") |
122 | 138 |
|
123 | 139 | def init_app(self, app: "Starlette") -> None:
|
124 | 140 | """Initialize the Starlette application with this configuration.
|
@@ -259,7 +275,7 @@ async def create_all_metadata(self) -> None: # pragma: no cover
|
259 | 275 | metadata_registry.get(None if self.bind_key == "default" else self.bind_key).create_all, conn
|
260 | 276 | )
|
261 | 277 | except OperationalError as exc:
|
262 |
| - echo(f" * Could not create target metadata. Reason: {exc}") |
| 278 | + _echo(f" * Could not create target metadata. Reason: {exc}") |
263 | 279 |
|
264 | 280 | def init_app(self, app: "Starlette") -> None:
|
265 | 281 | """Initialize the Starlette application with this configuration.
|
|
0 commit comments