Install hooks into a settings file (project or global).
(settings_path: &Path, force: bool)
| 374 | |
| 375 | /// Install hooks into a settings file (project or global). |
| 376 | fn install_to(settings_path: &Path, force: bool) -> AgentResult<usize> { |
| 377 | // Ensure parent directory exists |
| 378 | if let Some(parent) = settings_path.parent() { |
| 379 | if !parent.exists() { |
| 380 | std::fs::create_dir_all(parent).map_err(|e| AgentError::ConfigError { |
| 381 | operation: "create directory".to_string(), |
| 382 | path: parent.to_path_buf(), |
| 383 | reason: e.to_string(), |
| 384 | })?; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | let (mut raw, mut hooks) = Self::read_settings(settings_path)?; |
| 389 | |
| 390 | if force { |
| 391 | remove_atomic_hooks(&mut hooks.session_start); |
| 392 | remove_atomic_hooks(&mut hooks.session_end); |
| 393 | remove_atomic_hooks(&mut hooks.before_agent); |
| 394 | remove_atomic_hooks(&mut hooks.after_agent); |
| 395 | remove_atomic_hooks(&mut hooks.before_tool); |
| 396 | remove_atomic_hooks(&mut hooks.after_tool); |
| 397 | } |
| 398 | |
| 399 | // Define the hooks to install: |
| 400 | // (settings event key, matcher, verb, name, description) |
| 401 | let hook_defs: Vec<(&str, &str, &str, &str)> = vec![ |
| 402 | ("session_start", "", "session-start", "atomic-session-start"), |
| 403 | ("session_end", "", "session-end", "atomic-session-end"), |
| 404 | ("before_agent", "", "before-agent", "atomic-turn-start"), |
| 405 | ("after_agent", "", "after-agent", "atomic-turn-end"), |
| 406 | ("before_tool", ".*", "before-tool", "atomic-pre-tool"), |
| 407 | ("after_tool", ".*", "after-tool", "atomic-post-tool"), |
| 408 | ]; |
| 409 | |
| 410 | let mut count = 0; |
| 411 | for (event_key, matcher, verb, hook_name) in &hook_defs { |
| 412 | let command = |
| 413 | crate::hooks::guarded_hook_command(&format!("{} {}", ATOMIC_HOOK_PREFIX, verb)); |
| 414 | |
| 415 | let matchers = match *event_key { |
| 416 | "session_start" => &mut hooks.session_start, |
| 417 | "session_end" => &mut hooks.session_end, |
| 418 | "before_agent" => &mut hooks.before_agent, |
| 419 | "after_agent" => &mut hooks.after_agent, |
| 420 | "before_tool" => &mut hooks.before_tool, |
| 421 | "after_tool" => &mut hooks.after_tool, |
| 422 | _ => continue, |
| 423 | }; |
| 424 | |
| 425 | if !hook_command_exists(matchers, matcher, &command) { |
| 426 | add_hook_to_matcher(matchers, matcher, &command, Some(hook_name)); |
| 427 | count += 1; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if count == 0 { |
| 432 | return Ok(0); |
| 433 | } |
nothing calls this directly
no test coverage detected