use std::io::{BufRead, BufReader}; use std::process::Stdio; use std::{env::current_dir, path::PathBuf, process::Command}; use crate::model::CuddleVariable; #[allow(dead_code)] pub struct ShellAction { path: String, name: String, } impl ShellAction { pub fn new(name: String, path: String) -> Self { Self { path, name } } pub fn execute(self, variables: Vec) -> anyhow::Result<()> { log::debug!("executing shell action: {}", self.path.clone()); log::info!("Starting running shell action: {}", self.path.clone()); let path = PathBuf::from(self.path.clone()); if !path.exists() { log::info!("script='{}' not found, aborting", path.to_string_lossy()); return Err(anyhow::anyhow!("file not found aborting")); } let current_dir = current_dir()?; log::trace!("current executable dir={}", current_dir.to_string_lossy()); let mut process = Command::new(&path) .current_dir(current_dir) .envs(variables.iter().rev().map(|v| { log::trace!("{:?}", v); (v.name.to_uppercase(), v.value.clone()) })) .spawn()?; let status = process.wait()?; match status.code() { None => { log::warn!("process exited without code") } Some(n) => { if n > 0 { return Err(anyhow::anyhow!( "{} exited with: {}", path.clone().to_string_lossy(), n )); } } } log::info!("Finished running shell action"); Ok(()) } }