use std::path::Path;
use anyhow::{Context, Result};
use serde_json::{json, Value};
use super::Installed;
pub const SETTINGS: &str = ".github/hooks/ralon.json";
pub const EVENT: &str = "PreToolUse";
pub fn entry() -> Value {
json!({
"type": "command",
"command": "ralon hook check",
"timeout": 15
})
}
pub fn document() -> Value {
json!({ "hooks": { EVENT: [entry()] } })
}
pub fn install(root: &Path, dry_run: bool) -> Result<Installed> {
let path = root.join(SETTINGS);
let replaced = path.is_file();
let rendered = format!("{}\n", serde_json::to_string_pretty(&document())?);
if dry_run {
print!("{rendered}");
} else {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
std::fs::write(&path, rendered)
.with_context(|| format!("failed to write {}", path.display()))?;
}
Ok(Installed { path, replaced })
}