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

25 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 root callback for the `bijux config` command group. 

5 

6This module defines the default action for the `bijux config` command. When 

7invoked without a subcommand (like `get`, `set`, or `unset`), it lists all 

8key-value pairs currently stored in the active configuration, presenting them 

9in a structured, machine-readable format. 

10 

11Output Contract: 

12 * Success: `{"KEY_1": "VALUE_1", "KEY_2": "VALUE_2", ...}` 

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

14 

15Exit Codes: 

16 * `0`: Success. 

17 * `1`: An unexpected error occurred while accessing 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.precedence import current_execution_policy 

45from bijux_cli.services.config.contracts import ConfigProtocol 

46 

47 

48def config( 

49 ctx: typer.Context, 

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

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

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

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

54) -> None: 

55 """Defines the entrypoint for the `bijux config` command group. 

56 

57 This function serves as the default action when `bijux config` is run 

58 without a subcommand. It retrieves and displays all key-value pairs from 

59 the current configuration. If a subcommand (`get`, `set`, etc.) is 

60 invoked, this function yields control to it. 

61 

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. 

67 

68 Returns: 

69 None: 

70 """ 

71 if ctx.invoked_subcommand: 

72 return 

73 

74 command = "config" 

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 pretty = effective.pretty 

85 

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

87 

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

89 """Builds the payload containing all configuration values. 

90 

91 Args: 

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

93 

94 Returns: 

95 ConfigDumpPayload: A payload containing configuration entries 

96 and optional runtime metadata. 

97 """ 

98 data = config_svc.all() 

99 payload: dict[str, object] = dict(data) 

100 if include_runtime: 

101 payload.update( 

102 { 

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

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

105 } 

106 ) 

107 return payload 

108 

109 new_run_command( 

110 command_name=command, 

111 payload_builder=payload_builder, 

112 quiet=quiet, 

113 fmt=fmt_lower, 

114 pretty=pretty, 

115 log_level=log_level, 

116 )