updated with error handling

This commit is contained in:
Kasper Juul Hermansen 2022-08-13 17:34:49 +02:00
parent 275435daf5
commit f0488cded8
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
2 changed files with 19 additions and 8 deletions

View File

@ -35,7 +35,13 @@ impl CuddleAction {
for (k, v) in args { for (k, v) in args {
let var = match v { let var = match v {
CuddleShellScriptArg::Env(e) => { CuddleShellScriptArg::Env(e) => {
let env_var = env::var(e.key.clone())?; let env_var = match env::var(e.key.clone()) {
Ok(var) => var,
Err(e) => {
log::error!("env_variable not found: {}", k);
return Err(anyhow::anyhow!(e));
}
};
CuddleVariable::new(k.clone(), env_var) CuddleVariable::new(k.clone(), env_var)
} }
}; };
@ -49,6 +55,8 @@ impl CuddleAction {
let mut vars = variables.clone(); let mut vars = variables.clone();
vars.append(&mut arg_variables); vars.append(&mut arg_variables);
log::trace!("preparing to run action");
match actions::shell::ShellAction::new( match actions::shell::ShellAction::new(
self.name.clone(), self.name.clone(),
format!( format!(
@ -61,7 +69,10 @@ impl CuddleAction {
) )
.execute(vars) .execute(vars)
{ {
Ok(()) => Ok(()), Ok(()) => {
log::trace!("finished running action");
Ok(())
}
Err(e) => { Err(e) => {
log::error!("{}", e); log::error!("{}", e);
Err(e) Err(e)
@ -274,19 +285,19 @@ impl<'a> CuddleCli<'a> {
match exe_submatch.subcommand() { match exe_submatch.subcommand() {
Some((name, _action_matches)) => { Some((name, _action_matches)) => {
log::trace!(action=name; "running action"); log::trace!(action=name; "running action; name={}", name);
match self.scripts.iter().find(|ele| ele.name == name) { match self.scripts.iter().find(|ele| ele.name == name) {
Some(script) => { Some(script) => {
script.clone().execute(self.variables.clone()); script.clone().execute(self.variables.clone())?;
Ok(()) Ok(())
} }
_ => (Err(anyhow!("could not find a match"))), _ => Err(anyhow!("could not find a match")),
} }
} }
_ => (Err(anyhow!("could not find a match"))), _ => Err(anyhow!("could not find a match")),
} }
} }
_ => (Err(anyhow!("could not find a match"))), _ => Err(anyhow!("could not find a match")),
}; };
match res { match res {

View File

@ -35,7 +35,7 @@ fn main() -> anyhow::Result<()> {
fn init_logging() -> anyhow::Result<()> { fn init_logging() -> anyhow::Result<()> {
tracing_subscriber::fmt() tracing_subscriber::fmt()
.pretty() .pretty()
.with_max_level(Level::INFO) .with_max_level(Level::TRACE)
.init(); .init();
Ok(()) Ok(())