feat: define minimal app context for cli use

This commit is contained in:
Aravinth Manivannan 2023-10-04 18:33:24 +05:30
parent db161fe6f0
commit 3f4b8d363e
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 89 additions and 112 deletions

View file

@ -26,95 +26,6 @@ pub trait FullAppContext: std::marker::Send + std::marker::Sync + CloneFullAppCt
fn port(&self) -> u32;
}
pub trait MinAppContext: std::marker::Send + std::marker::Sync {
fn docker_(&self) -> Arc<dyn DockerLike>;
fn results_(&self) -> &ResultStore;
fn port_(&self) -> u32;
}
impl MinAppContext for Arc<dyn FullAppContext> {
fn docker_(&self) -> Arc<dyn DockerLike> {
self.docker()
}
fn results_(&self) -> &ResultStore {
self.results()
}
fn port_(&self) -> u32 {
self.port()
}
}
#[derive(Clone)]
pub struct Ctx {
settings: Settings,
db: Database,
results: ResultStore,
docker: Arc<dyn DockerLike>,
}
impl FullAppContext for Ctx {
fn settings(&self) -> &Settings {
&self.settings
}
fn db(&self) -> &Database {
&self.db
}
fn docker(&self) -> Arc<dyn DockerLike> {
self.docker.clone()
}
fn results(&self) -> &ResultStore {
&self.results
}
fn port(&self) -> u32 {
self.settings.server.port
}
}
impl MinAppContext for Ctx {
fn docker_(&self) -> Arc<dyn DockerLike> {
self.docker()
}
fn results_(&self) -> &ResultStore {
self.results()
}
fn port_(&self) -> u32 {
self.port()
}
}
//impl MinAppContext for Arc<dyn MinAppContext> {
// fn docker_(&self) -> Arc<dyn DockerLike>{
// self.docker_()
// }
// fn results_(&self) -> &ResultStore {
// self.results_()
// }
// fn port_(&self) -> u32 {
// self.port_()
// }
//}
//
//impl FullAppContext for Arc<dyn FullAppContext> {
// fn settings(&self) -> &Settings {
// self.settings()
// }
// fn db(&self) -> &Database {
// self.db()
// }
//
// fn docker(&self) -> Arc<dyn DockerLike>{
// self.docker()
// }
// fn results(&self) -> &ResultStore {
// self.results()
// }
// fn port(&self) -> u32 {
// self.port()
// }
//
//
//}
pub trait CloneFullAppCtx {
fn clone_f(&self) -> Box<dyn FullAppContext>;
}
@ -134,28 +45,65 @@ impl Clone for Box<dyn FullAppContext> {
}
}
//pub trait CloneMinAppContext {
// fn clone_fi(&self) -> Box<dyn MinAppContext>;
//}
//
//impl<T> CloneMinAppContext for T
//where
// T: CloneMinAppContext + Clone + 'static,
//{
// fn clone_fi(&self) -> Box<dyn MinAppContext> {
// Box::new(self.clone())
// }
//}
//
//impl Clone for Box<dyn MinAppContext> {
// fn clone(&self) -> Self {
// (**self).clone_fi()
// }
//}
pub trait MinAppContext: std::marker::Send + std::marker::Sync {
fn docker_(&self) -> Arc<dyn DockerLike>;
fn results_(&self) -> &ResultStore;
fn port_(&self) -> u32;
}
impl MinAppContext for Arc<dyn FullAppContext> {
fn docker_(&self) -> Arc<dyn DockerLike> {
self.docker()
}
fn results_(&self) -> &ResultStore {
self.results()
}
fn port_(&self) -> u32 {
self.port()
}
}
#[derive(Clone)]
pub struct DaemonCtx {
settings: Settings,
db: Database,
results: ResultStore,
docker: Arc<dyn DockerLike>,
}
impl FullAppContext for DaemonCtx {
fn settings(&self) -> &Settings {
&self.settings
}
fn db(&self) -> &Database {
&self.db
}
fn docker(&self) -> Arc<dyn DockerLike> {
self.docker.clone()
}
fn results(&self) -> &ResultStore {
&self.results
}
fn port(&self) -> u32 {
self.settings.server.port
}
}
impl MinAppContext for DaemonCtx {
fn docker_(&self) -> Arc<dyn DockerLike> {
self.docker()
}
fn results_(&self) -> &ResultStore {
self.results()
}
fn port_(&self) -> u32 {
self.port()
}
}
pub type ResultStore = Arc<RwLock<HashMap<String, Sender<CResult>>>>;
impl Ctx {
impl DaemonCtx {
pub async fn new(settings: Settings) -> Self {
let results = HashMap::default();
let results = Arc::new(RwLock::new(results));
@ -168,3 +116,32 @@ impl Ctx {
}
}
}
#[derive(Clone)]
pub struct CliCtx {
results: ResultStore,
docker: Arc<dyn DockerLike>,
}
impl MinAppContext for CliCtx {
fn docker_(&self) -> Arc<dyn DockerLike> {
self.docker.clone()
}
fn results_(&self) -> &ResultStore {
&self.results
}
fn port_(&self) -> u32 {
29130
}
}
impl CliCtx {
pub fn new() -> Self {
let results = HashMap::default();
let results = Arc::new(RwLock::new(results));
Self {
results,
docker: Arc::new(Docker::new()),
}
}
}

View file

@ -59,14 +59,14 @@ mod tests {
use super::*;
use crate::docker_compose::DockerCompose;
use crate::{AppMinCtx, Ctx, Settings};
use crate::{AppMinCtx, DaemonCtx, Settings};
use crate::complaince::suite::Test;
#[actix_rt::test]
async fn launch_init_containers_works() {
let settings = Settings::new().unwrap();
let ctx = AppMinCtx::new(Arc::new(Ctx::new(settings.clone()).await));
let ctx = AppMinCtx::new(Arc::new(DaemonCtx::new(settings.clone()).await));
// let base_dir = Path::new(&ctx.settings.repository.base_dir);
// let control = base_dir.join("control");

View file

@ -172,7 +172,7 @@ mod tests {
use crate::complaince::result::Result as CResult;
use crate::complaince::suite::Test;
use crate::{AppMinCtx, Ctx, Settings};
use crate::{AppMinCtx, DaemonCtx, Settings};
use std::sync::Arc;
@ -240,7 +240,7 @@ mod tests {
const LOGS: &str = "SUITE RUNNER LOG STRING";
let settings = Settings::new().unwrap();
let ctx = Ctx::new(settings.clone()).await;
let ctx = DaemonCtx::new(settings.clone()).await;
// ctx.docker_ = Arc::new(Testdocker_::new());
let ctx = crate::AppFullCtx::new(Arc::new(ctx));