feat: rename cuddle_cli -> cuddle
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
62
cuddle/src/actions/shell.rs
Normal file
62
cuddle/src/actions/shell.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
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<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)
|
||||
.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) => {
|
||||
if n > 0 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"{} exited with: {}",
|
||||
path.clone().to_string_lossy(),
|
||||
n
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Finished running shell action");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user