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

30 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 list` subcommand for the Bijux CLI. 

5 

6This module contains the logic for listing all keys currently defined in the 

7active configuration store. It retrieves the keys and presents them in a 

8structured, machine-readable list format. 

9 

10Output Contract: 

11 * Success: `{"items": [{"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""" 

18 

19from __future__ import annotations 

20 

21import platform 

22 

23import typer 

24 

25from bijux_cli.cli.core.command import ( 

26 ascii_safe, 

27 new_run_command, 

28 validate_common_flags, 

29) 

30from bijux_cli.cli.core.constants import ( 

31 OPT_FORMAT, 

32 OPT_LOG_LEVEL, 

33 OPT_PRETTY, 

34 OPT_QUIET, 

35) 

36from bijux_cli.cli.core.help_text import ( 

37 HELP_FORMAT, 

38 HELP_LOG_LEVEL, 

39 HELP_NO_PRETTY, 

40 HELP_QUIET, 

41) 

42from bijux_cli.core.di import DIContainer 

43from bijux_cli.core.enums import ErrorType 

44from bijux_cli.core.exit_policy import ExitIntentError 

45from bijux_cli.core.precedence import current_execution_policy, resolve_exit_intent 

46from bijux_cli.services.config.contracts import ConfigProtocol 

47 

48 

49def list_config( 

50 ctx: typer.Context, 

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

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

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

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

55) -> None: 

56 """Lists all configuration keys from the active configuration store. 

57 

58 This function retrieves all defined keys, sorts them, and then uses the 

59 `new_run_command` helper to emit them in a structured payload. 

60 

61 Args: 

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

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

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

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

66 

67 Returns: 

68 None: 

69 

70 Raises: 

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

72 payload, indicating success or detailing the error. 

73 """ 

74 command = "config list" 

75 effective = current_execution_policy() 

76 fmt_lower = validate_common_flags( 

77 fmt, 

78 command, 

79 effective.quiet, 

80 include_runtime=effective.include_runtime, 

81 log_level=effective.log_level, 

82 ) 

83 quiet = effective.quiet 

84 include_runtime = effective.include_runtime 

85 pretty = effective.pretty 

86 

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

88 

89 try: 

90 keys = config_svc.list_keys() 

91 except Exception as exc: 

92 intent = resolve_exit_intent( 

93 message=f"Failed to list config: {exc}", 

94 code=1, 

95 failure="list_failed", 

96 command=command, 

97 fmt=fmt_lower, 

98 quiet=quiet, 

99 include_runtime=include_runtime, 

100 error_type=ErrorType.INTERNAL, 

101 log_level=effective.log_level, 

102 ) 

103 raise ExitIntentError(intent) from exc 

104 

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

106 """Builds a payload containing the list of configuration keys. 

107 

108 Args: 

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

110 

111 Returns: 

112 ConfigListPayload: A payload containing a sorted list of keys 

113 under an "items" field, plus optional runtime metadata. 

114 """ 

115 payload: dict[str, object] = { 

116 "items": [{"key": k} for k in sorted(keys, key=str.lower)] 

117 } 

118 if include_runtime: 

119 payload.update( 

120 { 

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

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

123 } 

124 ) 

125 return payload 

126 

127 new_run_command( 

128 command_name=command, 

129 payload_builder=payload_builder, 

130 quiet=quiet, 

131 fmt=fmt_lower, 

132 pretty=pretty, 

133 log_level=log_level, 

134 )