|
3 | 3 | import os |
4 | 4 | import shlex |
5 | 5 | import subprocess |
| 6 | +from collections.abc import Callable |
6 | 7 | from pathlib import Path |
7 | 8 |
|
8 | 9 | from agr.config import ( |
@@ -145,6 +146,57 @@ def run_config_get(key: str, global_scope: bool) -> None: |
145 | 146 | print(_format_nullable_value(getattr(config, key))) |
146 | 147 |
|
147 | 148 |
|
| 149 | +def _set_default_tool(config: AgrConfig, value: str) -> str: |
| 150 | + """Validate and set default_tool. Returns the display value.""" |
| 151 | + names = normalize_and_validate_tool_names([value]) |
| 152 | + display_value = names[0] |
| 153 | + if display_value not in config.tools: |
| 154 | + error_exit( |
| 155 | + f"Tool '{display_value}' is not in configured tools. " |
| 156 | + f"Add it first with 'agr config add tools {display_value}'." |
| 157 | + ) |
| 158 | + config.default_tool = display_value |
| 159 | + return display_value |
| 160 | + |
| 161 | + |
| 162 | +def _set_default_source(config: AgrConfig, value: str) -> str: |
| 163 | + """Validate and set default_source. Returns the display value.""" |
| 164 | + source_names = [s.name for s in config.sources] |
| 165 | + if value not in source_names: |
| 166 | + error_exit( |
| 167 | + f"Source '{value}' not found in sources list.", |
| 168 | + hint=f"Available sources: {', '.join(source_names)}", |
| 169 | + ) |
| 170 | + config.default_source = value |
| 171 | + return value |
| 172 | + |
| 173 | + |
| 174 | +def _set_sync_instructions(config: AgrConfig, value: str) -> str: |
| 175 | + """Validate and set sync_instructions. Returns the display value.""" |
| 176 | + if value.lower() not in ("true", "false"): |
| 177 | + error_exit("sync_instructions must be 'true' or 'false'.") |
| 178 | + config.sync_instructions = value.lower() == "true" |
| 179 | + return value.lower() |
| 180 | + |
| 181 | + |
| 182 | +def _set_canonical_instructions(config: AgrConfig, value: str) -> str: |
| 183 | + """Validate and set canonical_instructions. Returns the display value.""" |
| 184 | + try: |
| 185 | + validate_canonical_instructions(value) |
| 186 | + except ConfigError as exc: |
| 187 | + error_exit(str(exc)) |
| 188 | + config.canonical_instructions = value |
| 189 | + return value |
| 190 | + |
| 191 | + |
| 192 | +_SCALAR_SETTERS: dict[str, Callable[[AgrConfig, str], str]] = { |
| 193 | + "default_tool": _set_default_tool, |
| 194 | + "default_source": _set_default_source, |
| 195 | + "sync_instructions": _set_sync_instructions, |
| 196 | + "canonical_instructions": _set_canonical_instructions, |
| 197 | +} |
| 198 | + |
| 199 | + |
148 | 200 | def run_config_set(key: str, values: list[str], global_scope: bool) -> None: |
149 | 201 | """Write a scalar value or replace a list.""" |
150 | 202 | console = get_console() |
@@ -176,45 +228,11 @@ def run_config_set(key: str, values: list[str], global_scope: bool) -> None: |
176 | 228 | if len(values) != 1: |
177 | 229 | error_exit(f"'{key}' expects exactly one value.") |
178 | 230 |
|
179 | | - value = values[0] |
180 | | - |
181 | | - if key == "default_tool": |
182 | | - names = normalize_and_validate_tool_names([value]) |
183 | | - display_value = names[0] |
184 | | - if display_value not in config.tools: |
185 | | - error_exit( |
186 | | - f"Tool '{display_value}' is not in configured tools. " |
187 | | - f"Add it first with 'agr config add tools {display_value}'." |
188 | | - ) |
189 | | - config.default_tool = display_value |
190 | | - |
191 | | - elif key == "default_source": |
192 | | - source_names = [s.name for s in config.sources] |
193 | | - if value not in source_names: |
194 | | - error_exit( |
195 | | - f"Source '{value}' not found in sources list.", |
196 | | - hint=f"Available sources: {', '.join(source_names)}", |
197 | | - ) |
198 | | - config.default_source = value |
199 | | - display_value = value |
200 | | - |
201 | | - elif key == "sync_instructions": |
202 | | - if value.lower() not in ("true", "false"): |
203 | | - error_exit("sync_instructions must be 'true' or 'false'.") |
204 | | - config.sync_instructions = value.lower() == "true" |
205 | | - display_value = value.lower() |
206 | | - |
207 | | - elif key == "canonical_instructions": |
208 | | - try: |
209 | | - validate_canonical_instructions(value) |
210 | | - except ConfigError as exc: |
211 | | - error_exit(str(exc)) |
212 | | - config.canonical_instructions = value |
213 | | - display_value = value |
214 | | - |
215 | | - else: |
| 231 | + setter = _SCALAR_SETTERS.get(key) |
| 232 | + if setter is None: |
216 | 233 | raise AssertionError(f"Unhandled key: {key}") |
217 | 234 |
|
| 235 | + display_value = setter(config, values[0]) |
218 | 236 | config.save() |
219 | 237 | console.print(f"[green]Set:[/green] {key} = {display_value}") |
220 | 238 |
|
|
0 commit comments