From 512c3f625eba588bdfd5fdca0374bfbe205fb214 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 3 Feb 2024 18:54:17 +0100 Subject: [PATCH] feat: with context Signed-off-by: kjuulh --- crates/cuddle-ci/src/cli.rs | 49 +++++++++++++++++++++---- crates/cuddle-ci/src/cuddle_please.rs | 8 ++-- crates/cuddle-ci/src/cuddle_releaser.rs | 4 +- crates/cuddle-ci/src/leptos_service.rs | 6 +-- crates/cuddle-ci/src/node_service.rs | 6 +-- crates/cuddle-ci/src/rust_lib.rs | 5 ++- crates/cuddle-ci/src/rust_service.rs | 49 +++++++++++++++++-------- 7 files changed, 89 insertions(+), 38 deletions(-) diff --git a/crates/cuddle-ci/src/cli.rs b/crates/cuddle-ci/src/cli.rs index 2549bbf..cf1d07a 100644 --- a/crates/cuddle-ci/src/cli.rs +++ b/crates/cuddle-ci/src/cli.rs @@ -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>>, } +#[derive(Default, Debug)] +pub struct Context { + store: BTreeMap, +} + +impl Deref for Context { + type Target = BTreeMap; + + 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>, @@ -58,26 +81,36 @@ impl CuddleCI { .subcommand_required(true) .try_get_matches_from(args.into_iter().map(|a| a.into()).collect::>())?; + 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(()) diff --git a/crates/cuddle-ci/src/cuddle_please.rs b/crates/cuddle-ci/src/cuddle_please.rs index 82c00ad..559eda1 100644 --- a/crates/cuddle-ci/src/cuddle_please.rs +++ b/crates/cuddle-ci/src/cuddle_please.rs @@ -1,9 +1,7 @@ use async_trait::async_trait; -use dagger_cuddle_please::{ - models::CuddlePleaseSrcArgs, DaggerCuddlePlease, DaggerCuddlePleaseAction, -}; +use dagger_cuddle_please::{models::CuddlePleaseSrcArgs, DaggerCuddlePlease}; -use crate::{MainAction, PullRequestAction}; +use crate::{Context, MainAction}; pub struct CuddlePlease { client: dagger_sdk::Query, @@ -17,7 +15,7 @@ impl CuddlePlease { #[async_trait] impl MainAction for CuddlePlease { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()> { let client = self.client.clone(); let action = DaggerCuddlePlease::new(client); diff --git a/crates/cuddle-ci/src/cuddle_releaser.rs b/crates/cuddle-ci/src/cuddle_releaser.rs index 1ea3c4a..d21b3ed 100644 --- a/crates/cuddle-ci/src/cuddle_releaser.rs +++ b/crates/cuddle-ci/src/cuddle_releaser.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use eyre::Context; -use crate::{cuddle_file::CuddleFile, MainAction}; +use crate::{cli, cuddle_file::CuddleFile, MainAction}; pub struct CuddleReleaser { client: dagger_sdk::Query, @@ -35,7 +35,7 @@ impl CuddleReleaser { #[async_trait] impl MainAction for CuddleReleaser { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, _ctx: &mut cli::Context) -> eyre::Result<()> { let client = self.client.clone(); if self.cuddle_file.deployment.is_none() { diff --git a/crates/cuddle-ci/src/leptos_service.rs b/crates/cuddle-ci/src/leptos_service.rs index ab93c49..db94587 100644 --- a/crates/cuddle-ci/src/leptos_service.rs +++ b/crates/cuddle-ci/src/leptos_service.rs @@ -12,7 +12,7 @@ use crate::{ extensions::CargoBInstallExt, RustServiceStage, }, - MainAction, PullRequestAction, + Context, MainAction, PullRequestAction, }; #[derive(Clone)] @@ -328,7 +328,7 @@ impl LeptosService { #[async_trait] impl PullRequestAction for LeptosService { - async fn execute_pull_request(&self) -> eyre::Result<()> { + async fn execute_pull_request(&self, _ctx: &mut Context) -> eyre::Result<()> { let mut s = self.clone(); s.with_cargo_binstall("latest", ["cargo-leptos"]) @@ -341,7 +341,7 @@ impl PullRequestAction for LeptosService { #[async_trait] impl MainAction for LeptosService { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()> { let mut s = self.clone(); let container = s diff --git a/crates/cuddle-ci/src/node_service.rs b/crates/cuddle-ci/src/node_service.rs index 8f95e0a..ccc9d1b 100644 --- a/crates/cuddle-ci/src/node_service.rs +++ b/crates/cuddle-ci/src/node_service.rs @@ -6,7 +6,7 @@ use dagger_sdk::{Container, ContainerWithDirectoryOptsBuilder, HostDirectoryOpts use crate::{ dagger_middleware::DynMiddleware, rust_service::architecture::{Architecture, Os}, - MainAction, PullRequestAction, + Context, MainAction, PullRequestAction, }; #[derive(Clone)] @@ -215,7 +215,7 @@ impl NodeService { #[async_trait] impl PullRequestAction for NodeService { - async fn execute_pull_request(&self) -> eyre::Result<()> { + async fn execute_pull_request(&self, _ctx: &mut Context) -> eyre::Result<()> { let release = self.build_release().await?; release.sync().await?; @@ -226,7 +226,7 @@ impl PullRequestAction for NodeService { #[async_trait] impl MainAction for NodeService { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()> { let container = self.build_release().await?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) diff --git a/crates/cuddle-ci/src/rust_lib.rs b/crates/cuddle-ci/src/rust_lib.rs index b4ce8b0..6b87858 100644 --- a/crates/cuddle-ci/src/rust_lib.rs +++ b/crates/cuddle-ci/src/rust_lib.rs @@ -6,6 +6,7 @@ use dagger_sdk::Container; use futures::{stream, StreamExt}; use crate::{ + cli, dagger_middleware::{DaggerMiddleware, DynMiddleware}, rust_service::architecture::{Architecture, Os}, MainAction, PullRequestAction, @@ -141,7 +142,7 @@ impl RustLib { #[async_trait] impl PullRequestAction for RustLib { - async fn execute_pull_request(&self) -> eyre::Result<()> { + async fn execute_pull_request(&self, _ctx: &mut cli::Context) -> eyre::Result<()> { self.build_test().await?; Ok(()) @@ -150,7 +151,7 @@ impl PullRequestAction for RustLib { #[async_trait] impl MainAction for RustLib { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, _ctx: &mut cli::Context) -> eyre::Result<()> { self.build_test().await?; Ok(()) diff --git a/crates/cuddle-ci/src/rust_service.rs b/crates/cuddle-ci/src/rust_service.rs index 8cd5e89..adbccc8 100644 --- a/crates/cuddle-ci/src/rust_service.rs +++ b/crates/cuddle-ci/src/rust_service.rs @@ -7,7 +7,7 @@ use futures::{stream, StreamExt}; use crate::{ dagger_middleware::{DaggerMiddleware, DynMiddleware}, - MainAction, PullRequestAction, + Context, MainAction, PullRequestAction, }; use self::architecture::{Architecture, Os}; @@ -325,29 +325,52 @@ impl RustService { #[async_trait] impl PullRequestAction for RustService { - async fn execute_pull_request(&self) -> eyre::Result<()> { + async fn execute_pull_request(&self, _ctx: &mut Context) -> eyre::Result<()> { self.build_test().await?; Ok(()) } } +const IMAGE_TAG: &str = "RUST_SERVICE_IMAGE_TAG"; + +pub trait RustServiceContext { + fn set_image_tag(&mut self, tag: impl Into) -> eyre::Result<()>; + fn get_image_tag(&self) -> eyre::Result>; +} + +impl RustServiceContext for Context { + fn get_image_tag(&self) -> eyre::Result> { + Ok(self.get(IMAGE_TAG).cloned()) + } + + fn set_image_tag(&mut self, tag: impl Into) -> eyre::Result<()> { + let tag = tag.into(); + + self.insert(IMAGE_TAG.to_string(), tag); + + Ok(()) + } +} + #[async_trait] impl MainAction for RustService { - async fn execute_main(&self) -> eyre::Result<()> { + async fn execute_main(&self, ctx: &mut Context) -> eyre::Result<()> { let container = self.build_release().await?; let timestamp = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs(); - container + let image_tag = container .publish(format!( "docker.io/kasperhermansen/{}:main-{}", self.bin_name, timestamp, )) .await?; + ctx.set_image_tag(image_tag)?; + if self.deployment { let update_deployments_docker_image = "docker.io/kasperhermansen/update-deployment:1701123940"; @@ -443,18 +466,14 @@ pub mod extensions { #[cfg(test)] mod test { - use futures::FutureExt; - use crate::{ - dagger_middleware::middleware, - rust_service::{ - apt::AptExt, - architecture::{Architecture, Os}, - cargo_binstall::CargoBInstallExt, - clap_sanity_test::ClapSanityTestExt, - mold::MoldActionExt, - RustService, RustServiceStage, - }, + use crate::rust_service::{ + apt::AptExt, + architecture::{Architecture, Os}, + cargo_binstall::CargoBInstallExt, + clap_sanity_test::ClapSanityTestExt, + mold::MoldActionExt, + RustService, }; #[tokio::test]