Skip to content

Commit bd6ee2b

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: extract helpers from run_init to reduce complexity from 16 to 7
Extract _resolve_tools and _apply_instruction_sync from run_init so it stays under the C901 threshold. Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 37ebc7e commit bd6ee2b

1 file changed

Lines changed: 51 additions & 29 deletions

File tree

agr/commands/init.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,55 @@ def _parse_tools_flag(value: str | None) -> list[str] | None:
6767
return raw or None
6868

6969

70+
def _resolve_tools(
71+
config: AgrConfig,
72+
tools_flag: str | None,
73+
created: bool,
74+
repo_root: Path,
75+
) -> list[str] | None:
76+
"""Resolve the tools list from flag, auto-detection, or existing config.
77+
78+
Updates ``config.tools`` in place and returns the list for display,
79+
or None when there is nothing to display.
80+
"""
81+
tools_override = _parse_tools_flag(tools_flag)
82+
if tools_override:
83+
tools_override = normalize_and_validate_tool_names(tools_override)
84+
config.tools = tools_override
85+
return tools_override
86+
87+
if created:
88+
detected = detect_tools(repo_root)
89+
if detected:
90+
config.tools = detected
91+
return detected
92+
return None
93+
94+
return config.tools if config.tools else None
95+
96+
97+
def _apply_instruction_sync(
98+
config: AgrConfig,
99+
sync_instructions: bool | None,
100+
canonical_instructions: str | None,
101+
) -> None:
102+
"""Apply instruction-sync settings to *config* in place."""
103+
if sync_instructions is not None:
104+
config.sync_instructions = sync_instructions
105+
106+
if canonical_instructions:
107+
try:
108+
validate_canonical_instructions(canonical_instructions)
109+
except ConfigError as exc:
110+
error_exit(str(exc))
111+
config.canonical_instructions = canonical_instructions
112+
elif config.sync_instructions and config.canonical_instructions is None:
113+
if config.default_tool:
114+
config.canonical_instructions = canonical_instruction_file(
115+
config.default_tool
116+
)
117+
118+
70119
def run_init(
71120
skill_name: str | None = None,
72121
*,
@@ -110,20 +159,7 @@ def run_init(
110159
else:
111160
console.print(f"[yellow]Already exists:[/yellow] {config_path}")
112161

113-
# Tools
114-
tools_display: list[str] | None = None
115-
tools_override = _parse_tools_flag(tools)
116-
if tools_override:
117-
tools_override = normalize_and_validate_tool_names(tools_override)
118-
config.tools = tools_override
119-
tools_display = tools_override
120-
elif created:
121-
detected_tools = detect_tools(repo_root)
122-
if detected_tools:
123-
config.tools = detected_tools
124-
tools_display = detected_tools
125-
else:
126-
tools_display = config.tools if config.tools else None
162+
tools_display = _resolve_tools(config, tools, created, repo_root)
127163

128164
# Default tool
129165
if default_tool:
@@ -133,21 +169,7 @@ def run_init(
133169
if config.default_tool and config.default_tool not in config.tools:
134170
error_exit("default_tool must be listed in tools. Use --tools to include it.")
135171

136-
# Instruction sync
137-
if sync_instructions is not None:
138-
config.sync_instructions = sync_instructions
139-
140-
if canonical_instructions:
141-
try:
142-
validate_canonical_instructions(canonical_instructions)
143-
except ConfigError as exc:
144-
error_exit(str(exc))
145-
config.canonical_instructions = canonical_instructions
146-
elif config.sync_instructions and config.canonical_instructions is None:
147-
if config.default_tool:
148-
config.canonical_instructions = canonical_instruction_file(
149-
config.default_tool
150-
)
172+
_apply_instruction_sync(config, sync_instructions, canonical_instructions)
151173

152174
current_state = (
153175
list(config.tools),

0 commit comments

Comments
 (0)