use std::path::PathBuf; use clap::ArgMatches; use crate::{ actions::shell::ShellAction, model::{CuddleScript, CuddleShellScriptArg, CuddleVariable}, }; pub mod shell; #[derive(Debug, Clone)] #[allow(dead_code)] pub struct CuddleAction { pub script: CuddleScript, pub path: PathBuf, pub description: Option, pub name: String, } #[allow(dead_code)] impl CuddleAction { pub fn new( script: CuddleScript, path: PathBuf, name: String, description: Option, ) -> Self { Self { script, path, name, description, } } pub fn execute( self, matches: &ArgMatches, variables: Vec, ) -> anyhow::Result<()> { match self.script { CuddleScript::Shell(s) => { let mut arg_variables: Vec = vec![]; if let Some(args) = s.args { for (k, v) in args { let var = match v { CuddleShellScriptArg::Env(e) => { let env_var = matches.get_one::(&e.key).cloned().unwrap(); CuddleVariable::new(k.clone(), env_var) } CuddleShellScriptArg::Flag(flag) => { match matches.get_one::(&flag.name) { Some(flag_var) => { CuddleVariable::new(k.clone(), flag_var.clone()) } None => continue, } } }; arg_variables.push(var); } } else { arg_variables = vec![] }; let mut vars = variables.clone(); vars.append(&mut arg_variables); log::trace!("preparing to run action"); return match ShellAction::new( self.name.clone(), format!( "{}/scripts/{}.sh", self.path .to_str() .expect("action doesn't have a name, this should never happen"), self.name ), ) .execute(vars) { Ok(()) => { log::trace!("finished running action"); Ok(()) } Err(e) => { log::error!("{}", e); Err(e) } }; } CuddleScript::Dagger(_d) => Err(anyhow::anyhow!("not implemented yet!")), } } }