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

27 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 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 * 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 accessing the configuration. 

19""" 

20 

21from __future__ import annotations 

22 

23import platform 

24 

25import typer 

26 

27from bijux_cli.commands.utilities import ascii_safe, new_run_command, parse_global_flags 

28from bijux_cli.contracts import ConfigProtocol 

29from bijux_cli.core.constants import ( 

30 HELP_DEBUG, 

31 HELP_FORMAT, 

32 HELP_NO_PRETTY, 

33 HELP_QUIET, 

34 HELP_VERBOSE, 

35) 

36from bijux_cli.core.di import DIContainer 

37 

38 

39def config( 

40 ctx: typer.Context, 

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

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

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

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

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

46) -> None: 

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

48 

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

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

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

52 invoked, this function yields control to it. 

53 

54 Args: 

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

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

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

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

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

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

61 

62 Returns: 

63 None: 

64 """ 

65 if ctx.invoked_subcommand: 

66 return 

67 

68 flags = parse_global_flags() 

69 

70 quiet = flags["quiet"] 

71 verbose = flags["verbose"] 

72 fmt = flags["format"] 

73 pretty = flags["pretty"] 

74 debug = flags["debug"] 

75 

76 fmt_lower = fmt.lower() 

77 

78 command = "config" 

79 

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

81 

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

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

84 

85 Args: 

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

87 

88 Returns: 

89 dict[str, object]: A dictionary of all configuration key-value 

90 pairs and optional runtime metadata. 

91 """ 

92 data = config_svc.all() 

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

94 if include_runtime: 

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

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

97 return payload 

98 

99 new_run_command( 

100 command_name=command, 

101 payload_builder=payload_builder, 

102 quiet=quiet, 

103 verbose=verbose, 

104 fmt=fmt_lower, 

105 pretty=pretty, 

106 debug=debug, 

107 )