2022-08-13 17:57:00 +02:00

57 lines
1.1 KiB
Rust

use std::{path::PathBuf, process::Command};
use crate::cli::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<CuddleVariable>) -> 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 mut process = Command::new(path)
.envs(variables.iter().map(|v| {
log::trace!("{:?}", v);
(v.name.to_uppercase(), v.value.clone())
}))
.spawn()?;
process.wait()?;
log::info!(
"
===
Finished running shell action
===
"
);
Ok(())
}
}