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:
Kasper Juul Hermansen 2024-02-03 18:54:17 +01:00
parent e8507cd2f2
commit 512c3f625e
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
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 async_trait::async_trait;
use tokio::sync::Mutex; use tokio::sync::Mutex;
@ -9,6 +13,25 @@ pub struct CuddleCI {
release_action: Vec<Arc<Mutex<dyn ReleaseAction + Send + Sync>>>, 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 { impl CuddleCI {
pub fn new( pub fn new(
pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>, pr: Arc<Mutex<dyn PullRequestAction + Send + Sync>>,
@ -58,26 +81,36 @@ impl CuddleCI {
.subcommand_required(true) .subcommand_required(true)
.try_get_matches_from(args.into_iter().map(|a| a.into()).collect::<Vec<String>>())?; .try_get_matches_from(args.into_iter().map(|a| a.into()).collect::<Vec<String>>())?;
let mut context = Context::default();
match matches.subcommand() { match matches.subcommand() {
Some((name, args)) => match (name, args) { Some((name, args)) => match (name, args) {
("pr", _args) => { ("pr", _args) => {
eprintln!("starting pr validate"); eprintln!("starting pr validate");
for pr_action in self.pr_action.iter() { 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"); eprintln!("finished pr validate");
} }
("main", _args) => { ("main", _args) => {
eprintln!("starting main validate"); eprintln!("starting main validate");
for main_action in self.main_action.iter() { 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"); eprintln!("finished main validate");
} }
("release", _args) => { ("release", _args) => {
eprintln!("starting release validate"); eprintln!("starting release validate");
for release_action in self.release_action.iter() { 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"); eprintln!("finished release validate");
} }
@ -104,7 +137,7 @@ impl Default for CuddleCI {
#[async_trait] #[async_trait]
pub trait PullRequestAction { 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"); eprintln!("validate pull request: noop");
Ok(()) Ok(())
} }
@ -116,20 +149,20 @@ impl PullRequestAction for DefaultPullRequestAction {}
#[async_trait] #[async_trait]
pub trait MainAction { pub trait MainAction {
async fn execute_main(&self) -> eyre::Result<()>; async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()>;
} }
pub struct DefaultMainAction {} pub struct DefaultMainAction {}
#[async_trait] #[async_trait]
impl MainAction for DefaultMainAction { impl MainAction for DefaultMainAction {
async fn execute_main(&self) -> eyre::Result<()> { async fn execute_main(&self, _ctx: &mut Context) -> eyre::Result<()> {
Ok(()) Ok(())
} }
} }
#[async_trait] #[async_trait]
pub trait ReleaseAction { pub trait ReleaseAction {
async fn execute_release(&self) -> eyre::Result<()> { async fn execute_release(&self, _ctx: &mut Context) -> eyre::Result<()> {
eprintln!("validate release: noop"); eprintln!("validate release: noop");
Ok(()) Ok(())

View File

@ -1,9 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use dagger_cuddle_please::{ use dagger_cuddle_please::{models::CuddlePleaseSrcArgs, DaggerCuddlePlease};
models::CuddlePleaseSrcArgs, DaggerCuddlePlease, DaggerCuddlePleaseAction,
};
use crate::{MainAction, PullRequestAction}; use crate::{Context, MainAction};
pub struct CuddlePlease { pub struct CuddlePlease {
client: dagger_sdk::Query, client: dagger_sdk::Query,
@ -17,7 +15,7 @@ impl CuddlePlease {
#[async_trait] #[async_trait]
impl MainAction for CuddlePlease { 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 client = self.client.clone();
let action = DaggerCuddlePlease::new(client); let action = DaggerCuddlePlease::new(client);

View File

@ -1,7 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use eyre::Context; use eyre::Context;
use crate::{cuddle_file::CuddleFile, MainAction}; use crate::{cli, cuddle_file::CuddleFile, MainAction};
pub struct CuddleReleaser { pub struct CuddleReleaser {
client: dagger_sdk::Query, client: dagger_sdk::Query,
@ -35,7 +35,7 @@ impl CuddleReleaser {
#[async_trait] #[async_trait]
impl MainAction for CuddleReleaser { 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(); let client = self.client.clone();
if self.cuddle_file.deployment.is_none() { if self.cuddle_file.deployment.is_none() {

View File

@ -12,7 +12,7 @@ use crate::{
extensions::CargoBInstallExt, extensions::CargoBInstallExt,
RustServiceStage, RustServiceStage,
}, },
MainAction, PullRequestAction, Context, MainAction, PullRequestAction,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -328,7 +328,7 @@ impl LeptosService {
#[async_trait] #[async_trait]
impl PullRequestAction for LeptosService { 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(); let mut s = self.clone();
s.with_cargo_binstall("latest", ["cargo-leptos"]) s.with_cargo_binstall("latest", ["cargo-leptos"])
@ -341,7 +341,7 @@ impl PullRequestAction for LeptosService {
#[async_trait] #[async_trait]
impl MainAction for LeptosService { 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 mut s = self.clone();
let container = s let container = s

View File

@ -6,7 +6,7 @@ use dagger_sdk::{Container, ContainerWithDirectoryOptsBuilder, HostDirectoryOpts
use crate::{ use crate::{
dagger_middleware::DynMiddleware, dagger_middleware::DynMiddleware,
rust_service::architecture::{Architecture, Os}, rust_service::architecture::{Architecture, Os},
MainAction, PullRequestAction, Context, MainAction, PullRequestAction,
}; };
#[derive(Clone)] #[derive(Clone)]
@ -215,7 +215,7 @@ impl NodeService {
#[async_trait] #[async_trait]
impl PullRequestAction for NodeService { 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?; let release = self.build_release().await?;
release.sync().await?; release.sync().await?;
@ -226,7 +226,7 @@ impl PullRequestAction for NodeService {
#[async_trait] #[async_trait]
impl MainAction for NodeService { 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 container = self.build_release().await?;
let timestamp = std::time::SystemTime::now() let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)

View File

@ -6,6 +6,7 @@ use dagger_sdk::Container;
use futures::{stream, StreamExt}; use futures::{stream, StreamExt};
use crate::{ use crate::{
cli,
dagger_middleware::{DaggerMiddleware, DynMiddleware}, dagger_middleware::{DaggerMiddleware, DynMiddleware},
rust_service::architecture::{Architecture, Os}, rust_service::architecture::{Architecture, Os},
MainAction, PullRequestAction, MainAction, PullRequestAction,
@ -141,7 +142,7 @@ impl RustLib {
#[async_trait] #[async_trait]
impl PullRequestAction for RustLib { 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?; self.build_test().await?;
Ok(()) Ok(())
@ -150,7 +151,7 @@ impl PullRequestAction for RustLib {
#[async_trait] #[async_trait]
impl MainAction for RustLib { 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?; self.build_test().await?;
Ok(()) Ok(())

View File

@ -7,7 +7,7 @@ use futures::{stream, StreamExt};
use crate::{ use crate::{
dagger_middleware::{DaggerMiddleware, DynMiddleware}, dagger_middleware::{DaggerMiddleware, DynMiddleware},
MainAction, PullRequestAction, Context, MainAction, PullRequestAction,
}; };
use self::architecture::{Architecture, Os}; use self::architecture::{Architecture, Os};
@ -325,29 +325,52 @@ impl RustService {
#[async_trait] #[async_trait]
impl PullRequestAction for RustService { 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?; self.build_test().await?;
Ok(()) Ok(())
} }
} }
const IMAGE_TAG: &str = "RUST_SERVICE_IMAGE_TAG";
pub trait RustServiceContext {
fn set_image_tag(&mut self, tag: impl Into<String>) -> eyre::Result<()>;
fn get_image_tag(&self) -> eyre::Result<Option<String>>;
}
impl RustServiceContext for Context {
fn get_image_tag(&self) -> eyre::Result<Option<String>> {
Ok(self.get(IMAGE_TAG).cloned())
}
fn set_image_tag(&mut self, tag: impl Into<String>) -> eyre::Result<()> {
let tag = tag.into();
self.insert(IMAGE_TAG.to_string(), tag);
Ok(())
}
}
#[async_trait] #[async_trait]
impl MainAction for RustService { 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 container = self.build_release().await?;
let timestamp = std::time::SystemTime::now() let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)
.unwrap() .unwrap()
.as_secs(); .as_secs();
container let image_tag = container
.publish(format!( .publish(format!(
"docker.io/kasperhermansen/{}:main-{}", "docker.io/kasperhermansen/{}:main-{}",
self.bin_name, timestamp, self.bin_name, timestamp,
)) ))
.await?; .await?;
ctx.set_image_tag(image_tag)?;
if self.deployment { if self.deployment {
let update_deployments_docker_image = let update_deployments_docker_image =
"docker.io/kasperhermansen/update-deployment:1701123940"; "docker.io/kasperhermansen/update-deployment:1701123940";
@ -443,18 +466,14 @@ pub mod extensions {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use futures::FutureExt;
use crate::{ use crate::rust_service::{
dagger_middleware::middleware, apt::AptExt,
rust_service::{ architecture::{Architecture, Os},
apt::AptExt, cargo_binstall::CargoBInstallExt,
architecture::{Architecture, Os}, clap_sanity_test::ClapSanityTestExt,
cargo_binstall::CargoBInstallExt, mold::MoldActionExt,
clap_sanity_test::ClapSanityTestExt, RustService,
mold::MoldActionExt,
RustService, RustServiceStage,
},
}; };
#[tokio::test] #[tokio::test]