Coverage for /home/runner/work/bijux-cli/bijux-cli/src/bijux_cli/commands/memory/list.py: 100%
22 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"""Implements the `memory list` subcommand for the Bijux CLI.
6This module contains the logic for listing all keys currently held in the
7transient, in-memory data store. It retrieves the keys and presents them in a
8structured, machine-readable list format.
10Output Contract:
11 * Success: `{"status": "ok", "keys": list, "count": int}`
12 * Verbose: Adds `{"python": str, "platform": str}` to the payload.
13 * Error: `{"error": str, "code": int}`
15Exit Codes:
16 * `0`: Success.
17 * `1`: An unexpected error occurred (e.g., service unavailable, list failed).
18"""
20from __future__ import annotations
22from collections.abc import Mapping
23import platform
25import typer
27from bijux_cli.commands.memory.utils import resolve_memory_service
28from bijux_cli.commands.utilities import (
29 ascii_safe,
30 emit_error_and_exit,
31 new_run_command,
32 validate_common_flags,
33)
34from bijux_cli.core.constants import (
35 HELP_DEBUG,
36 HELP_FORMAT,
37 HELP_NO_PRETTY,
38 HELP_QUIET,
39 HELP_VERBOSE,
40)
43def _build_payload(include_runtime: bool, keys: list[str]) -> Mapping[str, object]:
44 """Builds the payload for the memory keys list response.
46 Args:
47 include_runtime (bool): If True, includes Python and platform info.
48 keys (list[str]): The list of keys from the memory store.
50 Returns:
51 Mapping[str, object]: A dictionary containing the status, a sorted list
52 of keys, the key count, and optional runtime metadata.
53 """
54 payload: dict[str, object] = {"status": "ok", "keys": keys, "count": len(keys)}
55 if include_runtime:
56 payload["python"] = ascii_safe(platform.python_version(), "python_version")
57 payload["platform"] = ascii_safe(platform.platform(), "platform")
58 return payload
61def list_memory(
62 quiet: bool = typer.Option(False, "-q", "--quiet", help=HELP_QUIET),
63 verbose: bool = typer.Option(False, "-v", "--verbose", help=HELP_VERBOSE),
64 fmt: str = typer.Option("json", "-f", "--format", help=HELP_FORMAT),
65 pretty: bool = typer.Option(True, "--pretty/--no-pretty", help=HELP_NO_PRETTY),
66 debug: bool = typer.Option(False, "-d", "--debug", help=HELP_DEBUG),
67) -> None:
68 """Lists all keys currently stored in the transient in-memory store.
70 This command retrieves all defined keys from the memory service, sorts them,
71 and then emits them in a structured payload.
73 Args:
74 quiet (bool): If True, suppresses all output except for errors.
75 verbose (bool): If True, includes Python/platform details in the output.
76 fmt (str): The output format, "json" or "yaml".
77 pretty (bool): If True, pretty-prints the output.
78 debug (bool): If True, enables debug diagnostics.
80 Returns:
81 None:
83 Raises:
84 SystemExit: Always exits with a contract-compliant status code and
85 payload, indicating success or detailing an error.
86 """
87 command = "memory list"
89 fmt_lower = validate_common_flags(fmt, command, quiet)
91 memory_svc = resolve_memory_service(command, fmt_lower, quiet, verbose, debug)
93 try:
94 keys = sorted(memory_svc.keys())
95 except Exception as exc:
96 emit_error_and_exit(
97 f"Failed to list memory keys: {exc}",
98 code=1,
99 failure="list_failed",
100 command=command,
101 fmt=fmt_lower,
102 quiet=quiet,
103 include_runtime=verbose,
104 debug=debug,
105 )
107 new_run_command(
108 command_name=command,
109 payload_builder=lambda include: _build_payload(include, keys),
110 quiet=quiet,
111 verbose=verbose,
112 fmt=fmt_lower,
113 pretty=pretty,
114 debug=debug,
115 )