the state before focusing on mize running on all platforms

This commit is contained in:
Sebastian Moser
2024-05-11 08:51:37 +02:00
parent 831125bdf0
commit 75a6dca3fc
7 changed files with 52 additions and 61 deletions

View File

@@ -28,3 +28,5 @@ env_logger = "0.10.1"
daemonize = "0.5.0" daemonize = "0.5.0"
nix = { version = "0.27.1", features = ["signal"] } nix = { version = "0.27.1", features = ["signal"] }
serde_cbor = "0.11.2" serde_cbor = "0.11.2"

View File

@@ -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;

View File

@@ -3,6 +3,7 @@ use std::path::PathBuf;
use std::fs::create_dir; use std::fs::create_dir;
use colored::Colorize; use colored::Colorize;
use log::{trace, debug, info, warn, error}; use log::{trace, debug, info, warn, error};
use serde_json::json;
use tokio::net::{UnixListener, UnixStream}; use tokio::net::{UnixListener, UnixStream};
use uuid::Uuid; use uuid::Uuid;
use std::fs::File; use std::fs::File;
@@ -12,7 +13,7 @@ use nix::unistd::Pid;
use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait}; use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait};
use crate::itemstore::{Itemstore, self}; use crate::itemstore::{Itemstore, self};
use crate::item::{Item, MizeId, ItemGeneral}; use crate::item::{Item, MizeId, ItemRef};
pub mod peer; pub mod peer;
pub mod connection; pub mod connection;
@@ -25,6 +26,7 @@ static TCP_SOCKET_CHANNEL_SIZE: usize = 200;
static UPDATE_CHANNEL_SIZE: usize = 200; static UPDATE_CHANNEL_SIZE: usize = 200;
/// The Instance type is the heart of the mize system
pub struct Instance { pub struct Instance {
store: Itemstore, store: Itemstore,
peers: Vec<Connection>, peers: Vec<Connection>,
@@ -71,41 +73,18 @@ impl Instance {
}; };
if !instance.store.has_item(MizeId::main(1)).await? { if !instance.store.has_item(MizeId::main(1)).await? {
// create first item ... which is this instance instance.store.create_item_from_json(json!({
let mut instance_item = ItemGeneral { "item": {
id: MizeId::main(1), "kind": "instance",
kind: vec!["instance".to_owned()], },
value: Vec::new(), "daemon": {
map: HashMap::new(), "log": "",
}; "pid": "",
instance_item.map.insert("daemon".to_owned(), MizeId::main(1).join("daemon")); "item": {
instance.store.set_item(Item::General(instance_item)).await?; "kind": "instance/daemon",
},
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?;
}; };
return Ok(instance); return Ok(instance);

View File

@@ -3,8 +3,10 @@ use std::{collections::HashMap, path::PathBuf, io::Cursor, fmt::Display};
use serde_cbor::Value as CborValue; use serde_cbor::Value as CborValue;
use std::fs::File; use std::fs::File;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use std::rc::Rc;
use crate::error::{MizeError, MizeResult, IntoMizeResult}; use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::itemstore::Itemstore;
#[derive(Debug)] #[derive(Debug)]
@@ -178,7 +180,7 @@ impl ItemRef {
todo!() todo!()
} }
fn write_to_path(&self, path: PathBuf) -> MizeResult<()> { async fn write_to_path(&self, path: PathBuf) -> MizeResult<()> {
let store = self.store.lock().await; let store = self.store.lock().await;
for (key, value) in self.map.iter() { for (key, value) in self.map.iter() {
// TODO right here!!!! // TODO right here!!!!

View File

@@ -4,8 +4,9 @@ use std::path::{PathBuf, Path};
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{Read, Write}; use std::io::{Read, Write};
use colored::Colorize; 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}; use crate::error::{MizeError, MizeResult, IntoMizeResult};
pub enum Itemstore { pub enum Itemstore {
@@ -146,7 +147,7 @@ impl FolderItemstore {
final_path.push(id_string[..2].to_owned()); 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<()> { async fn set_item(&self, item: Item) -> MizeResult<()> {

View File

@@ -16,7 +16,24 @@ use std::env;
use log::{trace, debug, info, warn, error}; 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 error;
// mod types; // mod types;
mod instance; mod instance;
@@ -24,11 +41,17 @@ mod item;
mod itemstore; mod itemstore;
mod proto; mod proto;
#[cfg(test)]
mod test;
static APPNAME: &str = "mize"; static APPNAME: &str = "mize";
static DEFAULT_LOG_LEVEL: LevelFilter = LevelFilter::Warn; static DEFAULT_LOG_LEVEL: LevelFilter = LevelFilter::Warn;
#[tokio::main] #[tokio::main]
async fn 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(); let cli_matches = cli_matches();
@@ -55,8 +78,8 @@ async fn main() {
// mi set // mi set
Some(("set", sub_matches)) => cli::set(sub_matches).await, Some(("set", sub_matches)) => cli::set(sub_matches).await,
// mi print // mi sub
Some(("print", sub_matches)) => cli::print(sub_matches).await, Some(("sub", sub_matches)) => cli::sub(sub_matches).await,
// mi call // mi call
Some(("call", sub_matches)) => cli::call(sub_matches).await, 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")) .arg(Arg::new("id").help("The id or path to set"))
) )
.subcommand( .subcommand(
Command::new("print") Command::new("sub")
.aliases(["p"]) .aliases(["su"])
.arg(&store_arg) .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( .subcommand(
Command::new("call") Command::new("call")