Skip to main content

dotm/
hooks.rs

1use anyhow::{bail, Result};
2use std::path::Path;
3use std::process::Command;
4
5/// Run a hook command via `sh -c`. Empty hooks are no-ops.
6/// Sets DOTM_PACKAGE, DOTM_TARGET, DOTM_ACTION environment variables.
7pub fn run_hook(command: &str, cwd: &Path, package: &str, action: &str) -> Result<()> {
8    if command.is_empty() {
9        return Ok(());
10    }
11
12    let status = Command::new("sh")
13        .arg("-c")
14        .arg(command)
15        .current_dir(cwd)
16        .env("DOTM_PACKAGE", package)
17        .env("DOTM_TARGET", cwd.to_str().unwrap_or(""))
18        .env("DOTM_ACTION", action)
19        .status()?;
20
21    if !status.success() {
22        bail!(
23            "hook failed for package '{}' ({}): command '{}' exited with {}",
24            package,
25            action,
26            command,
27            status
28        );
29    }
30
31    Ok(())
32}