refactor: shell

This commit is contained in:
Kasper Juul Hermansen 2025-02-28 21:23:23 +01:00
parent 9be64b74b2
commit 2fadde0f8a
Signed by: kjuulh
SSH Key Fingerprint: SHA256:RjXh0p7U6opxnfd3ga/Y9TCo18FYlHFdSpRIV72S/QM
2 changed files with 51 additions and 52 deletions

View File

@ -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 {

View File

@ -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(),
}
}
}