use std::path::Path;
use serde_json::{Value, json};
use crate::install::{HookSpec, HostConfig};
pub(super) fn entry(config: &HostConfig, spec: &HookSpec, binary: &Path) -> Value {
let path = binary.to_string_lossy();
let args = guard_args(config);
json!({
"type": "command",
"bash": format!("'{}'{args}", path.replace('\'', r"'\''")),
"powershell": format!("& '{}'{args}", path.replace('\'', "''")),
"timeoutSec": spec.timeout,
})
}
fn guard_args(config: &HostConfig) -> String {
format!(" guard --host {}", config.host.id())
}
pub(super) fn is_ours(entry: &Value, config: &HostConfig) -> bool {
bash(entry).is_some_and(|c| c.trim_end().ends_with(&guard_args(config)))
}
pub(super) fn binary(entry: &Value, config: &HostConfig) -> Option<String> {
let quoted = bash(entry)?
.trim_end()
.strip_suffix(&guard_args(config))?
.trim();
Some(
match quoted.strip_prefix('\'').and_then(|q| q.strip_suffix('\'')) {
Some(inner) => inner.replace(r"'\''", "'"),
None => quoted.to_owned(),
},
)
}
fn bash(entry: &Value) -> Option<&str> {
entry.get("bash").and_then(Value::as_str)
}
#[cfg(test)]
mod tests;