feat: handle for cuddle_file
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-04-05 22:45:25 +02:00
parent 99bc420f71
commit 924bcf8c8c
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 173 additions and 11 deletions

23
Cargo.lock generated
View File

@ -358,6 +358,7 @@ dependencies = [
"dagger-sdk",
"eyre",
"futures",
"pretty_assertions",
"serde",
"serde_json",
"serde_yaml",
@ -498,6 +499,12 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "digest"
version = "0.10.7"
@ -1247,6 +1254,16 @@ dependencies = [
"winapi",
]
[[package]]
name = "pretty_assertions"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66"
dependencies = [
"diff",
"yansi",
]
[[package]]
name = "proc-macro2"
version = "1.0.79"
@ -2253,3 +2270,9 @@ dependencies = [
"linux-raw-sys",
"rustix",
]
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View File

@ -27,4 +27,10 @@ chrono = {version = "0.4.37"}
toml = "0.8.12"
[dev-dependencies]
pretty_assertions = "1.4.0"
tokio.workspace = true
[features]
default = []
dagger = []
integration = []

View File

@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, path::PathBuf};
use serde::{Deserialize, Serialize};
@ -6,6 +6,33 @@ use serde::{Deserialize, Serialize};
pub struct CuddleFile {
pub vars: CuddleVars,
pub deployment: Option<CuddleDeployment>,
pub components: Option<CuddleComponents>,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct CuddleComponents {
pub database: Option<CuddleDatabase>,
pub assets: Option<CuddleAssets>,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct CuddleAssets {
pub volumes: Option<Vec<CuddleAssetInclude>>,
pub clean: Option<bool>,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct CuddleAssetInclude {
pub from: PathBuf,
pub to: PathBuf,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum CuddleDatabase {
Enabled(bool),
Default {},
Values { migrations: PathBuf },
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
@ -54,13 +81,7 @@ impl CuddleFile {
#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use crate::cuddle_file::{
CuddleCluster, CuddleClusters, CuddleDeploymentCluster, CuddleDeploymentEnv, CuddleVars,
};
use super::CuddleFile;
use super::*;
#[test]
fn parse_file() {
@ -82,6 +103,13 @@ deployment:
prod:
clusters:
- clank_prod
components:
database: true
assets:
volumes:
- from: somewhere
to: somewhere-else
scripts:
render:
@ -119,11 +147,52 @@ scripts:
clusters: Some(CuddleClusters(clusters)),
},
deployment: Some(crate::cuddle_file::CuddleDeployment {
registry: "git@git.front.kjuulh.io:kjuulh/clank-cluster".into(),
registry: "git@git.front.kjuulh.io:kjuulh/clank-clusters".into(),
env: CuddleDeploymentEnv(deployment),
}),
components: Some(CuddleComponents {
database: Some(CuddleDatabase::Enabled(true)),
assets: Some(CuddleAssets {
volumes: Some(vec![CuddleAssetInclude {
from: "somewhere".into(),
to: "somewhere-else".into(),
}]),
clean: None,
}),
}),
};
assert_eq!(expected, res)
pretty_assertions::assert_eq!(expected, res)
}
#[test]
fn cuddle_database_default() {
let cuddle_file = r#"
base: "git@git.front.kjuulh.io:kjuulh/cuddle-base.git"
vars:
service: "infrastructure-example"
registry: kasperhermansen
components:
database: {}
"#;
let res = CuddleFile::parse_cuddle_file(cuddle_file).expect("to parse file");
let expected = CuddleFile {
vars: CuddleVars {
service: "infrastructure-example".into(),
registry: "kasperhermansen".into(),
clusters: None,
},
deployment: None,
components: Some(CuddleComponents {
database: Some(CuddleDatabase::Default {}),
assets: None,
}),
};
pretty_assertions::assert_eq!(expected, res)
}
}

View File

@ -442,6 +442,7 @@ mod cargo_binstall;
mod cargo_clean;
mod clap_sanity_test;
mod cuddle_cli;
mod cuddle_file;
mod docker_cache;
mod docker_cli;
mod kubectl;
@ -458,6 +459,7 @@ pub mod extensions {
pub use super::cargo_clean::*;
pub use super::clap_sanity_test::*;
pub use super::cuddle_cli::*;
pub use super::cuddle_file::*;
pub use super::docker_cache::*;
pub use super::docker_cli::*;
pub use super::kubectl::*;
@ -480,6 +482,7 @@ mod test {
};
#[tokio::test]
#[cfg(any(feature = "dagger", feature = "integration"))]
async fn test_can_build_rust() -> eyre::Result<()> {
let client = dagger_sdk::connect().await?;

View File

@ -1,7 +1,7 @@
use std::{path::PathBuf, sync::Arc};
use async_trait::async_trait;
use dagger_sdk::{Container};
use dagger_sdk::Container;
use crate::{dagger_middleware::DaggerMiddleware, leptos_service::LeptosService};

View File

@ -0,0 +1,61 @@
use std::path::PathBuf;
use crate::cuddle_file::{CuddleDatabase, CuddleFile};
use super::{
extensions::{AssetsExt, CargoCleanExt, SqlxExt},
RustService,
};
#[derive(Default)]
pub struct CuddleFileAction {}
impl CuddleFileAction {
pub fn new() -> Self {
Self {}
}
}
pub trait CuddleFileExt {
fn with_cuddle_file(&mut self, cuddle_file: &CuddleFile) -> &mut Self;
}
impl CuddleFileExt for RustService {
fn with_cuddle_file(&mut self, cuddle_file: &CuddleFile) -> &mut Self {
let mut s = self
.with_bin_name(&cuddle_file.vars.service)
.with_deployment(false);
if let Some(components) = &cuddle_file.components {
s = if let Some(database) = &components.database {
match database {
CuddleDatabase::Enabled(true) => s.with_sqlx().with_sqlx_migrations(
PathBuf::from("crates")
.join(&cuddle_file.vars.service)
.join("migrations/crdb"),
),
CuddleDatabase::Values { migrations } => {
s.with_sqlx().with_sqlx_migrations(migrations)
}
CuddleDatabase::Enabled(false) | CuddleDatabase::Default {} => s,
}
} else {
s
};
if let Some(assets) = &components.assets {
if let Some(true) = assets.clean {
s = s.with_cargo_clean()
}
if let Some(volumes) = &assets.volumes {
let mappings = volumes.iter().cloned().map(|val| (val.from, val.to));
s = s.with_assets(mappings);
}
}
}
s
}
}