use std::{fs::File, process::Command};
use crate::{
config::{FileContents, Override},
error::{Result, with_io_context},
};
#[cfg(feature = "opencode")]
pub mod opencode;
#[cfg(feature = "zed")]
pub mod zed;
#[cfg(any(feature = "opencode", feature = "zed"))]
mod jsonc;
#[derive(Debug)]
#[cfg_attr(feature = "config_file", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub struct ExternalTool {
pub name: String,
}
impl ExternalTool {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}
}
impl Override for ExternalTool {
fn apply(&self, mut contents: FileContents) -> Result<()> {
let path = std::env::temp_dir().join("tartarus-temp-override-helper");
let mut temp_file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(&path)
.map_err(|e| {
with_io_context(self.name.as_str(), "preparing input file for ExternalTool", e)
})?;
std::io::copy(&mut contents.read(), &mut temp_file).map_err(|e| {
with_io_context(self.name.as_str(), "writing initial contents for ExternalTool", e)
})?;
Command::new(&self.name)
.arg(&path)
.status()
.map_err(|e| with_io_context(self.name.as_str(), "running ExternalTool", e))?;
Ok(())
}
}