slab-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
slab_cli/context.py ADDED
@@ -0,0 +1,50 @@
1
+ """Shared runtime context for CLI commands.
2
+
3
+ Created once per invocation in main(). Commands receive it instead of
4
+ calling _client()/_require_collector() themselves."""
5
+
6
+ from __future__ import annotations
7
+
8
+ import sys
9
+
10
+ from .client import SlabClient
11
+ from .config import get_api_key, get_base_url, get_collector_uuid
12
+ from .display import console
13
+
14
+
15
+ class MissingCollector(Exception):
16
+ """Raised when a command needs a collector but SLAB_COLLECTOR is not set."""
17
+
18
+
19
+ class CliContext:
20
+ """Holds the API client and resolved collector UUID for a CLI session."""
21
+
22
+ def __init__(self) -> None:
23
+ self._client: SlabClient | None = None
24
+ self._collector: str | None = None
25
+
26
+ @property
27
+ def client(self) -> SlabClient:
28
+ if self._client is None:
29
+ self._client = SlabClient(get_base_url(), get_api_key())
30
+ return self._client
31
+
32
+ @property
33
+ def collector(self) -> str:
34
+ """The active collector UUID. Resolution order: SLAB_COLLECTOR env var, then the account's
35
+ default collector (so a solo user only needs their API key). Raises MissingCollector if
36
+ neither yields one. Cached per invocation."""
37
+ if self._collector:
38
+ return self._collector
39
+ uuid = get_collector_uuid()
40
+ if not uuid:
41
+ # Solo zero-config: fall back to the calling account's default collector.
42
+ uuid = self.client.get_account_context().default_collector_uuid
43
+ if not uuid:
44
+ raise MissingCollector
45
+ self._collector = uuid
46
+ return uuid
47
+
48
+
49
+ # Module-level singleton — set by main() before commands run.
50
+ ctx = CliContext()