feat: with context
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-02-03 18:54:17 +01:00
parent e8507cd2f2
commit 512c3f625e
7 changed files with 89 additions and 38 deletions

View File

@@ -1,4 +1,8 @@
use std::sync::Arc;
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
sync::Arc,
};
use async_trait::async_trait;
use tokio::sync::Mutex;
@@ -9,6 +13,25 @@ pub struct CuddleCI {
release_action: Vec<Arc<Mutex<dyn ReleaseAction + Send + Sync>>>,
}
#[derive(Default, Debug)]
pub struct Context {
store: BTreeMap<String, String>,
}
impl Deref for Context {
type Target = BTreeMap<String, String>;
fn deref(&self) -> &Self::Target {
&self.store
}
}
impl DerefMut for Context {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.store
}
}
impl CuddleCI {
pub fn new(
pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>,
@@ -58,26 +81,36 @@ impl CuddleCI {
.subcommand_required(true)
.try_get_matches_from(args.into_iter().map(|a| a.into()).collect::<Vec<String>>())?;
let mut context = Context::default();
match matches.subcommand() {
Some((name, args)) => match (name, args) {
("pr", _args) => {
eprintln!("starting pr validate");
for pr_action in self.pr_action.iter() {
pr_action.lock().await.execute_pull_request().await?;
pr_action
.lock()
.await
.execute_pull_request(&mut context)
.await?;
}
eprintln!("finished pr validate");
}
("main", _args) => {
eprintln!("starting main validate");
for main_action in self.main_action.iter() {
main_action.lock().await.execute_main().await?;
main_action.lock().await.execute_main(&mut context).await?;
}
eprintln!("finished main validate");
}
("release", _args) => {
eprintln!("starting release validate");
for release_action in self.release_action.iter() {
release_action.lock().await.execute_release().await?;
release_action
.lock()
.await
.execute_release(&mut context)
.await?;
}
eprintln!("finished release validate");
}
@@ -104,7 +137,7 @@ impl Default for CuddleCI {
#[async_trait]
pub trait PullRequestAction {
async fn execute_pull_request(&self) -> eyre::Result<()> {
async fn execute_pull_request(&self, _ctx: &mut Context) -> eyre::Result<()> {
eprintln!("validate pull request: noop");
Ok(())
}
@@ -116,20 +149,20 @@ impl PullRequestAction for DefaultPullRequestAction {}
#[async_trait]
pub trait MainAction {
async fn execute_main(&self) -> eyre::Result<()>;
async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()>;
}
pub struct DefaultMainAction {}
#[async_trait]
impl MainAction for DefaultMainAction {
async fn execute_main(&self) -> eyre::Result<()> {
async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()> {
Ok(())
}
}
#[async_trait]
pub trait ReleaseAction {
async fn execute_release(&self) -> eyre::Result<()> {
async fn execute_release(&self, _ctx: &mut Context) -> eyre::Result<()> {
eprintln!("validate release: noop");
Ok(())