Compare commits
12 Commits
feat/with-
...
82ccdefd93
| Author | SHA1 | Date | |
|---|---|---|---|
|
82ccdefd93
|
|||
| 3e9a840851 | |||
| 455660f1e0 | |||
|
f5ba46186b
|
|||
|
8cf148726a
|
|||
|
e29615cb05
|
|||
|
cdd13283e0
|
|||
|
a3d92cdde3
|
|||
|
e4fc1cc834
|
|||
|
d6af354776
|
|||
|
0524b2e0bf
|
|||
|
5c69c3fa16
|
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -320,6 +320,18 @@ dependencies = [
|
|||||||
"typenum",
|
"typenum",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cuddle-ci"
|
||||||
|
version = "0.2.0"
|
||||||
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"clap",
|
||||||
|
"dagger-sdk",
|
||||||
|
"eyre",
|
||||||
|
"futures",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cuddle-components"
|
name = "cuddle-components"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|||||||
@@ -23,3 +23,7 @@ eyre = "0.6.9"
|
|||||||
tokio = "1.34.0"
|
tokio = "1.34.0"
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
async-trait = "0.1.74"
|
async-trait = "0.1.74"
|
||||||
|
color-eyre = "*"
|
||||||
|
clap = {version = "4", features = ["derive"]}
|
||||||
|
futures = "0.3.29"
|
||||||
|
async-scoped = { version = "0.8.0", features = ["tokio", "use-tokio"] }
|
||||||
|
|||||||
20
crates/cuddle-ci/Cargo.toml
Normal file
20
crates/cuddle-ci/Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "cuddle-ci"
|
||||||
|
version.workspace = true
|
||||||
|
edition.workspace = true
|
||||||
|
license.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
readme.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dagger-sdk.workspace = true
|
||||||
|
eyre.workspace = true
|
||||||
|
clap.workspace = true
|
||||||
|
async-trait.workspace = true
|
||||||
|
futures.workspace = true
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tokio.workspace = true
|
||||||
156
crates/cuddle-ci/src/cli.rs
Normal file
156
crates/cuddle-ci/src/cli.rs
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
use std::{env::Args, sync::Arc};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
pub struct CuddleCI {
|
||||||
|
pr_action: Arc<dyn PullRequestAction + Send + Sync>,
|
||||||
|
main_action: Arc<dyn MainAction + Send + Sync>,
|
||||||
|
release_action: Arc<dyn ReleaseAction + Send + Sync>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CuddleCI {
|
||||||
|
pub fn new(
|
||||||
|
pr: Arc<dyn PullRequestAction + Send + Sync>,
|
||||||
|
main: Arc<dyn MainAction + Send + Sync>,
|
||||||
|
release: Arc<dyn ReleaseAction + Send + Sync>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
pr_action: pr,
|
||||||
|
main_action: main,
|
||||||
|
release_action: release,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_pull_request(&mut self, pr: Arc<dyn PullRequestAction + Send + Sync>) -> &mut Self {
|
||||||
|
self.pr_action = pr;
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_main(&mut self, main: Arc<dyn MainAction + Send + Sync>) -> &mut Self {
|
||||||
|
self.main_action = main;
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_release(&mut self, release: Arc<dyn ReleaseAction + Send + Sync>) -> &mut Self {
|
||||||
|
self.release_action = release;
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn execute(&mut self, args: impl IntoIterator<Item = &str>) -> eyre::Result<()> {
|
||||||
|
let matches = clap::Command::new("cuddle-ci")
|
||||||
|
.about("is a wrapper around common CI actions")
|
||||||
|
.subcommand(clap::Command::new("pr"))
|
||||||
|
.subcommand(clap::Command::new("main"))
|
||||||
|
.subcommand(clap::Command::new("release"))
|
||||||
|
.subcommand_required(true)
|
||||||
|
.try_get_matches_from(args.into_iter())?;
|
||||||
|
|
||||||
|
match matches.subcommand() {
|
||||||
|
Some((name, args)) => match (name, args) {
|
||||||
|
("pr", args) => {
|
||||||
|
eprintln!("starting pr validate");
|
||||||
|
self.pr_action.execute_pull_request().await?;
|
||||||
|
eprintln!("finished pr validate");
|
||||||
|
}
|
||||||
|
("main", args) => {
|
||||||
|
eprintln!("starting main validate");
|
||||||
|
self.main_action.execute_main().await?;
|
||||||
|
eprintln!("finished main validate");
|
||||||
|
}
|
||||||
|
("release", args) => {
|
||||||
|
eprintln!("starting release validate");
|
||||||
|
self.release_action.execute_release().await?;
|
||||||
|
eprintln!("finished release validate");
|
||||||
|
}
|
||||||
|
(command_name, _) => {
|
||||||
|
eyre::bail!("command is not recognized: {}", command_name)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => eyre::bail!("command required a subcommand [pr, main, release] etc."),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for CuddleCI {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new(
|
||||||
|
Arc::new(DefaultPullRequestAction {}),
|
||||||
|
Arc::new(DefaultMainAction {}),
|
||||||
|
Arc::new(DefaultReleaseAction {}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait PullRequestAction {
|
||||||
|
async fn execute_pull_request(&self) -> eyre::Result<()> {
|
||||||
|
eprintln!("validate pull request: noop");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultPullRequestAction {}
|
||||||
|
#[async_trait]
|
||||||
|
impl PullRequestAction for DefaultPullRequestAction {}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait MainAction {
|
||||||
|
async fn execute_main(&self) -> eyre::Result<()> {
|
||||||
|
eprintln!("validate main: noop");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultMainAction {}
|
||||||
|
#[async_trait]
|
||||||
|
impl MainAction for DefaultMainAction {}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait ReleaseAction {
|
||||||
|
async fn execute_release(&self) -> eyre::Result<()> {
|
||||||
|
eprintln!("validate release: noop");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DefaultReleaseAction {}
|
||||||
|
#[async_trait]
|
||||||
|
impl ReleaseAction for DefaultReleaseAction {}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_can_call_default() -> eyre::Result<()> {
|
||||||
|
CuddleCI::default().execute(["cuddle-ci", "pr"]).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_fails_on_no_command() -> eyre::Result<()> {
|
||||||
|
let res = CuddleCI::default().execute(["cuddle-ci"]).await;
|
||||||
|
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_fails_on_wrong_command() -> eyre::Result<()> {
|
||||||
|
let res = CuddleCI::default()
|
||||||
|
.execute(["cuddle-ci", "something"])
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(res.is_err());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
54
crates/cuddle-ci/src/dagger_middleware.rs
Normal file
54
crates/cuddle-ci/src/dagger_middleware.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use dagger_sdk::Container;
|
||||||
|
use std::{future::Future, pin::Pin};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait DaggerMiddleware {
|
||||||
|
async fn handle(
|
||||||
|
&mut self,
|
||||||
|
container: dagger_sdk::Container,
|
||||||
|
) -> eyre::Result<dagger_sdk::Container> {
|
||||||
|
Ok(container)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DaggerMiddlewareFn<F>
|
||||||
|
where
|
||||||
|
F: FnMut(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>>,
|
||||||
|
{
|
||||||
|
pub func: F,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn middleware<F>(func: F) -> Box<DaggerMiddlewareFn<F>>
|
||||||
|
where
|
||||||
|
F: FnMut(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>>,
|
||||||
|
{
|
||||||
|
Box::new(DaggerMiddlewareFn { func })
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl<F> DaggerMiddleware for DaggerMiddlewareFn<F>
|
||||||
|
where
|
||||||
|
F: FnMut(Container) -> Pin<Box<dyn Future<Output = eyre::Result<Container>> + Send>>
|
||||||
|
+ Send
|
||||||
|
+ Sync,
|
||||||
|
{
|
||||||
|
async fn handle(&mut self, container: Container) -> eyre::Result<Container> {
|
||||||
|
// Call the closure stored in the struct
|
||||||
|
(self.func)(container).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use futures::FutureExt;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn can_add_middleware() -> eyre::Result<()> {
|
||||||
|
middleware(|c| async move { Ok(c) }.boxed());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
111
crates/cuddle-ci/src/lib.rs
Normal file
111
crates/cuddle-ci/src/lib.rs
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
pub mod cli;
|
||||||
|
pub use cli::*;
|
||||||
|
|
||||||
|
pub mod dagger_middleware;
|
||||||
|
|
||||||
|
pub mod rust_service {
|
||||||
|
use std::{future::Future, pin::Pin, sync::Arc};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use dagger_sdk::Container;
|
||||||
|
use futures::future::BoxFuture;
|
||||||
|
|
||||||
|
use crate::{dagger_middleware::DaggerMiddleware, MainAction, PullRequestAction};
|
||||||
|
|
||||||
|
pub type DynMiddleware = Box<dyn DaggerMiddleware + Send + Sync>;
|
||||||
|
|
||||||
|
pub enum RustServiceStage {
|
||||||
|
BeforeBase(DynMiddleware),
|
||||||
|
AfterBase(DynMiddleware),
|
||||||
|
BeforeRelease(DynMiddleware),
|
||||||
|
AfterRelease(DynMiddleware),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RustService {
|
||||||
|
client: dagger_sdk::Query,
|
||||||
|
|
||||||
|
base_image: Option<dagger_sdk::Container>,
|
||||||
|
|
||||||
|
stages: Vec<RustServiceStage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<dagger_sdk::Query> for RustService {
|
||||||
|
fn from(value: dagger_sdk::Query) -> Self {
|
||||||
|
Self {
|
||||||
|
client: value,
|
||||||
|
base_image: None,
|
||||||
|
stages: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RustService {
|
||||||
|
pub async fn new() -> eyre::Result<Self> {
|
||||||
|
Ok(Self {
|
||||||
|
client: dagger_sdk::connect().await?,
|
||||||
|
base_image: None,
|
||||||
|
stages: Vec::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_base_image(&mut self, base: dagger_sdk::Container) -> &mut Self {
|
||||||
|
self.base_image = Some(base);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_stage(&mut self, stage: RustServiceStage) -> &mut Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_sqlx(&mut self) -> &mut Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn build_release(&self) -> eyre::Result<Vec<Container>> {
|
||||||
|
Ok(Vec::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl PullRequestAction for RustService {
|
||||||
|
async fn execute_pull_request(&self) -> eyre::Result<()> {
|
||||||
|
self.build_release().await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl MainAction for RustService {
|
||||||
|
async fn execute_main(&self) -> eyre::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use futures::FutureExt;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
dagger_middleware::middleware,
|
||||||
|
rust_service::{RustService, RustServiceStage},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn can_build_rust() -> eyre::Result<()> {
|
||||||
|
let client = dagger_sdk::connect().await?;
|
||||||
|
|
||||||
|
RustService::from(client.clone())
|
||||||
|
.with_base_image(client.container().from("rustlang/rust:nightly"))
|
||||||
|
.with_sqlx()
|
||||||
|
.add_stage(RustServiceStage::BeforeBase(middleware(|c| async move { Ok(c) }.boxed())))
|
||||||
|
.build_release()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,6 +44,9 @@ impl RustBuild {
|
|||||||
.from(rust_version.to_string())
|
.from(rust_version.to_string())
|
||||||
.with_exec(vec!["rustup", "target", "add", &target.to_string()])
|
.with_exec(vec!["rustup", "target", "add", &target.to_string()])
|
||||||
.with_exec(vec!["apt", "update"])
|
.with_exec(vec!["apt", "update"])
|
||||||
|
.with_exec(vec!["wget", "https://github.com/rui314/mold/releases/latest/download/mold-2.3.3-x86_64-linux.tar.gz"])
|
||||||
|
.with_exec("tar -xvf mold-2.3.3-x86_64-linux.tar.gz".split_whitespace().collect())
|
||||||
|
.with_exec("mv mold-2.3.3-x86_64-linux/bin/mold /usr/bin/mold".split_whitespace().collect())
|
||||||
.with_exec(deps);
|
.with_exec(deps);
|
||||||
|
|
||||||
let target_cache = self.client.cache_volume(format!(
|
let target_cache = self.client.cache_volume(format!(
|
||||||
@@ -110,6 +113,8 @@ impl RustBuild {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let bin = build_container
|
let bin = build_container
|
||||||
|
.with_env_variable("SQLX_OFFLINE", "true")
|
||||||
|
.with_exec(vec!["cargo", "clean"])
|
||||||
.with_exec(vec![
|
.with_exec(vec![
|
||||||
"cargo",
|
"cargo",
|
||||||
"build",
|
"build",
|
||||||
|
|||||||
Reference in New Issue
Block a user