Coverage for  / home / runner / work / bijux-cli / bijux-cli / src / bijux_cli / cli / commands / history / clear.py: 100%

35 statements  

« 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 

3 

4"""Implements the `history clear` subcommand for the Bijux CLI. 

5 

6This module contains the logic for permanently erasing all entries from the 

7command history store. This action is irreversible. A structured confirmation 

8is emitted upon success. 

9 

10Output Contract: 

11 * Success: `{"status": "cleared"}` 

12 * Error: `{"error": str, "code": int}` 

13 

14Exit Codes: 

15 * `0`: Success. 

16 * `1`: An unexpected error occurred, such as the history service being 

17 unavailable or a failure during the clear operation. 

18""" 

19 

20from __future__ import annotations 

21 

22import platform 

23 

24import typer 

25 

26from bijux_cli.cli.commands.payloads import HistoryClearPayload 

27from bijux_cli.cli.core.command import ( 

28 ascii_safe, 

29 new_run_command, 

30 raise_exit_intent, 

31 validate_common_flags, 

32) 

33from bijux_cli.cli.core.constants import ( 

34 OPT_FORMAT, 

35 OPT_LOG_LEVEL, 

36 OPT_PRETTY, 

37 OPT_QUIET, 

38) 

39from bijux_cli.cli.core.help_text import ( 

40 HELP_FORMAT, 

41 HELP_LOG_LEVEL, 

42 HELP_NO_PRETTY, 

43 HELP_QUIET, 

44) 

45from bijux_cli.core.di import DIContainer 

46from bijux_cli.core.enums import ErrorType, LogLevel, OutputFormat 

47from bijux_cli.core.precedence import current_execution_policy 

48from bijux_cli.services.history.contracts import HistoryProtocol 

49 

50 

51def resolve_history_service( 

52 command: str, 

53 fmt_lower: OutputFormat, 

54 quiet: bool, 

55 include_runtime: bool, 

56 log_level: LogLevel, 

57) -> HistoryProtocol: 

58 """Resolves the HistoryProtocol implementation from the DI container. 

59 

60 Args: 

61 command (str): The full command name (e.g., "history clear"). 

62 fmt_lower (OutputFormat): The chosen output format. 

63 quiet (bool): If True, suppresses non-error output. 

64 include_runtime (bool): If True, includes runtime metadata in errors. 

65 log_level (LogLevel): Logging level for diagnostics. 

66 

67 Returns: 

68 HistoryProtocol: An instance of the history service. 

69 

70 Raises: 

71 SystemExit: Exits with a structured error if the service cannot be 

72 resolved from the container. 

73 """ 

74 try: 

75 return DIContainer.current().resolve(HistoryProtocol) 

76 except Exception as exc: 

77 raise_exit_intent( 

78 f"History service unavailable: {exc}", 

79 code=1, 

80 failure="service_unavailable", 

81 command=command, 

82 fmt=fmt_lower, 

83 quiet=quiet, 

84 include_runtime=include_runtime, 

85 log_level=log_level, 

86 ) 

87 

88 

89def clear_history( 

90 quiet: bool = typer.Option(False, *OPT_QUIET, help=HELP_QUIET), 

91 fmt: str = typer.Option("json", *OPT_FORMAT, help=HELP_FORMAT), 

92 pretty: bool = typer.Option(True, OPT_PRETTY, help=HELP_NO_PRETTY), 

93 log_level: str = typer.Option("info", *OPT_LOG_LEVEL, help=HELP_LOG_LEVEL), 

94) -> None: 

95 """Erases all stored command history. 

96 

97 This command permanently removes all entries from the history store and 

98 emits a structured payload to confirm the operation. 

99 

100 Args: 

101 quiet (bool): If True, suppresses all output except for errors. 

102 fmt (str): The output format, "json" or "yaml". 

103 pretty (bool): If True, pretty-prints the output. 

104 log_level (str): Logging level for diagnostics. 

105 

106 Returns: 

107 None: 

108 

109 Raises: 

110 SystemExit: Always exits with a contract-compliant status code and 

111 payload, indicating success or detailing an error. 

112 """ 

113 command = "history clear" 

114 policy = current_execution_policy() 

115 quiet = policy.quiet 

116 include_runtime = policy.include_runtime 

117 log_level_value = policy.log_level 

118 pretty = policy.pretty 

119 fmt_lower = validate_common_flags( 

120 fmt, 

121 command, 

122 quiet, 

123 include_runtime=include_runtime, 

124 log_level=log_level_value, 

125 ) 

126 history_svc = resolve_history_service( 

127 command, fmt_lower, quiet, include_runtime, log_level_value 

128 ) 

129 

130 try: 

131 history_svc.clear() 

132 except Exception as exc: 

133 raise_exit_intent( 

134 f"Failed to clear history: {exc}", 

135 code=1, 

136 failure="clear_failed", 

137 error_type=ErrorType.INTERNAL, 

138 command=command, 

139 fmt=fmt_lower, 

140 quiet=quiet, 

141 include_runtime=include_runtime, 

142 log_level=log_level_value, 

143 ) 

144 

145 def payload_builder(include_runtime: bool) -> HistoryClearPayload: 

146 """Builds the payload confirming the history was cleared. 

147 

148 Args: 

149 include_runtime (bool): If True, includes Python and platform info. 

150 

151 Returns: 

152 HistoryClearPayload: The structured payload. 

153 """ 

154 payload = HistoryClearPayload(status="cleared") 

155 if include_runtime: 

156 return HistoryClearPayload( 

157 status=payload.status, 

158 python=ascii_safe(platform.python_version(), "python_version"), 

159 platform=ascii_safe(platform.platform(), "platform"), 

160 ) 

161 return payload 

162 

163 new_run_command( 

164 command_name=command, 

165 payload_builder=payload_builder, 

166 quiet=quiet, 

167 fmt=fmt_lower, 

168 pretty=pretty, 

169 log_level=log_level, 

170 )