use anyhow::Context as _;
use diffr_plugin_sdk::host::Host as SdkHost;
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
#[derive(Clone)]
pub(crate) struct Host {
pub(crate) name: Arc<str>,
pub(crate) workdir: Arc<Path>,
}
impl Host {
pub(crate) fn git(&self, args: &[String]) -> anyhow::Result<Result<String, String>> {
let output = Command::new("git")
.args(args)
.current_dir(&*self.workdir)
.output()
.with_context(|| format!("plugin {}: running git {args:?}", self.name))?;
let text = |bytes: Vec<u8>| {
String::from_utf8(bytes)
.with_context(|| format!("plugin {}: git {args:?} wrote non-UTF-8", self.name))
};
Ok(match output.status.success() {
true => Ok(text(output.stdout)?),
false => Err(text(output.stderr)?),
})
}
}
impl SdkHost for Host {
fn git(&self, args: &[String]) -> Result<String, String> {
Host::git(self, args).unwrap_or_else(|error| Err(format!("{error:#}")))
}
}