kjuulh 12bd424f18
Some checks failed
continuous-integration/drone/push Build is failing
feat: with schema
Signed-off-by: kjuulh <contact@kjuulh.io>
2023-07-02 00:47:14 +02:00

99 lines
2.9 KiB
Rust

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<String>,
pub name: String,
}
#[allow(dead_code)]
impl CuddleAction {
pub fn new(
script: CuddleScript,
path: PathBuf,
name: String,
description: Option<String>,
) -> Self {
Self {
script,
path,
name,
description,
}
}
pub fn execute(
self,
matches: &ArgMatches,
variables: Vec<CuddleVariable>,
) -> anyhow::Result<()> {
match self.script {
CuddleScript::Shell(s) => {
let mut arg_variables: Vec<CuddleVariable> = 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::<String>(&e.key).cloned().unwrap();
CuddleVariable::new(k.clone(), env_var)
}
CuddleShellScriptArg::Flag(flag) => {
match matches.get_one::<String>(&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!")),
}
}
}