From 38f967db768c20f0c44559693dfd808b6d093933 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Thu, 30 May 2024 14:59:47 +0200 Subject: [PATCH] basic get command works with items from 0/config defined in test-config.toml --- Cargo.lock | 157 +++++++++++++++++----- Cargo.toml | 6 +- flake.nix | 3 + src/core/error.rs | 17 ++- src/core/id.rs | 31 ++++- src/core/instance/mod.rs | 35 +++-- src/core/instance/store.rs | 4 +- src/core/instance/subscription.rs | 1 + src/core/item.rs | 26 +++- src/core/memstore.rs | 80 ++++++++++- src/core/proto/mod.rs | 2 +- src/lib.rs | 11 +- src/platform/os/cli/call.rs | 4 +- src/platform/os/cli/get.rs | 26 ++-- src/platform/os/cli/mount.rs | 4 +- src/platform/os/cli/{daemon.rs => run.rs} | 8 +- src/platform/os/cli/serve.rs | 4 - src/platform/os/cli/set.rs | 4 +- src/platform/os/cli/show.rs | 10 +- src/platform/os/cli/stop.rs | 6 + src/platform/os/main.rs | 47 +++---- src/platform/os/mod.rs | 32 +++++ test-config.toml | 4 + 23 files changed, 402 insertions(+), 120 deletions(-) rename src/platform/os/cli/{daemon.rs => run.rs} (91%) delete mode 100644 src/platform/os/cli/serve.rs create mode 100644 src/platform/os/cli/stop.rs create mode 100644 src/platform/os/mod.rs create mode 100644 test-config.toml diff --git a/Cargo.lock b/Cargo.lock index d915466..6fb572f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -309,12 +309,6 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" -[[package]] -name = "byteorder" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855" - [[package]] name = "byteorder" version = "1.5.0" @@ -327,16 +321,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" -[[package]] -name = "cbor" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e56053652b4b5c0ded5ae6183c7cd547ad2dd6bcce149658bef052a4995533bd" -dependencies = [ - "byteorder 0.5.3", - "rustc-serialize", -] - [[package]] name = "cc" version = "1.0.97" @@ -367,6 +351,33 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "clap" version = "4.5.4" @@ -469,6 +480,12 @@ version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-common" version = "0.1.6" @@ -557,6 +574,12 @@ dependencies = [ "termcolor", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.9" @@ -685,7 +708,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" dependencies = [ "fallible-iterator", - "indexmap", + "indexmap 1.9.3", "stable_deref_trait", ] @@ -695,12 +718,28 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "headers" version = "0.3.9" @@ -882,7 +921,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", +] + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown 0.14.5", ] [[package]] @@ -1002,7 +1051,7 @@ dependencies = [ "axum", "axum-extra", "axum-macros", - "cbor", + "ciborium", "clap", "colored", "console_error_panic_hook", @@ -1011,7 +1060,9 @@ dependencies = [ "home", "log", "nix", + "serde", "tokio", + "toml", "uuid", "wasm-bindgen", "wasm-bindgen-cli", @@ -1301,12 +1352,6 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" -[[package]] -name = "rustc-serialize" -version = "0.3.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401" - [[package]] name = "rustix" version = "0.38.34" @@ -1377,18 +1422,18 @@ checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" [[package]] name = "serde" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.202" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", @@ -1416,6 +1461,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1660,6 +1714,40 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" +dependencies = [ + "indexmap 2.2.6", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower" version = "0.4.13" @@ -1744,7 +1832,7 @@ version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e3dac10fd62eaf6617d3a904ae222845979aec67c615d1c842b4002c7666fb9" dependencies = [ - "byteorder 1.5.0", + "byteorder", "bytes", "data-encoding", "http", @@ -2310,6 +2398,15 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86c949fede1d13936a99f14fafd3e76fd642b556dd2ce96287fbe2e0151bfac6" +dependencies = [ + "memchr", +] + [[package]] name = "zeroize" version = "1.7.0" diff --git a/Cargo.toml b/Cargo.toml index 2935409..85f2f5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ path = "src/platform/os/main.rs" required-features = [ "os-target" ] [features] -os-target = [ "clap", "home", "env_logger", "daemonize", "nix" ] +os-target = [ "clap", "home", "env_logger", "daemonize", "nix", "toml" ] wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli" ] @@ -27,7 +27,8 @@ colored = "2.0.0" uuid = "1.3.0" log = "0.4.20" tokio = { version = "1.21.0", features = ["sync"] } -cbor = "0.4.1" +ciborium = { version = "0.2.2", default-features = false } +serde = "1.0.203" # wasm dependencies wasm-bindgen = { version = "0.2.84", optional = true } @@ -40,6 +41,7 @@ home = { version = "0.5.9", optional = true } env_logger = { version = "0.10.1", optional = true } daemonize = { version = "0.5.0", optional = true } nix = { version = "0.27.1", features = ["signal"], optional = true } +toml = { version = "0.8.13", features = ["parse"], optional = true } # bare dependencies diff --git a/flake.nix b/flake.nix index 341e727..3847e26 100644 --- a/flake.nix +++ b/flake.nix @@ -89,6 +89,9 @@ in { */ (fenix.packages.${system}.combine [ wasmToolchain osToolchain ]) ]; + shellHook = '' + export MIZE_CONFIG_FILE=${self}/default-config.toml + ''; }; }) // { diff --git a/src/core/error.rs b/src/core/error.rs index 6133cd3..cb6c318 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -103,7 +103,13 @@ impl MizeError { } pub fn log(self) -> MizeError { - error!("MizeError encountered: {}", self.messages.join("\n")); + error!("MizeError envountered!"); + if let Some(ref location) = self.code_location { + error!("[ {} ] {}", "LOCATION".yellow(), location); + }; + for msg in &self.messages { + error!("[ {} ] {}", "MSG".yellow(), msg); + } self } @@ -209,7 +215,9 @@ impl IntoMizeResult for Result where E: std::fmt::Display { match self { Ok(val) => MizeResult::Ok(val), - Err(err) => MizeResult::Err(MizeError::new().category("misc").msg(format!("From any Error: {}", err))), + Err(err) => MizeResult::Err(MizeError::new() + .category("misc") + .msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))), } } #[track_caller] @@ -217,7 +225,10 @@ impl IntoMizeResult for Result where E: std::fmt::Display { let caller_location = std::panic::Location::caller(); match self { Ok(val) => MizeResult::Ok(val), - Err(err) => MizeResult::Err(MizeError::new().category("misc").msg(format!("{}", msg)).msg(format!("From any Error: {}", err))), + Err(err) => MizeResult::Err(MizeError::new() + .category("misc") + .msg(format!("{}", msg)) + .msg(format!("From {}: {}", std::any::type_name_of_val(&err), err))), } } } diff --git a/src/core/id.rs b/src/core/id.rs index 5161c6f..c94d715 100644 --- a/src/core/id.rs +++ b/src/core/id.rs @@ -1,13 +1,34 @@ -use std::hash::Hash; +use core::fmt; +use std::{fmt::write, hash::Hash}; -#[derive(Hash, Eq, PartialEq)] +#[derive(Debug, Clone, Hash, Eq, PartialEq)] pub struct MizeId { path: Vec } -impl From for MizeId { - fn from(value: String) -> Self { - MizeId{path: vec![value]} +impl MizeId { + pub fn store_part(&self) -> &str { + self.path.first() + .expect("an empty MizeId found, that should absolutely not be possible!!!!").as_str() + } + pub fn as_vec(self) -> Vec { + self.path } } + +impl fmt::Display for MizeId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.path.join("/")) + } +} + +impl> From for MizeId { + fn from(value: T) -> Self { + let string = value.into(); + let path: Vec = string.split("/").map(|v| v.to_owned()).collect(); + return MizeId { path } + } +} + + diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index 5518eda..152e783 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -1,5 +1,7 @@ +use core::fmt; use std::collections::HashMap; use std::fs::create_dir; +use std::sync::{Arc, Mutex}; use log::{trace, debug, info, warn, error}; use uuid::Uuid; use std::fs::File; @@ -21,15 +23,17 @@ pub mod subscription; static MSG_CHANNEL_SIZE: usize = 200; /// The Instance type is the heart of the mize system +#[derive(Clone)] pub struct Instance { // question vec of stores??? // would mean replication logic and such things is handeled by the instance // i think it would be better thogh to then implement a ReplicatedStore - store: S, - peers: Vec>, - subs: HashMap + pub store: S, + peers: Arc>>>, + subs: Arc>>, } + /* pub enum RealmId { Uuid(Uuid), @@ -38,21 +42,27 @@ pub enum RealmId { } */ - -impl Instance { - // new() opens a - pub async fn new() -> MizeResult> { +impl Instance { + pub fn new() -> MizeResult> { trace!("[ {} ] Instance::new()", "CALL".yellow()); let memstore = MemStore::new(); - let instance = Instance {store: memstore, peers: Vec::new(), subs: HashMap::new()}; + let instance = Instance {store: memstore, peers: Arc::new(Mutex::new(Vec::new())), subs: Arc::new(Mutex::new(HashMap::new())) }; + // platform specific init code - // TODO + if cfg!(feature = "os-target") { // on os platforms + crate::platform::os::os_instance_init(instance.clone())? + } + + // end of platform specific init code + return Ok(instance); } +} +impl Instance { pub fn new_item(self) -> MizeResult> { let id = self.store.new_id()?; return Ok(Item {id, instance: self}); @@ -63,9 +73,14 @@ impl Instance { } pub fn set, V: Into>(self, id: I, value: V) -> MizeResult<()> { - self.store.set(id.into(), value) + self.store.set(&id.into(), value) } } +impl fmt::Debug for Instance { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Mize Instance with subs: {:?}", self.subs,) + } +} diff --git a/src/core/instance/store.rs b/src/core/instance/store.rs index 96da4dc..3b304c6 100644 --- a/src/core/instance/store.rs +++ b/src/core/instance/store.rs @@ -9,7 +9,7 @@ pub trait Store { // is implemented, so that there can be a multithreaded implementation //fn get(self, id: MizeId) -> MizeResult> where Self: Sized; - fn set>(self, id: MizeId, data: T) -> MizeResult<()>; + fn set>(self, id: &MizeId, data: T) -> MizeResult<()>; // in the future should implement transactions, .... @@ -19,6 +19,8 @@ pub trait Store { fn get_backlinks(self, item: Item) -> MizeResult> where Self: Sized; fn new_id(&self) -> MizeResult; + + fn get_value_raw(&self, id: &MizeId) -> MizeResult>; } /// references an item that is stored in a store diff --git a/src/core/instance/subscription.rs b/src/core/instance/subscription.rs index 049ca0d..57efe31 100644 --- a/src/core/instance/subscription.rs +++ b/src/core/instance/subscription.rs @@ -1,3 +1,4 @@ +#[derive(Debug)] pub struct Subscription; diff --git a/src/core/item.rs b/src/core/item.rs index 731f200..1598bc4 100644 --- a/src/core/item.rs +++ b/src/core/item.rs @@ -8,10 +8,11 @@ use crate::error::{MizeError, MizeResult, IntoMizeResult}; use crate::instance::Instance; use crate::instance::store::Store; use crate::id::MizeId; -use cbor::Cbor as CborValue; +use ciborium::Value as CborValue; // a item always has to do with a Instance, which takes care of how it is updated +#[derive(Debug)] pub struct Item { pub id: MizeId, pub instance: Instance @@ -22,16 +23,31 @@ pub struct Item { pub type ItemData = CborValue; impl Item { - pub fn id(self) -> MizeId { - self.id + pub fn id(&self) -> &MizeId { + &self.id } - pub fn value_raw(self) -> MizeResult> { + pub fn value_raw(&self) -> MizeResult> { // this will call from the instance which gets the value from the store - Ok(Vec::new()) + self.instance.store.get_value_raw(&self.id()) } } +impl Display for Item { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.value_raw() { + Ok(value) => match String::from_utf8(value) { + Ok(string) => write!(f, "{}", string), + Err(_) => write!(f, "{:?}", self.value_raw()), + } + Err(err) => {err.log(); Ok(())}, + }; + return Ok(()); + } +} + + + /* #[derive(Debug)] diff --git a/src/core/memstore.rs b/src/core/memstore.rs index 8c1420b..d8cdfe4 100644 --- a/src/core/memstore.rs +++ b/src/core/memstore.rs @@ -1,35 +1,101 @@ use std::collections::HashMap; -use cbor::Cbor as CborValue; +use std::iter::Map; +use std::sync::{Arc, Mutex}; +use ciborium::Value as CborValue; +use log::trace; use crate::id::MizeId; use crate::instance::store::Store; -use crate::error::MizeResult; +use crate::error::{MizeError, MizeResult, IntoMizeResult}; use crate::item::{Item, ItemData}; +#[derive(Clone, Debug)] pub struct MemStore { - map: HashMap, + inner: Arc>, +} + +#[derive(Debug)] +struct MemStoreInner { + map: HashMap, next_id: u64, } impl Store for MemStore { - fn set>(mut self, id: MizeId, data: T) -> MizeResult<()> { - self.map.insert(id, data.into()); + fn set>(mut self, id: &MizeId, data: T) -> MizeResult<()> { + let mut inner = self.inner.lock()?; + + + inner.map.insert(id_to_u64(id)?, data.into()); return Ok(()) } fn get_links(self, item: Item) -> MizeResult> { + let inner = self.inner.lock()?; + Ok(Vec::new()) } fn get_backlinks(self, item: Item) -> MizeResult> { + let inner = self.inner.lock()?; + Ok(Vec::new()) } fn new_id(&self) -> MizeResult { - let id_string = format!("{}", self.next_id); + let inner = self.inner.lock()?; + + let id_string = format!("{}", inner.next_id); return Ok(id_string.into()); } + fn get_value_raw(&self, id: &MizeId) -> MizeResult> { + let inner = self.inner.lock()?; + + let cbor_val = inner.map.get(&id_to_u64(id)?) + .ok_or(MizeError::new() + .msg(format!("Item with store_part: {}, id: {} does not exist in MemStore", id_to_u64(id)?, id)))?; + + let mut id_without_first = id.clone().as_vec(); + id_without_first.remove(0); + get_raw_from_cbor(cbor_val.to_owned(), id_without_first) + } } impl MemStore { pub fn new() -> MemStore { - return MemStore { map: HashMap::new(), next_id: 0 }; + let inner = MemStoreInner { map: HashMap::new(), next_id: 0 }; + return MemStore { inner: Arc::new(Mutex::new(inner)) }; } } + +fn id_to_u64(id: &MizeId) -> MizeResult { + id.store_part() + .parse() + .mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id)) +} + +fn get_raw_from_cbor(value: CborValue, mut path: Vec) -> MizeResult> { + match value { + CborValue::Bytes(vec) => Ok(vec), + CborValue::Text(string) => Ok(string.into_bytes()), + CborValue::Map(map) => { + let mut inner_val: CborValue = CborValue::Null; + trace!("map: {:?}", map); + for (key, val) in map.into_iter() { + let first_path_string = path.first() + .ok_or(MizeError::new().msg("get_raw_from_cbor: path is empty"))?; + + if let CborValue::Text(key_text) = key { + if key_text == *first_path_string { + inner_val = val + } + } + } + if inner_val == CborValue::Null { + return Err(MizeError::new().msg(format!("get_raw_from_cbor: Path {} not Found", path.join("/")))) + } + path.remove(0); + get_raw_from_cbor(inner_val, path) + }, + _ => Err(MizeError::new().msg("get_raw_from_cbor: value is not Bytes, Text or Map")), + } +} + + + diff --git a/src/core/proto/mod.rs b/src/core/proto/mod.rs index 58153ee..a02f24f 100644 --- a/src/core/proto/mod.rs +++ b/src/core/proto/mod.rs @@ -1,4 +1,4 @@ -use cbor::Cbor as CborValue; +use ciborium::Value as CborValue; #[derive(Debug, Clone)] pub struct MizeMessage { diff --git a/src/lib.rs b/src/lib.rs index 8b98d18..b220ed8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,8 @@ static PROTO_VERSION: u8 = 1; -// the core part houses the code that can run on any platform -/* -pub mod core { +// the core part has the code that can run on any platform +mod core { pub mod memstore; pub mod instance; pub mod item; @@ -16,11 +15,15 @@ pub mod core { } pub use core::*; -// */ + +// platform specific stuff pub mod platform { #[cfg(feature = "wasm-target")] pub mod wasm; + + //#[cfg(feature = "os-target")] + pub mod os; } diff --git a/src/platform/os/cli/call.rs b/src/platform/os/cli/call.rs index 20a6047..16b29e4 100644 --- a/src/platform/os/cli/call.rs +++ b/src/platform/os/cli/call.rs @@ -1,4 +1,6 @@ use clap::ArgMatches; +use mize::error::MizeResult; -pub fn call(sub_matches: &ArgMatches) { +pub fn call(sub_matches: &ArgMatches) -> MizeResult<()> { + Ok(()) } diff --git a/src/platform/os/cli/get.rs b/src/platform/os/cli/get.rs index 478b58d..eb701db 100644 --- a/src/platform/os/cli/get.rs +++ b/src/platform/os/cli/get.rs @@ -3,22 +3,22 @@ use std::path::PathBuf; use clap::ArgMatches; use home::home_dir; -use mize::error::{MizeResultTrait, IntoMizeResult, MizeError}; +use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait}; use mize::instance::Instance; -pub fn get(sub_matches: &ArgMatches) { - let instance_folder_path = match sub_matches.get_one::("store") { - Some(a) => PathBuf::from(a), - None => { - let mut home_dir = home_dir() - .ok_or(MizeError::new().category("io").category("env").msg("could not get the home directory, where the mize store is by default")).critical(); - home_dir.push(".mize"); - home_dir - }, - }; - println!("unfinished"); - //let instance = Instance::new(instance_folder_path); +pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> { + + let instance = Instance::new()?; + + let id = sub_matches.get_one::("id") + .ok_or(MizeError::new().msg("No id Argument specified"))?; + + let item = instance.get(id)?; + + println!("{}", item); + + return Ok(()); } diff --git a/src/platform/os/cli/mount.rs b/src/platform/os/cli/mount.rs index e3d5196..ea980e7 100644 --- a/src/platform/os/cli/mount.rs +++ b/src/platform/os/cli/mount.rs @@ -1,4 +1,6 @@ use clap::ArgMatches; +use mize::error::MizeResult; -pub fn mount(sub_matches: &ArgMatches) { +pub fn mount(sub_matches: &ArgMatches) -> MizeResult<()> { + Ok(()) } diff --git a/src/platform/os/cli/daemon.rs b/src/platform/os/cli/run.rs similarity index 91% rename from src/platform/os/cli/daemon.rs rename to src/platform/os/cli/run.rs index 76f6756..01be002 100644 --- a/src/platform/os/cli/daemon.rs +++ b/src/platform/os/cli/run.rs @@ -6,12 +6,13 @@ use std::fs::File; use log::{trace, debug, info, warn, error}; use nix::sys::signal::{self, Signal}; -use mize::error::{MizeResultTrait, IntoMizeResult, MizeError}; +use mize::error::{MizeResultTrait, IntoMizeResult, MizeError, MizeResult}; use mize::instance::Instance; -pub fn daemon(sub_matches: &ArgMatches) { - println!("not implemented"); +pub fn run(sub_matches: &ArgMatches) -> MizeResult<()> { + + let instance = Instance::new(); /* let instance_path = match sub_matches.get_one::("store") { Some(a) => PathBuf::from(a), @@ -63,4 +64,5 @@ pub fn daemon(sub_matches: &ArgMatches) { }, } */ + return Ok(()); } diff --git a/src/platform/os/cli/serve.rs b/src/platform/os/cli/serve.rs deleted file mode 100644 index de6e3e1..0000000 --- a/src/platform/os/cli/serve.rs +++ /dev/null @@ -1,4 +0,0 @@ -use clap::ArgMatches; - -pub fn serve(sub_matches: &ArgMatches) { -} diff --git a/src/platform/os/cli/set.rs b/src/platform/os/cli/set.rs index 3abe745..daf51fb 100644 --- a/src/platform/os/cli/set.rs +++ b/src/platform/os/cli/set.rs @@ -1,4 +1,6 @@ use clap::ArgMatches; +use mize::error::MizeResult; -pub fn set(sub_matches: &ArgMatches) { +pub fn set(sub_matches: &ArgMatches) -> MizeResult<()> { + Ok(()) } diff --git a/src/platform/os/cli/show.rs b/src/platform/os/cli/show.rs index dbaedda..039e1ac 100644 --- a/src/platform/os/cli/show.rs +++ b/src/platform/os/cli/show.rs @@ -1,4 +1,12 @@ use clap::ArgMatches; +use mize::error::MizeResult; +use mize::instance::Instance; -pub fn show(sub_matches: &ArgMatches) { +pub fn show(sub_matches: &ArgMatches) -> MizeResult<()> { + + let instance = Instance::new()?; + + println!("0/config/test is: {}", instance.get("0/config/test")?); + + Ok(()) } diff --git a/src/platform/os/cli/stop.rs b/src/platform/os/cli/stop.rs new file mode 100644 index 0000000..c3a7ece --- /dev/null +++ b/src/platform/os/cli/stop.rs @@ -0,0 +1,6 @@ +use clap::ArgMatches; +use mize::error::MizeResult; + +pub fn stop(sub_matches: &ArgMatches) -> MizeResult<()> { + Ok(()) +} diff --git a/src/platform/os/main.rs b/src/platform/os/main.rs index 147cd8d..44c9d94 100644 --- a/src/platform/os/main.rs +++ b/src/platform/os/main.rs @@ -5,6 +5,7 @@ use std::path::PathBuf; use clap::{ArgAction, ArgMatches}; use clap::{Arg, crate_version, Command}; +use mize::error::MizeError; use tokio::sync::{Mutex, mpsc}; use std::sync::Arc; use log::Level; @@ -17,16 +18,16 @@ use std::env; use log::{trace, debug, info, warn, error}; mod cli { - pub mod daemon; - pub mod serve; + pub mod run; + pub mod stop; pub mod get; pub mod set; pub mod show; pub mod mount; pub mod call; - pub use daemon::daemon; - pub use serve::serve; + pub use run::run; + pub use stop::stop; pub use get::get; pub use set::set; pub use show::show; @@ -51,12 +52,12 @@ fn main() { // match command - match cli_matches.subcommand() { + let result = match cli_matches.subcommand() { // mi daemon - Some(("daemon", sub_matches)) => cli::daemon(sub_matches), + Some(("daemon", sub_matches)) => cli::run(sub_matches), // mi serve - Some(("serve", sub_matches)) => cli::serve(sub_matches), + Some(("serve", sub_matches)) => cli::stop(sub_matches), // mi mount Some(("mount", sub_matches)) => cli::mount(sub_matches), @@ -74,12 +75,14 @@ fn main() { Some(("call", sub_matches)) => cli::call(sub_matches), // some unknown command passed - Some((cmd, sub_matches)) => error!("The subcommand: {} is not known. use --help to list availavle commands", cmd), + Some((cmd, sub_matches)) => Err(MizeError::new().msg(format!("The subcommand: {} is not known. use --help to list availavle commands", cmd))), - None => error!("No subcommand was passed. use --help to list availavle comamnds."), + None => Err(MizeError::new().msg("No subcommand was passed. use --help to list availavle comamnds.")), + }; + + if let Err(err) = result { + err.log(); } - - } fn init_logger(cli_matches: &ArgMatches) { @@ -282,24 +285,12 @@ fn cli_matches() -> clap::ArgMatches { .help("set the log-level to OFF") ) .subcommand( - Command::new("daemon") - .aliases(["da"]) - .arg(&store_arg) - .about("Starts the mize daemon. Has subcommands to stop, etc") - .subcommand(Command::new("stop") - .about("Stops the mize daemon if one is running") - ) - .subcommand(Command::new("kill")) - .about("Stops the mize daemon with a signal") - .alias("k") - .subcommand(Command::new("status") - .alias("st") - ) + Command::new("run") + .aliases(["r"]) + .about("Run a MiZe Instance") ) - .subcommand( - Command::new("serve") - .aliases(["se"]) - .arg(&store_arg) + .subcommand(Command::new("stop") + .about("Stop a MiZe Instance") ) .subcommand( Command::new("mount") diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs new file mode 100644 index 0000000..22c7c91 --- /dev/null +++ b/src/platform/os/mod.rs @@ -0,0 +1,32 @@ +use ciborium::Value as CborValue; +use serde::Deserialize; +use std::fs; + +use crate::id::MizeId; +use crate::instance::store::Store; +use crate::instance::Instance; +use crate::error::{IntoMizeResult, MizeError, MizeResult}; + + +pub fn os_instance_init(instance: Instance) -> MizeResult<()> { + // this is the code, that runs to initialize an Instance on a system with an os present. + + // load MIZE_CONFIG_FILE as the instance, which is item 0 + let config_file_path = std::env::var("MIZE_CONFIG_FILE") + .mize_result_msg("Could not get the MIZE_CONFIG_FILE Environment Variable")?; + + let toml_string = fs::read_to_string(&config_file_path) + .mize_result_msg(format!("Could not read file {} as mize config", &config_file_path))?; + + let toml_deserializer = toml::Deserializer::new(&toml_string.as_str()); + + let config = CborValue::deserialize(toml_deserializer) + .mize_result_msg(format!("Could not deserialize the content of MIZE_CONFIG_FILE at {}", &config_file_path))?; + + instance.set("0", config)?; + + return Ok(()); +} + + + diff --git a/test-config.toml b/test-config.toml new file mode 100644 index 0000000..dfe6d64 --- /dev/null +++ b/test-config.toml @@ -0,0 +1,4 @@ + +[config] +namespace = "test.c2vi.dev" +test = "hello from test item"