From 75a6dca3fc860c4149d47e6715b9b5dfd4ea3b7b Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Sat, 11 May 2024 08:51:37 +0200 Subject: [PATCH] the state before focusing on mize running on all platforms --- Cargo.toml | 2 ++ src/cli/mod.rs | 16 ----------- src/cli/{print.rs => sub.rs} | 0 src/instance/mod.rs | 51 +++++++++++------------------------- src/item.rs | 4 ++- src/itemstore.rs | 5 ++-- src/main.rs | 35 ++++++++++++++++++++----- 7 files changed, 52 insertions(+), 61 deletions(-) delete mode 100644 src/cli/mod.rs rename src/cli/{print.rs => sub.rs} (100%) diff --git a/Cargo.toml b/Cargo.toml index b048cb3..70b4a76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,3 +28,5 @@ env_logger = "0.10.1" daemonize = "0.5.0" nix = { version = "0.27.1", features = ["signal"] } serde_cbor = "0.11.2" + + diff --git a/src/cli/mod.rs b/src/cli/mod.rs deleted file mode 100644 index aac64fc..0000000 --- a/src/cli/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ - -pub mod daemon; -pub mod serve; -pub mod get; -pub mod set; -pub mod mount; -pub mod print; -pub mod call; - -pub use daemon::daemon; -pub use serve::serve; -pub use get::get; -pub use set::set; -pub use mount::mount; -pub use print::print; -pub use call::call; diff --git a/src/cli/print.rs b/src/cli/sub.rs similarity index 100% rename from src/cli/print.rs rename to src/cli/sub.rs diff --git a/src/instance/mod.rs b/src/instance/mod.rs index 4ff46c6..95ad691 100644 --- a/src/instance/mod.rs +++ b/src/instance/mod.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use std::fs::create_dir; use colored::Colorize; use log::{trace, debug, info, warn, error}; +use serde_json::json; use tokio::net::{UnixListener, UnixStream}; use uuid::Uuid; use std::fs::File; @@ -12,7 +13,7 @@ use nix::unistd::Pid; use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait}; use crate::itemstore::{Itemstore, self}; -use crate::item::{Item, MizeId, ItemGeneral}; +use crate::item::{Item, MizeId, ItemRef}; pub mod peer; pub mod connection; @@ -25,6 +26,7 @@ static TCP_SOCKET_CHANNEL_SIZE: usize = 200; static UPDATE_CHANNEL_SIZE: usize = 200; +/// The Instance type is the heart of the mize system pub struct Instance { store: Itemstore, peers: Vec, @@ -71,41 +73,18 @@ impl Instance { }; if !instance.store.has_item(MizeId::main(1)).await? { - // create first item ... which is this instance - let mut instance_item = ItemGeneral { - id: MizeId::main(1), - kind: vec!["instance".to_owned()], - value: Vec::new(), - map: HashMap::new(), - }; - instance_item.map.insert("daemon".to_owned(), MizeId::main(1).join("daemon")); - instance.store.set_item(Item::General(instance_item)).await?; - - let mut daemon_item = ItemGeneral { - id: MizeId::main(1).join("daemon"), - kind: vec!["instance/daemon".to_owned()], - value: Vec::new(), - map: HashMap::new(), - }; - daemon_item.map.insert("log".to_owned(), MizeId::main(1).join("daemon").join("log")); - daemon_item.map.insert("pid".to_owned(), MizeId::main(1).join("daemon").join("pid")); - instance.store.set_item(Item::General(daemon_item)).await?; - - let log_item = ItemGeneral { - id: MizeId::main(1).join("daemon").join("log"), - kind: vec!["blob".to_owned()], - value: Vec::new(), - map: HashMap::new(), - }; - instance.store.set_item(Item::General(log_item)).await?; - - let pid_item = ItemGeneral { - id: MizeId::main(1).join("daemon").join("pid"), - kind: vec!["int".to_owned()], - value: Vec::new(), - map: HashMap::new(), - }; - instance.store.set_item(Item::General(pid_item)).await?; + instance.store.create_item_from_json(json!({ + "item": { + "kind": "instance", + }, + "daemon": { + "log": "", + "pid": "", + "item": { + "kind": "instance/daemon", + }, + }, + })); }; return Ok(instance); diff --git a/src/item.rs b/src/item.rs index b605661..ded7b17 100644 --- a/src/item.rs +++ b/src/item.rs @@ -3,8 +3,10 @@ use std::{collections::HashMap, path::PathBuf, io::Cursor, fmt::Display}; use serde_cbor::Value as CborValue; use std::fs::File; use tokio::sync::Mutex; +use std::rc::Rc; use crate::error::{MizeError, MizeResult, IntoMizeResult}; +use crate::itemstore::Itemstore; #[derive(Debug)] @@ -178,7 +180,7 @@ impl ItemRef { todo!() } - fn write_to_path(&self, path: PathBuf) -> MizeResult<()> { + async fn write_to_path(&self, path: PathBuf) -> MizeResult<()> { let store = self.store.lock().await; for (key, value) in self.map.iter() { // TODO right here!!!! diff --git a/src/itemstore.rs b/src/itemstore.rs index 5767464..cbbad65 100644 --- a/src/itemstore.rs +++ b/src/itemstore.rs @@ -4,8 +4,9 @@ use std::path::{PathBuf, Path}; use std::collections::HashMap; use std::io::{Read, Write}; use colored::Colorize; +use std::rc::Rc; -use crate::item::{Item, MizeId, ItemCbor, ItemDescriptor}; +use crate::item::{Item, MizeId, ItemCbor, ItemRef}; use crate::error::{MizeError, MizeResult, IntoMizeResult}; pub enum Itemstore { @@ -146,7 +147,7 @@ impl FolderItemstore { final_path.push(id_string[..2].to_owned()); }; - return Ok(Item::Descriptor(ItemDescriptor { path: final_path, id})); + return Ok(Item::Descriptor(ItemRef { path: final_path, id})); } async fn set_item(&self, item: Item) -> MizeResult<()> { diff --git a/src/main.rs b/src/main.rs index b7b1592..d9b2fe6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,24 @@ use std::env; use log::{trace, debug, info, warn, error}; -mod cli; +mod cli { + pub mod daemon; + pub mod serve; + pub mod get; + pub mod set; + pub mod mount; + pub mod print; + pub mod call; + + pub use daemon::daemon; + pub use serve::serve; + pub use get::get; + pub use set::set; + pub use mount::mount; + pub use print::print; + pub use call::call; +} + mod error; // mod types; mod instance; @@ -24,11 +41,17 @@ mod item; mod itemstore; mod proto; +#[cfg(test)] +mod test; + static APPNAME: &str = "mize"; static DEFAULT_LOG_LEVEL: LevelFilter = LevelFilter::Warn; #[tokio::main] async fn main() { + // welcome to the mize source code + // this is the main entry point for platforms with an os (eg: Linux, MacOS, Windows, BSD, ...) + // Also let's try to make the most best documented code ever. (eventually) so it's fun to work on let cli_matches = cli_matches(); @@ -55,8 +78,8 @@ async fn main() { // mi set Some(("set", sub_matches)) => cli::set(sub_matches).await, - // mi print - Some(("print", sub_matches)) => cli::print(sub_matches).await, + // mi sub + Some(("sub", sub_matches)) => cli::sub(sub_matches).await, // mi call Some(("call", sub_matches)) => cli::call(sub_matches).await, @@ -307,10 +330,10 @@ fn cli_matches() -> clap::ArgMatches { .arg(Arg::new("id").help("The id or path to set")) ) .subcommand( - Command::new("print") - .aliases(["p"]) + Command::new("sub") + .aliases(["su"]) .arg(&store_arg) - .arg(Arg::new("id").help("The id or path to print")) + .arg(Arg::new("id").help("The id or path to sub to and show")) ) .subcommand( Command::new("call")