use std::path::{Path, PathBuf};
use openmoat_hosts::{Host, SettingsFile};
use serde_json::{Map, Value};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Off {
pub path: PathBuf,
pub what: Vec<String>,
pub overridden: bool,
}
#[must_use]
pub fn turned_off(
cwd: &Path,
user_home: &Path,
host: Host,
hook_file: &Path,
keys: &[&str],
) -> Vec<Off> {
let Some(spec) = host.spec().project_settings.as_ref() else {
return Vec::new();
};
let mut found = Vec::new();
for name in spec.files() {
for path in super::candidates(cwd, user_home, spec, name) {
let bytes = match super::read_bytes(&path) {
Ok(None) => continue,
Ok(Some(bytes)) => bytes,
Err(e) => {
found.push(Off {
path,
what: vec![format!("cannot be checked: {e:#}")],
overridden: false,
});
continue;
}
};
let text = String::from_utf8_lossy(&bytes);
if let Some(marker) = spec
.reader
.substitutions()
.iter()
.find(|marker| text.contains(**marker))
{
found.push(Off {
path,
what: vec![format!(
"holds `{marker}…}}`, which the agent replaces with text OpenMoat cannot \
read before it loads the file"
)],
overridden: false,
});
continue;
}
let Ok(root) = super::parse(&path, &bytes, spec.reader.reads_comments()) else {
continue;
};
let base = path.parent().unwrap_or(&path).to_path_buf();
let off = |root: &Map<String, Value>| {
let file = SettingsFile::new(root, &base, hook_file, Some(user_home));
spec.reader.project_switched_off(&file)
};
let what = off(&root);
if what.is_empty() {
continue;
}
let mut rest = root.clone();
rest.retain(|key, _| !keys.contains(&key.as_str()));
let overridden = !keys.is_empty() && off(&rest).is_empty();
found.push(Off {
path,
what,
overridden,
});
}
}
if let Some(dir) = &spec.plugin_dir {
for root in super::project_dirs(cwd, user_home) {
if let Some(plugin) = super::plugins::replacing(&root, dir, hook_file) {
found.push(Off {
path: plugin,
what: vec![format!(
"replaces OpenMoat's plugin with one of the project's (`{}`)",
dir.own
)],
overridden: false,
});
}
}
}
found
}
#[cfg(test)]
mod tests;