use std::fs;
use std::path::{Path, PathBuf};
use anyhow::Result;
use openmoat_hosts::{OwnedFile, PluginConfig};
use serde_json::Value;
use super::{HookState, HostConfig, Outcome, owned};
use crate::home::write_private;
pub fn plugin_path(config: &HostConfig, layout: &dyn PluginConfig) -> PathBuf {
layout
.plugin_path()
.iter()
.fold(config.dir().to_path_buf(), |path, part| path.join(part))
}
pub fn plugin_current(config: &HostConfig, layout: &dyn PluginConfig, binary: &Path) -> bool {
let wanted = owned::text(config, layout.plugin(), binary).ok();
fs::read_to_string(plugin_path(config, layout)).ok() == wanted
}
pub fn write_plugin(config: &HostConfig, layout: &dyn PluginConfig, binary: &Path) -> Result<()> {
write_private(
&plugin_path(config, layout),
owned::text(config, layout.plugin(), binary)?.as_bytes(),
)
}
fn plugin_binary(plugin: &OwnedFile, path: &Path) -> Option<String> {
plugin.binary(&fs::read_to_string(path).ok()?)
}
pub fn upsert(root: &mut Value, config: &HostConfig, layout: &dyn PluginConfig) -> Result<Outcome> {
let file = config.settings_path.display().to_string();
let ours = crate::context::path_string(&plugin_path(config, layout));
layout
.upsert(root, &file, &ours, config.hooks)
.map_err(anyhow::Error::msg)
}
pub fn state(
config: &HostConfig,
layout: &dyn PluginConfig,
binary: &Path,
root: &Value,
) -> HookState {
if !layout.lists_ours(root) {
return HookState::Missing;
}
let mut desired = root.clone();
let current = matches!(upsert(&mut desired, config, layout), Ok(Outcome::Unchanged));
if current && plugin_current(config, layout, binary) {
return HookState::Installed;
}
HookState::Stale {
command: plugin_binary(layout.plugin(), &plugin_path(config, layout))
.unwrap_or_else(|| "?".to_owned()),
}
}
pub fn remove_plugin(config: &HostConfig, layout: &dyn PluginConfig) -> Result<Option<PathBuf>> {
let path = plugin_path(config, layout);
if !path.is_file() {
return Ok(None);
}
fs::remove_file(&path)?;
if let Some(dir) = path.parent() {
fs::remove_dir(dir).ok();
}
Ok(Some(path))
}
#[cfg(test)]
mod tests;