diff --git a/crates/forest/src/script.rs b/crates/forest/src/script.rs index 8343967..62dce4a 100644 --- a/crates/forest/src/script.rs +++ b/crates/forest/src/script.rs @@ -4,58 +4,7 @@ use shell::ShellExecutor; use crate::model::{Context, Script}; -pub mod shell { - use std::process::Stdio; - - use anyhow::Context; - - use super::ScriptExecutor; - - pub struct ShellExecutor { - root: ScriptExecutor, - } - - impl ShellExecutor { - pub async fn execute(&self, name: &str) -> anyhow::Result<()> { - let path = &self.root.project_path; - let script_path = path.join("scripts").join(format!("{name}.sh")); - - if !script_path.exists() { - anyhow::bail!("script was not found at: {}", script_path.display()); - } - - let mut cmd = tokio::process::Command::new(&script_path); - let cmd = cmd.current_dir(path); - cmd.stdin(Stdio::inherit()); - cmd.stdout(Stdio::inherit()); - cmd.stderr(Stdio::inherit()); - - let mut proc = cmd.spawn().context(format!( - "failed to spawn process: {}", - script_path.display() - ))?; - - let exit = proc.wait().await?; - - if !exit.success() { - anyhow::bail!( - "command: {name} failed with status: {}", - exit.code().unwrap_or(-1) - ) - } - - Ok(()) - } - } - - impl From<&ScriptExecutor> for ShellExecutor { - fn from(value: &ScriptExecutor) -> Self { - Self { - root: value.clone(), - } - } - } -} +pub mod shell; #[derive(Clone)] pub struct ScriptExecutor { diff --git a/crates/forest/src/script/shell.rs b/crates/forest/src/script/shell.rs new file mode 100644 index 0000000..eb1dd3a --- /dev/null +++ b/crates/forest/src/script/shell.rs @@ -0,0 +1,50 @@ +use std::process::Stdio; + +use anyhow::Context; + +use super::ScriptExecutor; + +pub struct ShellExecutor { + root: ScriptExecutor, +} + +impl ShellExecutor { + pub async fn execute(&self, name: &str) -> anyhow::Result<()> { + let path = &self.root.project_path; + let script_path = path.join("scripts").join(format!("{name}.sh")); + + if !script_path.exists() { + anyhow::bail!("script was not found at: {}", script_path.display()); + } + + let mut cmd = tokio::process::Command::new(&script_path); + let cmd = cmd.current_dir(path); + cmd.stdin(Stdio::inherit()); + cmd.stdout(Stdio::inherit()); + cmd.stderr(Stdio::inherit()); + + let mut proc = cmd.spawn().context(format!( + "failed to spawn process: {}", + script_path.display() + ))?; + + let exit = proc.wait().await?; + + if !exit.success() { + anyhow::bail!( + "command: {name} failed with status: {}", + exit.code().unwrap_or(-1) + ) + } + + Ok(()) + } +} + +impl From<&ScriptExecutor> for ShellExecutor { + fn from(value: &ScriptExecutor) -> Self { + Self { + root: value.clone(), + } + } +}