From b69f72476e6bb791d3027b5da3e716ddcccff2d0 Mon Sep 17 00:00:00 2001 From: Elad Kaplan Date: Sun, 20 Oct 2024 09:01:30 +0300 Subject: [PATCH 1/2] add plan ctx --- examples/run.rs | 12 ++++-------- src/runner.rs | 4 +++- src/step.rs | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/run.rs b/examples/run.rs index a0f2444..c4ad3d3 100644 --- a/examples/run.rs +++ b/examples/run.rs @@ -10,10 +10,8 @@ struct StepOne {} impl StepTrait for StepOne { fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result { let eco_string = randomizer.string(StringDef::default()).to_string(); - Ok(Plan { - id: std::any::type_name::().to_string(), - command: format!("echo {eco_string}"), - }) + + Ok(Plan::new::(format!("echo {eco_string}"))) } fn is_success( @@ -38,10 +36,8 @@ struct StepTwo {} impl StepTrait for StepTwo { fn plan(&self, randomizer: &crazy_train::Randomizer) -> Result { let eco_string = randomizer.string(StringDef::default()).to_string(); - Ok(Plan { - id: std::any::type_name::().to_string(), - command: format!("unknown-command {eco_string}"), - }) + + Ok(Plan::new::(format!("unknown-command {eco_string}"))) } fn is_success( diff --git a/src/runner.rs b/src/runner.rs index bc7fd90..aed8319 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -156,7 +156,7 @@ impl Runner { #[cfg(test)] mod tests { - use std::path::PathBuf; + use std::{collections::HashMap, path::PathBuf}; use serde::{Deserialize, Serialize}; @@ -186,6 +186,7 @@ mod tests { "echo {eco_string} >> {}", self.location.join("test.txt").display() ), + ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])), }) } @@ -229,6 +230,7 @@ mod tests { "cat {eco_string} >> {}", self.location.join("test.txt").display() ), + ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])), }) } diff --git a/src/step.rs b/src/step.rs index 9add3e5..59208ff 100644 --- a/src/step.rs +++ b/src/step.rs @@ -3,6 +3,8 @@ //! which encapsulates a command to be executed as part of a step. //! +use std::collections::HashMap; + use crate::{ errors, executer::{self, Output}, @@ -63,6 +65,7 @@ pub trait StepTrait { pub struct Plan { pub id: String, pub command: String, + pub ctx: Option>, } impl Plan { @@ -74,4 +77,22 @@ impl Plan { pub fn execute(&self) -> errors::Result { executer::run_sh(&self.command) } + + #[must_use] + pub fn new(command: impl Into) -> Self { + Self { + id: std::any::type_name::().to_string(), + command: command.into(), + ctx: None, + } + } + + #[must_use] + pub fn with_ctx(command: impl Into, ctx: HashMap) -> Self { + Self { + id: std::any::type_name::().to_string(), + command: command.into(), + ctx: Some(ctx), + } + } } From c03996d95465f4fe2149d03d26fc8206002815fb Mon Sep 17 00:00:00 2001 From: Elad Kaplan Date: Sun, 20 Oct 2024 09:28:11 +0300 Subject: [PATCH 2/2] add plan ctx --- examples/run.rs | 4 +++- src/runner.rs | 51 ++++++++++++++++++++++++++++++++----------------- src/step.rs | 19 +++++++++++++----- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/examples/run.rs b/examples/run.rs index c4ad3d3..6d35ed4 100644 --- a/examples/run.rs +++ b/examples/run.rs @@ -1,5 +1,5 @@ use crazy_train::{ - step::{Plan, StepTrait}, + step::{Plan, PlanCtx, StepTrait}, Result, StringDef, }; use serde::{Deserialize, Serialize}; @@ -17,6 +17,7 @@ impl StepTrait for StepOne { fn is_success( &self, execution_result: &crazy_train::executer::Output, + _plan_ctx: &PlanCtx, ) -> Result { if execution_result.status_code == Some(0) { Ok(true) @@ -43,6 +44,7 @@ impl StepTrait for StepTwo { fn is_success( &self, execution_result: &crazy_train::executer::Output, + _plan_ctx: &PlanCtx, ) -> Result { if execution_result.status_code == Some(0) { Err("expected failure command") diff --git a/src/runner.rs b/src/runner.rs index aed8319..97fbb16 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -103,11 +103,13 @@ impl Runner { "{}", format!("Execute plan finished in {:?}", start.elapsed()).yellow() ); - let is_success = step.is_success(&result).map_err(|err| Error::StepError { - kind: step::Kind::Plan, - description: err.to_string(), - command_output: result, - })?; + let is_success = + step.is_success(&result, &step_plan.ctx) + .map_err(|err| Error::StepError { + kind: step::Kind::Plan, + description: err.to_string(), + command_output: result, + })?; if !is_success { continue; @@ -159,6 +161,7 @@ mod tests { use std::{collections::HashMap, path::PathBuf}; use serde::{Deserialize, Serialize}; + use step::PlanCtx; use super::*; use crate::{executer::Output, generator::StringDef, step::Plan}; @@ -180,17 +183,28 @@ mod tests { fn plan(&self, randomizer: &Randomizer) -> Result { let eco_string = randomizer.string(StringDef::default()).to_string(); - Ok(Plan { - id: std::any::type_name::().to_string(), - command: format!( + Ok(Plan::with_vars::( + format!( "echo {eco_string} >> {}", self.location.join("test.txt").display() ), - ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])), - }) + HashMap::from([("foo".to_string(), "bar".to_string())]), + )) } - fn is_success(&self, execution_result: &Output) -> Result { + fn is_success( + &self, + execution_result: &Output, + plan_ctx: &PlanCtx, + ) -> Result { + if let Some(foo_var) = plan_ctx.vars.get("foo") { + if foo_var != "bar" { + return Err("foo value should be equal to var"); + } + } else { + return Err("foo plan ctx var not found"); + }; + if execution_result.status_code == Some(0) { Ok(true) } else { @@ -224,17 +238,20 @@ mod tests { fn plan(&self, randomizer: &Randomizer) -> Result { let eco_string = randomizer.string(StringDef::default()).to_string(); - Ok(Plan { - id: std::any::type_name::().to_string(), - command: format!( + Ok(Plan::with_vars::( + format!( "cat {eco_string} >> {}", self.location.join("test.txt").display() ), - ctx: Some(HashMap::from([("foo".to_string(), "bar".to_string())])), - }) + HashMap::from([("foo".to_string(), "bar".to_string())]), + )) } - fn is_success(&self, execution_result: &Output) -> Result { + fn is_success( + &self, + execution_result: &Output, + _plan_ctx: &PlanCtx, + ) -> Result { if execution_result.status_code == Some(1) { Ok(true) } else { diff --git a/src/step.rs b/src/step.rs index 59208ff..9d3b5d4 100644 --- a/src/step.rs +++ b/src/step.rs @@ -44,7 +44,11 @@ pub trait StepTrait { /// /// # Errors /// When plan result parsing is not the expected behavior. - fn is_success(&self, execution_result: &Output) -> Result; + fn is_success( + &self, + execution_result: &Output, + plan_ctx: &PlanCtx, + ) -> Result; /// Optionally returns a command to run as a check after the execution of the plan. fn run_check(&self) -> Option { @@ -65,7 +69,12 @@ pub trait StepTrait { pub struct Plan { pub id: String, pub command: String, - pub ctx: Option>, + pub ctx: PlanCtx, +} + +#[derive(Default, Debug, Clone)] +pub struct PlanCtx { + pub vars: HashMap, } impl Plan { @@ -83,16 +92,16 @@ impl Plan { Self { id: std::any::type_name::().to_string(), command: command.into(), - ctx: None, + ctx: PlanCtx::default(), } } #[must_use] - pub fn with_ctx(command: impl Into, ctx: HashMap) -> Self { + pub fn with_vars(command: impl Into, vars: HashMap) -> Self { Self { id: std::any::type_name::().to_string(), command: command.into(), - ctx: Some(ctx), + ctx: PlanCtx { vars }, } } }