Files
cuddle/cuddle_cli/src/actions/shell.rs
kjuulh 581404c622
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is passing
fix stuff
2022-08-14 22:10:58 +02:00

82 lines
1.8 KiB
Rust

use std::{
env::current_dir,
path::PathBuf,
process::{Command, ExitStatus},
};
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<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 current_dir = current_dir()?;
log::trace!("current executable dir={}", current_dir.to_string_lossy());
let mut process = Command::new(path.clone())
.current_dir(current_dir)
.envs(variables.iter().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) => {
log::info!("process exited with code: {}", n);
if n > 0 {
return Err(anyhow::anyhow!(
"{} exited with: {}",
path.clone().to_string_lossy(),
n
));
}
}
}
log::info!(
"
===
Finished running shell action
===
"
);
Ok(())
}
}