Coverage for src / ezpl / cli / commands / config.py: 57.76%

92 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-13 19:35 +0000

1# /////////////////////////////////////////////////////////////// 

2# EZPL - CLI Config Commands 

3# Project: ezpl 

4# /////////////////////////////////////////////////////////////// 

5 

6""" 

7CLI commands for configuration management. 

8 

9This module provides commands for getting, setting, and resetting 

10Ezpl configuration with support for user environment variables. 

11""" 

12 

13from __future__ import annotations 

14 

15# /////////////////////////////////////////////////////////////// 

16# IMPORTS 

17# /////////////////////////////////////////////////////////////// 

18# Standard library imports 

19import click 

20 

21# Third-party imports 

22from rich.table import Table 

23 

24# Local imports 

25from ...config import ConfigurationManager 

26from ...core.exceptions import FileOperationError 

27from .._console import console 

28from ..utils.env_manager import UserEnvManager 

29 

30# /////////////////////////////////////////////////////////////// 

31# COMMAND GROUP 

32# /////////////////////////////////////////////////////////////// 

33 

34 

35@click.group(name="config", help="⚙️ Manage Ezpl configuration") 

36def config_group() -> None: 

37 """ 

38 Configuration management commands. 

39 

40 Get, set, and reset Ezpl configuration with support for 

41 user environment variables. 

42 """ 

43 

44 

45# /////////////////////////////////////////////////////////////// 

46# COMMANDS 

47# /////////////////////////////////////////////////////////////// 

48 

49 

50@config_group.command(name="get", help="Get configuration value(s)") 

51@click.argument("key", required=False, type=str) 

52@click.option( 

53 "--show-env", 

54 "-e", 

55 is_flag=True, 

56 help="Show environment variable names for each key", 

57) 

58def get_command(key: str | None, show_env: bool) -> None: 

59 """ 

60 Get configuration value(s). 

61 

62 If KEY is provided, display the value for that key. 

63 Otherwise, display all configuration values. 

64 

65 Use --show-env to display the corresponding environment variable names. 

66 """ 

67 try: 

68 config_manager = ConfigurationManager() 

69 env_manager = UserEnvManager() 

70 

71 if key: 

72 # Get specific key 

73 value = config_manager.get(key) 

74 if value is not None: 74 ↛ 80line 74 didn't jump to line 80 because the condition on line 74 was always true

75 console.print(f"[green]{key}[/green]: {value}") 

76 if show_env and key in env_manager.CONFIG_TO_ENV: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true

77 env_var = env_manager.CONFIG_TO_ENV[key] 

78 console.print(f"[dim]Environment variable: {env_var}[/dim]") 

79 else: 

80 console.print(f"[red]Key '{key}' not found[/red]") 

81 else: 

82 # Get all config 

83 all_config = config_manager.get_all() 

84 

85 if not all_config: 85 ↛ 86line 85 didn't jump to line 86 because the condition on line 85 was never true

86 console.print("[yellow]No configuration found[/yellow]") 

87 return 

88 

89 # Display as table 

90 if show_env: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 table = Table( 

92 title="Ezpl Configuration", 

93 show_header=True, 

94 header_style="bold blue", 

95 ) 

96 table.add_column("Key", style="cyan", no_wrap=True) 

97 table.add_column("Value", style="white") 

98 table.add_column("Env Variable", style="dim") 

99 

100 for k, v in sorted(all_config.items()): 

101 env_var = env_manager.CONFIG_TO_ENV.get(k, "") 

102 table.add_row(k, str(v), env_var if env_var else "-") 

103 else: 

104 table = Table( 

105 title="Ezpl Configuration", 

106 show_header=True, 

107 header_style="bold blue", 

108 ) 

109 table.add_column("Key", style="cyan", no_wrap=True) 

110 table.add_column("Value", style="white") 

111 

112 for k, v in sorted(all_config.items()): 

113 table.add_row(k, str(v)) 

114 

