Coverage for /home/runner/work/bijux-cli/bijux-cli/src/bijux_cli/commands/memory/utils.py: 100%
9 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-19 23:36 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-19 23:36 +0000
1# SPDX-License-Identifier: MIT
2# Copyright © 2025 Bijan Mousavi
4"""Provides shared utilities for the `bijux memory` command group.
6This module centralizes common logic used by the memory-related subcommands.
7Its primary responsibility is to provide a consistent way to resolve the
8`MemoryProtocol` service from the Dependency Injection (DI) container,
9including standardized error handling for cases where the service is
10unavailable.
11"""
13from __future__ import annotations
15from bijux_cli.commands.utilities import emit_error_and_exit
16from bijux_cli.contracts import MemoryProtocol
17from bijux_cli.core.di import DIContainer
20def resolve_memory_service(
21 command: str,
22 fmt_lower: str,
23 quiet: bool,
24 include_runtime: bool,
25 debug: bool,
26) -> MemoryProtocol:
27 """Resolves the MemoryProtocol implementation from the DI container.
29 Args:
30 command (str): The full command name (e.g., "memory list").
31 fmt_lower (str): The chosen output format, lowercased.
32 quiet (bool): If True, suppresses non-error output.
33 include_runtime (bool): If True, includes runtime metadata in errors.
34 debug (bool): If True, enables debug diagnostics.
36 Returns:
37 MemoryProtocol: An instance of the memory service.
39 Raises:
40 SystemExit: Exits with a structured error if the service cannot be
41 resolved from the container.
42 """
43 try:
44 return DIContainer.current().resolve(MemoryProtocol)
45 except Exception as exc:
46 emit_error_and_exit(
47 f"Memory service unavailable: {exc}",
48 code=1,
49 failure="service_unavailable",
50 command=command,
51 fmt=fmt_lower,
52 quiet=quiet,
53 include_runtime=include_runtime,
54 )