feat: extract arch
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-11-26 22:46:34 +01:00
parent 7a1ad63b57
commit 3c28b30f8f
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 67 additions and 29 deletions

View File

@ -1,6 +1,7 @@
use async_trait::async_trait; use async_trait::async_trait;
use dagger_sdk::Container; use dagger_sdk::Container;
use std::{future::Future, pin::Pin}; use std::{future::Future, pin::Pin};
pub type DynMiddleware = Box<dyn DaggerMiddleware + Send + Sync>;
#[async_trait] #[async_trait]
pub trait DaggerMiddleware { pub trait DaggerMiddleware {

View File

@ -5,9 +5,12 @@ use dagger_rust::source::RustSource;
use dagger_sdk::Container; use dagger_sdk::Container;
use futures::{stream, StreamExt}; use futures::{stream, StreamExt};
use crate::{dagger_middleware::DaggerMiddleware, MainAction, PullRequestAction}; use crate::{
dagger_middleware::{DaggerMiddleware, DynMiddleware},
MainAction, PullRequestAction,
};
pub type DynMiddleware = Box<dyn DaggerMiddleware + Send + Sync>; use self::architecture::{Architecture, Os};
pub enum RustServiceStage { pub enum RustServiceStage {
BeforeDeps(DynMiddleware), BeforeDeps(DynMiddleware),
@ -30,6 +33,8 @@ pub struct RustService {
source: Option<PathBuf>, source: Option<PathBuf>,
crates: Vec<String>, crates: Vec<String>,
bin_name: String, bin_name: String,
arch: Option<Architecture>,
os: Option<Os>,
} }
impl From<dagger_sdk::Query> for RustService { impl From<dagger_sdk::Query> for RustService {
@ -42,6 +47,8 @@ impl From<dagger_sdk::Query> for RustService {
source: None, source: None,
crates: Vec::new(), crates: Vec::new(),
bin_name: String::new(), bin_name: String::new(),
arch: None,
os: None,
} }
} }
} }
@ -56,6 +63,8 @@ impl RustService {
source: None, source: None,
crates: Vec::new(), crates: Vec::new(),
bin_name: String::new(), bin_name: String::new(),
arch: None,
os: None,
}) })
} }
@ -92,12 +101,44 @@ impl RustService {
self self
} }
pub fn with_arch(&mut self, arch: Architecture) -> &mut Self {
self.arch = Some(arch);
self
}
pub fn with_os(&mut self, os: Os) -> &mut Self {
self.os = Some(os);
self
}
fn get_src(&self) -> PathBuf { fn get_src(&self) -> PathBuf {
self.source self.source
.clone() .clone()
.unwrap_or(std::env::current_dir().unwrap()) .unwrap_or(std::env::current_dir().unwrap())
} }
fn get_arch(&self) -> Architecture {
self.arch
.clone()
.unwrap_or_else(|| match std::env::consts::ARCH {
"x86" | "x86_64" | "amd64" => Architecture::Amd64,
"arm" => Architecture::Arm64,
arch => panic!("unsupported architecture: {arch}"),
})
}
fn get_os(&self) -> Os {
self.os
.clone()
.unwrap_or_else(|| match std::env::consts::OS {
"linux" => Os::Linux,
"macos" => Os::MacOS,
os => panic!("unsupported os: {os}"),
})
}
async fn run_stage( async fn run_stage(
&self, &self,
stages: impl IntoIterator<Item = &Box<dyn DaggerMiddleware + Send + Sync>>, stages: impl IntoIterator<Item = &Box<dyn DaggerMiddleware + Send + Sync>>,
@ -278,24 +319,32 @@ impl MainAction for RustService {
} }
pub mod architecture { pub mod architecture {
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum Architecture { pub enum Architecture {
Amd64, Amd64,
Arm64, Arm64,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum Os { pub enum Os {
Linux, Linux,
MacOS, MacOS,
} }
} }
pub mod apt; mod apt;
pub mod cargo_binstall; mod cargo_binstall;
pub mod clap_sanity_test; mod clap_sanity_test;
pub mod mold; mod mold;
pub mod sqlx; mod sqlx;
pub mod extensions {
pub use super::apt::*;
pub use super::cargo_binstall::*;
pub use super::clap_sanity_test::*;
pub use super::mold::*;
pub use super::sqlx::*;
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
@ -320,12 +369,14 @@ mod test {
let root_dir = std::path::PathBuf::from("../../").canonicalize()?; let root_dir = std::path::PathBuf::from("../../").canonicalize()?;
let container = RustService::from(client.clone()) let container = RustService::from(client.clone())
.with_arch(Architecture::Amd64)
.with_os(Os::Linux)
.with_apt(&["git"]) .with_apt(&["git"])
.with_cargo_binstall(Architecture::Amd64, Os::Linux, "latest", ["sqlx-cli"]) .with_cargo_binstall("latest", ["sqlx-cli"])
.with_source(root_dir) .with_source(root_dir)
.with_bin_name("ci") .with_bin_name("ci")
.with_crates(["crates/*", "examples/*", "ci"]) .with_crates(["crates/*", "examples/*", "ci"])
.with_mold(Architecture::Amd64, Os::Linux, "2.3.3") .with_mold("2.3.3")
.with_stage(RustServiceStage::BeforeDeps(middleware(|c| { .with_stage(RustServiceStage::BeforeDeps(middleware(|c| {
async move { async move {
// Noop // Noop

View File

@ -83,8 +83,6 @@ impl DaggerMiddleware for CargoBInstall {
pub trait CargoBInstallExt { pub trait CargoBInstallExt {
fn with_cargo_binstall( fn with_cargo_binstall(
&mut self, &mut self,
arch: Architecture,
os: Os,
version: impl Into<String>, version: impl Into<String>,
crates: impl IntoIterator<Item = impl Into<String>>, crates: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self { ) -> &mut Self {
@ -95,15 +93,13 @@ pub trait CargoBInstallExt {
impl CargoBInstallExt for RustService { impl CargoBInstallExt for RustService {
fn with_cargo_binstall( fn with_cargo_binstall(
&mut self, &mut self,
arch: Architecture,
os: Os,
version: impl Into<String>, version: impl Into<String>,
crates: impl IntoIterator<Item = impl Into<String>>, crates: impl IntoIterator<Item = impl Into<String>>,
) -> &mut Self { ) -> &mut Self {
let crates: Vec<String> = crates.into_iter().map(|s| s.into()).collect(); let crates: Vec<String> = crates.into_iter().map(|s| s.into()).collect();
self.with_stage(super::RustServiceStage::BeforeDeps( self.with_stage(super::RustServiceStage::BeforeDeps(
Box::new(CargoBInstall::new(arch, os, version, crates)) Box::new(CargoBInstall::new(self.get_arch(), self.get_os(), version, crates))
)) ))
} }
} }

View File

@ -88,25 +88,15 @@ impl DaggerMiddleware for MoldInstall {
} }
pub trait MoldActionExt { pub trait MoldActionExt {
fn with_mold( fn with_mold(&mut self, version: impl Into<String>) -> &mut Self {
&mut self,
architecture: Architecture,
os: Os,
version: impl Into<String>,
) -> &mut Self {
self self
} }
} }
impl MoldActionExt for RustService { impl MoldActionExt for RustService {
fn with_mold( fn with_mold(&mut self, version: impl Into<String>) -> &mut Self {
&mut self,
architecture: Architecture,
os: Os,
version: impl Into<String>,
) -> &mut Self {
self.with_stage(super::RustServiceStage::AfterDeps( self.with_stage(super::RustServiceStage::AfterDeps(
Box::new(MoldInstall::new(architecture, os, version)) Box::new(MoldInstall::new(self.get_arch(), self.get_os(), version))
)) ))
} }
} }