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

29 statements  

« 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 

3 

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

5 

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. 

10 

11Output Contract: 

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

13 * Verbose: Adds `{"python": str, "platform": str}` to the payload. 

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

15 

16Exit Codes: 

17 * `0`: Success. 

18 * `1`: An unexpected error occurred while clearing the configuration. 

19""" 

20 

21from __future__ import annotations 

22 

23import platform 

24 

25import typer 

26 

27from bijux_cli.commands.utilities import ( 

28 ascii_safe, 

29 emit_error_and_exit, 

30 new_run_command, 

31 parse_global_flags, 

32) 

33from bijux_cli.contracts import ConfigProtocol 

34from bijux_cli.core.constants import ( 

35 HELP_DEBUG, 

36 HELP_FORMAT, 

37 HELP_NO_PRETTY, 

38 HELP_QUIET, 

39 HELP_VERBOSE, 

40) 

41from bijux_cli.core.di import DIContainer 

42 

43 

44def clear_config( 

45 ctx: typer.Context, 

46 quiet: bool = typer.Option(False, "-q", "--quiet", help=HELP_QUIET), 

47 verbose: bool = typer.Option(False, "-v", "--verbose", help=HELP_VERBOSE), 

48 fmt: str = typer.Option("json", "-f", "--format", help=HELP_FORMAT), 

49 pretty: bool = typer.Option(True, "--pretty/--no-pretty", help=HELP_NO_PRETTY), 

50 debug: bool = typer.Option(False, "-d", "--debug", help=HELP_DEBUG), 

51) -> None: 

52 """Clears all configuration settings from the active store. 

53 

54 This command erases all key-value pairs, effectively resetting the 

55 configuration. It emits a structured payload to confirm the operation. 

56 

57 Args: 

58 ctx (typer.Context): The Typer context for the CLI. 

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

60 verbose (bool): If True, includes Python/platform details in the output. 

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

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

63 debug (bool): If True, enables debug diagnostics. 

64 

65 Returns: 

66 None: 

67 

68 Raises: 

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

70 payload, indicating success or detailing the error. 

71 """ 

72 flags = parse_global_flags() 

73 

74 quiet = flags["quiet"] 

75 verbose = flags["verbose"] 

76 fmt = flags["format"] 

77 pretty = flags["pretty"] 

78 debug = flags["debug"] 

79 

80 include_runtime = verbose 

81 fmt_lower = fmt.lower() 

82 

83 command = "config clear" 

84 

85 config_svc = DIContainer.current().resolve(ConfigProtocol) 

86 

87 try: 

88 config_svc.clear() 

89 except Exception as exc: 

90 emit_error_and_exit( 

91 f"Failed to clear config: {exc}", 

92 code=1, 

93 failure="clear_failed", 

94 command=command, 

95 fmt=fmt_lower, 

96 quiet=quiet, 

97 include_runtime=include_runtime, 

98 debug=debug, 

99 ) 

100 

101 def payload_builder(include_runtime: bool) -> dict[str, object]: 

102 """Builds the payload confirming a successful configuration clear. 

103 

104 Args: 

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

106 

107 Returns: 

108 dict[str, object]: The structured payload. 

109 """ 

110 payload: dict[str, object] = {"status": "cleared"} 

111 if include_runtime: 

112 payload["python"] = ascii_safe(platform.python_version(), "python_version") 

113 payload["platform"] = ascii_safe(platform.platform(), "platform") 

114 return payload 

115 

116 new_run_command( 

117 command_name=command, 

118 payload_builder=payload_builder, 

119 quiet=quiet, 

120 verbose=verbose, 

121 fmt=fmt_lower, 

122 pretty=pretty, 

123 debug=debug, 

124 )