Skip to content

Commit 37ebc7e

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: extract scalar key setters from run_config_set to reduce complexity from 13 to 7
Each scalar config key (default_tool, default_source, sync_instructions, canonical_instructions) now has a dedicated setter function. The main run_config_set dispatches via a _SCALAR_SETTERS dict instead of an if/elif chain, making it easier to add new keys and keeping each validation path independently readable and testable. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 1f0476f commit 37ebc7e

1 file changed

Lines changed: 55 additions & 37 deletions

File tree

agr/commands/config_cmd.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import shlex
55
import subprocess
6+
from collections.abc import Callable
67
from pathlib import Path
78

89
from agr.config import (
@@ -145,6 +146,57 @@ def run_config_get(key: str, global_scope: bool) -> None:
145146
print(_format_nullable_value(getattr(config, key)))
146147

147148

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+
148200
def run_config_set(key: str, values: list[str], global_scope: bool) -> None:
149201
"""Write a scalar value or replace a list."""
150202
console = get_console()
@@ -176,45 +228,11 @@ def run_config_set(key: str, values: list[str], global_scope: bool) -> None:
176228
if len(values) != 1:
177229
error_exit(f"'{key}' expects exactly one value.")
178230

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:
216233
raise AssertionError(f"Unhandled key: {key}")
217234

235+
display_value = setter(config, values[0])
218236
config.save()
219237
console.print(f"[green]Set:[/green] {key} = {display_value}")
220238

0 commit comments

Comments
 (0)