115 console.print(table) 

116 except click.ClickException: 

117 raise 

118 except (FileOperationError, OSError, ValueError) as e: 

119 raise click.ClickException(str(e)) from e 

120 

121 

122@config_group.command(name="set", help="Set configuration value") 

123@click.argument("key", type=str) 

124@click.argument("value", type=str) 

125@click.option( 

126 "--env", 

127 "-e", 

128 is_flag=True, 

129 help="Also set as user environment variable (uses predefined variable name)", 

130) 

131def set_command(key: str, value: str, env: bool) -> None: 

132 """ 

133 Set a configuration value. 

134 

135 Set KEY to VALUE in the configuration file. 

136 The KEY must be one of the predefined configuration keys (e.g., 'log-level', 'printer-level'). 

137 

138 Use --env to also set it as a user environment variable. 

139 The environment variable name is automatically determined from the key. 

140 """ 

141 try: 

142 # Validate key exists in environment variable mapping 

143 env_manager = UserEnvManager() 

144 available_keys = list(env_manager.CONFIG_TO_ENV.keys()) 

145 

146 if key not in available_keys: 146 ↛ 147line 146 didn't jump to line 147 because the condition on line 146 was never true

147 console.print(f"[red]✗[/red] Invalid configuration key: [cyan]{key}[/cyan]") 

148 console.print("\n[yellow]Available configuration keys:[/yellow]") 

149 for available_key in sorted(available_keys): 

150 env_var_name = env_manager.CONFIG_TO_ENV[available_key] 

151 console.print( 

152 f" [cyan]{available_key:25}[/cyan] → [dim]{env_var_name}[/dim]" 

153 ) 

154 return 

155 

156 # Set configuration 

157 config_manager = ConfigurationManager() 

158 config_manager.set(key, value) 

159 config_manager.save() 

160 

161 console.print( 

162 f"[green]✓[/green] Set [cyan]{key}[/cyan] = [white]{value}[/white]" 

163 ) 

164 

165 # Set environment variable if requested 

166 if env: 166 ↛ 167line 166 didn't jump to line 167 because the condition on line 166 was never true

167 env_var_name = env_manager.CONFIG_TO_ENV[key] 

168 if env_manager.set_user_env(key, value): 

169 console.print( 

170 f"[green]✓[/green] Also set as user environment variable: [dim]{env_var_name}[/dim]" 

171 ) 

172 else: 

173 console.print( 

174 f"[yellow]⚠[/yellow] Could not set environment variable '{env_var_name}' for '{key}'" 

175 ) 

176 except click.ClickException: 

177 raise 

178 except (FileOperationError, OSError, ValueError) as e: 

179 raise click.ClickException(str(e)) from e 

180 

181 

182@config_group.command(name="reset", help="Reset configuration to defaults") 

183@click.option( 

184 "--confirm", 

185 "-y", 

186 is_flag=True, 

187 help="Skip confirmation prompt", 

188) 

189def reset_command(confirm: bool) -> None: 

190 """ 

191 Reset configuration to default values. 

192 

193 This will reset all configuration values to their defaults 

194 and remove all user environment variables. 

195 """ 

196 try: 

197 if not confirm and not click.confirm( 197 ↛ 200line 197 didn't jump to line 200 because the condition on line 197 was never true

198 "Are you sure you want to reset all configuration to defaults?" 

199 ): 

200 console.print("[yellow]Reset cancelled[/yellow]") 

201 return 

202 

203 config_manager = ConfigurationManager() 

204 config_manager.reset_to_defaults() 

205 config_manager.save() 

206 

207 env_manager = UserEnvManager() 

208 env_manager.remove_all_user_env() 

209 

210 console.print("[green]✓[/green] Configuration reset to defaults") 

211 console.print("[green]✓[/green] User environment variables removed") 

212 except click.ClickException: 

213 raise 

214 except (FileOperationError, OSError, ValueError) as e: 

215 raise click.ClickException(str(e)) from e