57 lines
1.1 KiB
Rust
Raw Normal View History

2022-08-13 17:48:07 +02:00
use std::{path::PathBuf, process::Command};
2022-08-10 16:46:56 +02:00
2022-08-11 02:03:19 +02:00
use crate::cli::CuddleVariable;
#[allow(dead_code)]
2022-08-10 16:46:56 +02:00
pub struct ShellAction {
path: String,
2022-08-10 17:55:56 +02:00
name: String,
2022-08-10 16:46:56 +02:00
}
impl ShellAction {
2022-08-10 17:55:56 +02:00
pub fn new(name: String, path: String) -> Self {
Self { path, name }
2022-08-10 16:46:56 +02:00
}
2022-08-11 02:03:19 +02:00
pub fn execute(self, variables: Vec<CuddleVariable>) -> anyhow::Result<()> {
2022-08-10 17:33:31 +02:00
log::debug!("executing shell action: {}", self.path.clone());
2022-08-10 16:46:56 +02:00
2022-08-10 17:33:31 +02:00
log::info!(
2022-08-10 16:46:56 +02:00
"
2022-08-10 17:55:56 +02:00
2022-08-10 16:46:56 +02:00
===
Starting running shell action: {}
===
",
self.path.clone()
);
2022-08-13 17:48:07 +02:00
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)
2022-08-11 02:03:19 +02:00
.envs(variables.iter().map(|v| {
log::trace!("{:?}", v);
2022-08-10 16:46:56 +02:00
2022-08-11 02:03:19 +02:00
(v.name.to_uppercase(), v.value.clone())
}))
.spawn()?;
2022-08-10 16:46:56 +02:00
process.wait()?;
2022-08-10 17:33:31 +02:00
log::info!(
2022-08-10 16:46:56 +02:00
"
2022-08-10 17:55:56 +02:00
2022-08-10 16:46:56 +02:00
===
Finished running shell action
===
"
);
Ok(())
}
}