dagger-components/crates/cuddle-ci/src/rust_service/assets.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

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<PathBuf>,
}
impl Assets {
pub fn new(client: dagger_sdk::Query) -> Self {
Self {
client,
assets: Vec::default(),
}
}
fn with_folders(mut self, folders: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
let mut folders = folders.into_iter().map(|f| f.into()).collect::<Vec<_>>();
self.assets.append(&mut folders);
self
}
}
#[async_trait]
impl DaggerMiddleware for Assets {
async fn handle(&self, container: Container) -> eyre::Result<Container> {
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<Item = impl Into<PathBuf>>) -> &mut Self {
self
}
}
impl AssetsExt for RustService {
fn with_assets(&mut self, folders: impl IntoIterator<Item = impl Into<PathBuf>>) -> &mut Self {
self.with_stage(super::RustServiceStage::AfterPackage(Arc::new(
Assets::new(self.client.clone()).with_folders(folders),
)))
}
}