Skip to content

Commit aec339c

Browse files
committed
Initialize token in app callback
1 parent f109a3b commit aec339c

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/fastapi_cloud_cli/cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,40 @@
99
from .commands.setup_ci import setup_ci
1010
from .commands.unlink import unlink
1111
from .commands.whoami import whoami
12+
from .context import ctx
1213
from .logging import setup_logging
1314
from .utils.sentry import init_sentry
1415

1516
setup_logging()
1617

18+
COMMANDS_USE_TOKEN = {"deploy", "whoami"}
19+
1720
app = typer.Typer(rich_markup_mode="rich")
1821

1922
cloud_app = typer.Typer(
2023
rich_markup_mode="rich",
2124
help="Manage [bold]FastAPI[/bold] Cloud deployments. 🚀",
2225
)
2326

27+
28+
@cloud_app.callback(invoke_without_command=True)
29+
def cloud_callback(typer_ctx: typer.Context) -> None:
30+
if typer_ctx.invoked_subcommand in COMMANDS_USE_TOKEN:
31+
ctx.initialize(prefer_auth_mode="token")
32+
else:
33+
ctx.initialize()
34+
35+
36+
@app.callback(invoke_without_command=True)
37+
def app_callback(typer_ctx: typer.Context) -> None:
38+
if typer_ctx.invoked_subcommand == "cloud":
39+
return
40+
if typer_ctx.invoked_subcommand in COMMANDS_USE_TOKEN:
41+
ctx.initialize(prefer_auth_mode="token")
42+
else:
43+
ctx.initialize()
44+
45+
2446
# TODO: use the app structure
2547

2648
# Additional commands

src/fastapi_cloud_cli/commands/deploy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,6 @@ def deploy(
652652
"Deploy path: %s, skip_wait: %s, app_id: %s", path, skip_wait, provided_app_id
653653
)
654654

655-
ctx.initialize(prefer_auth_mode="token")
656655
identity = ctx.get_identity()
657656

658657
with get_rich_toolkit() as toolkit:

src/fastapi_cloud_cli/commands/whoami.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
def whoami() -> Any:
15-
ctx.initialize(prefer_auth_mode="token")
1615
identity = ctx.get_identity()
1716

1817
if identity.auth_mode == "token":

src/fastapi_cloud_cli/context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def initialize(self, prefer_auth_mode: Literal["token", "user"] = "user") -> Non
1717

1818
def get_identity(self) -> Identity:
1919
if not self._is_initialized:
20-
logger.debug("Context not initialized, initializing with default settings")
21-
self.initialize()
20+
raise RuntimeError("Context must be initialized before use")
2221
return Identity(prefer_auth_mode=self.prefer_auth_mode)
2322

2423

tests/test_api_client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from httpx import Response
88
from time_machine import TimeMachineFixture
99

10+
from fastapi_cloud_cli.context import ctx
1011
from fastapi_cloud_cli.utils.api import (
1112
STREAM_LOGS_MAX_RETRIES,
1213
APIClient,
@@ -34,6 +35,11 @@ def logs_route(respx_mock: respx.MockRouter, deployment_id: str) -> respx.Route:
3435
return respx_mock.get(f"/deployments/{deployment_id}/build-logs")
3536

3637

38+
@pytest.fixture(autouse=True)
39+
def init_context() -> None:
40+
ctx.initialize() # Initialize context with defaults
41+
42+
3743
def test_stream_build_logs_successful(
3844
logs_route: respx.Route,
3945
client: APIClient,

0 commit comments

Comments
 (0)