refactor: shell
This commit is contained in:
parent
9be64b74b2
commit
2fadde0f8a
@ -4,58 +4,7 @@ use shell::ShellExecutor;
|
|||||||
|
|
||||||
use crate::model::{Context, Script};
|
use crate::model::{Context, Script};
|
||||||
|
|
||||||
pub mod shell {
|
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(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ScriptExecutor {
|
pub struct ScriptExecutor {
|
||||||
|
50
crates/forest/src/script/shell.rs
Normal file
50
crates/forest/src/script/shell.rs
Normal 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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user