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

33 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 `config unset` subcommand for the Bijux CLI. 

5 

6This module contains the logic for removing a key-value pair from the active 

7configuration store. It provides a structured, machine-readable response to 

8confirm the deletion or report an error, such as if the key does not exist. 

9 

10Output Contract: 

11 * Success: `{"status": "deleted", "key": str}` 

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

13 

14Exit Codes: 

15 * `0`: Success. 

16 * `1`: An unexpected error occurred while accessing the configuration. 

17 * `2`: The specified key was not found in the configuration. 

18""" 

19 

20from __future__ import annotations 

21 

22import platform 

23 

24import typer 

25 

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 

48 

49 

50def unset_config( 

51 ctx: typer.Context, 

52 key: str = typer.Argument(..., help="Key to remove"), 

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

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

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

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

57) -> None: 

58 """Removes a key from the active configuration store. 

59 

60 This function orchestrates the `unset` operation. It manually parses global 

61 flags, resolves the configuration service, attempts to remove the specified 

62 key, and then uses the `new_run_command` helper to emit a structured 

63 payload confirming the action. 

64 

65 Args: 

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

67 key (str): The configuration key to remove. 

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

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

70 pretty (bool): If True, pretty-prints the output. log_level (str): Logging level for diagnostics. 

71 

72 Returns: 

73 None: 

74 

75 Raises: 

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

77 payload, indicating success or detailing the error. 

78 """ 

79 command = "config unset" 

80 effective = current_execution_policy() 

81 fmt_lower = validate_common_flags( 

82 fmt, 

83 command, 

84 effective.quiet, 

85 include_runtime=effective.include_runtime, 

86 log_level=effective.log_level, 

87 ) 

88 quiet = effective.quiet 

89 include_runtime = effective.include_runtime 

90 pretty = effective.pretty 

91 

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

93 

94 try: 

95 config_svc.unset(key) 

96 except KeyError: 

97 intent = resolve_exit_intent( 

98 message=f"Config key not found: {key}", 

99 code=2, 

100 failure="not_found", 

101 command=command, 

102 fmt=fmt_lower, 

103 quiet=quiet, 

104 include_runtime=include_runtime, 

105 error_type=ErrorType.USER_INPUT, 

106 log_level=effective.log_level, 

107 extra={"key": key}, 

108 ) 

109 raise ExitIntentError(intent) from None 

110 except Exception as exc: 

111 intent = resolve_exit_intent( 

112 message=f"Failed to unset config: {exc}", 

113 code=1, 

114 failure="unset_failed", 

115 command=command, 

116 fmt=fmt_lower, 

117 quiet=quiet, 

118 include_runtime=include_runtime, 

119 error_type=ErrorType.INTERNAL, 

120 log_level=effective.log_level, 

121 ) 

122 raise ExitIntentError(intent) from exc 

123 

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

125 """Builds the payload confirming a key was deleted. 

126 

127 Args: 

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

129 

130 Returns: 

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

132 """ 

133 payload: dict[str, object] = {"status": "deleted", "key": key} 

134 if include_runtime: 

135 payload.update( 

136 { 

137 "python": ascii_safe(platform.python_version(), "python_version"), 

138 "platform": ascii_safe(platform.platform(), "platform"), 

139 } 

140 ) 

141 return payload 

142 

143 new_run_command( 

144 command_name=command, 

145 payload_builder=payload_builder, 

146 quiet=quiet, 

147 fmt=fmt_lower, 

148 pretty=pretty, 

149 log_level=log_level, 

150 )