From 320ff343e613bdf7f5760b71a56ee1ee092a0e64 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 28 Nov 2023 10:12:19 +0100 Subject: [PATCH] feat: with assets Signed-off-by: kjuulh --- crates/cuddle-ci/src/rust_service.rs | 2 + crates/cuddle-ci/src/rust_service/assets.rs | 56 +++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 crates/cuddle-ci/src/rust_service/assets.rs diff --git a/crates/cuddle-ci/src/rust_service.rs b/crates/cuddle-ci/src/rust_service.rs index 4f413c2..9b784b3 100644 --- a/crates/cuddle-ci/src/rust_service.rs +++ b/crates/cuddle-ci/src/rust_service.rs @@ -403,6 +403,7 @@ pub mod architecture { mod apt; mod apt_ca_certificates; +mod assets; mod cargo_binstall; mod cargo_clean; mod clap_sanity_test; @@ -412,6 +413,7 @@ mod sqlx; pub mod extensions { pub use super::apt::*; pub use super::apt_ca_certificates::*; + pub use super::assets::*; pub use super::cargo_binstall::*; pub use super::cargo_clean::*; pub use super::clap_sanity_test::*; diff --git a/crates/cuddle-ci/src/rust_service/assets.rs b/crates/cuddle-ci/src/rust_service/assets.rs new file mode 100644 index 0000000..58245c2 --- /dev/null +++ b/crates/cuddle-ci/src/rust_service/assets.rs @@ -0,0 +1,56 @@ +use std::{path::PathBuf, sync::Arc}; + +use async_trait::async_trait; +use dagger_sdk::{Container, HostDirectoryOpts, HostDirectoryOptsBuilder}; + +use crate::dagger_middleware::DaggerMiddleware; + +use super::RustService; + +pub struct Assets { + client: dagger_sdk::Query, + assets: Vec, +} + +impl Assets { + pub fn new(client: dagger_sdk::Query) -> Self { + Self { + client, + assets: Vec::default(), + } + } + + fn with_folders(mut self, folders: impl IntoIterator>) -> Self { + let mut folders = folders.into_iter().map(|f| f.into()).collect::>(); + self.assets.append(&mut folders); + + self + } +} + +#[async_trait] +impl DaggerMiddleware for Assets { + async fn handle(&self, container: Container) -> eyre::Result { + let container = self.assets.iter().fold(container, |container, asset_path| { + let rel_path = asset_path.display().to_string(); + let path = self.client.host().directory(&rel_path); + container.with_directory(&rel_path, path) + }); + + Ok(container) + } +} + +pub trait AssetsExt { + fn with_assets(&mut self, folders: impl IntoIterator>) -> &mut Self { + self + } +} + +impl AssetsExt for RustService { + fn with_assets(&mut self, folders: impl IntoIterator>) -> &mut Self { + self.with_stage(super::RustServiceStage::AfterPackage(Arc::new( + Assets::new(self.client.clone()).with_folders(folders), + ))) + } +}