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 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(())

View File

@ -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);

View File

@ -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() {

View File

@ -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

View File

@ -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)

View File

@ -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(())

View File

@ -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<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]
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::{
use crate::rust_service::{
apt::AptExt,
architecture::{Architecture, Os},
cargo_binstall::CargoBInstallExt,
clap_sanity_test::ClapSanityTestExt,
mold::MoldActionExt,
RustService, RustServiceStage,
},
RustService,
};
#[tokio::test]