Coverage for / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / commands / config / clear.py: 100%
30 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-01-26 17:59 +0000
« prev ^ index » next coverage.py v7.13.2, created at 2026-01-26 17:59 +0000
1# SPDX-License-Identifier: Apache-2.0
2# Copyright © 2025 Bijan Mousavi
4"""Implements the `config clear` subcommand for the Bijux CLI.
6This module contains the logic for completely erasing all key-value pairs from
7the active configuration store. This action is irreversible and effectively
8resets the configuration to an empty state. A structured confirmation is
9emitted upon success.
11Output Contract:
12 * Success: `{"status": "cleared"}`
13 * Error: `{"error": str, "code": int}`
15Exit Codes:
16 * `0`: Success.
17 * `1`: An unexpected error occurred while clearing the configuration.
18"""
20from __future__ import annotations
22import platform
24import typer
26from bijux_cli.cli.core.command import (
27 ascii_safe,
28 new_run_command,
29 validate_common_flags,
30)
31from bijux_cli.cli.core.constants import (
32 OPT_FORMAT,
33 OPT_LOG_LEVEL,
34 OPT_PRETTY,
35 OPT_QUIET,
36)
37from bijux_cli.cli.core.help_text import (
38 HELP_FORMAT,
39 HELP_LOG_LEVEL,
40 HELP_NO_PRETTY,
41 HELP_QUIET,
42)
43from bijux_cli.core.di import DIContainer
44from bijux_cli.core.enums import ErrorType
45from bijux_cli.core.exit_policy import ExitIntentError
46from bijux_cli.core.precedence import current_execution_policy, resolve_exit_intent
47from bijux_cli.services.config.contracts import ConfigProtocol
50def clear_config(
51 ctx: typer.Context,
52 quiet: bool = typer.Option(False, *OPT_QUIET, help=HELP_QUIET),
53 fmt: str = typer.Option("json", *OPT_FORMAT, help=HELP_FORMAT),
54 pretty: bool = typer.Option(True, OPT_PRETTY, help=HELP_NO_PRETTY),
55 log_level: str = typer.Option("info", *OPT_LOG_LEVEL, help=HELP_LOG_LEVEL),
56) -> None:
57 """Clears all configuration settings from the active store.
59 This command erases all key-value pairs, effectively resetting the
60 configuration. It emits a structured payload to confirm the operation.
62 Args:
63 ctx (typer.Context): The Typer context for the CLI.
64 quiet (bool): If True, suppresses all output except for errors.
65 fmt (str): The output format, "json" or "yaml".
66 pretty (bool): If True, pretty-prints the output. log_level (str): Logging level for diagnostics.
68 Returns:
69 None:
71 Raises:
72 SystemExit: Always exits with a contract-compliant status code and
73 payload, indicating success or detailing the error.
74 """
75 command = "config clear"
76 effective = current_execution_policy()
77 fmt_lower = validate_common_flags(
78 fmt,
79 command,
80 effective.quiet,
81 include_runtime=effective.include_runtime,
82 log_level=effective.log_level,
83 )
84 quiet = effective.quiet
85 include_runtime = effective.include_runtime
86 pretty = effective.pretty
88 config_svc = DIContainer.current().resolve(ConfigProtocol)
90 try:
91 config_svc.clear()
92 except Exception as exc:
93 intent = resolve_exit_intent(
94 message=f"Failed to clear config: {exc}",
95 code=1,
96 failure="clear_failed",
97 command=command,
98 fmt=fmt_lower,
99 quiet=quiet,
100 include_runtime=include_runtime,
101 error_type=ErrorType.INTERNAL,
102 log_level=effective.log_level,
103 )
104 raise ExitIntentError(intent) from exc
106 def payload_builder(include_runtime: bool) -> dict[str, object]:
107 """Builds the payload confirming a successful configuration clear.
109 Args:
110 include_runtime (bool): If True, includes Python and platform info.
112 Returns:
113 dict[str, object]: The structured payload.
114 """
115 payload: dict[str, object] = {"status": "cleared"}
116 if include_runtime:
117 payload.update(
118 {
119 "python": ascii_safe(platform.python_version(), "python_version"),
120 "platform": ascii_safe(platform.platform(), "platform"),
121 }
122 )
123 return payload
125 new_run_command(
126 command_name=command,
127 payload_builder=payload_builder,
128 quiet=quiet,
129 fmt=fmt_lower,
130 pretty=pretty,
131 log_level=log_level,
132 )