instance init with config works + view ItemData func implemented

This commit is contained in:
Sebastian Moser
2024-06-01 12:21:35 +02:00
parent 47b187d012
commit e0211b86ca
10 changed files with 332 additions and 232 deletions

View File

@@ -5,12 +5,13 @@ use home::home_dir;
use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait};
use mize::instance::Instance;
use mize::platform::os::config_from_cli_args;
pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::new()?;
let instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
let id = sub_matches.get_one::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;

View File

@@ -13,9 +13,8 @@ use mize::instance::Instance;
pub fn run(sub_matches: &ArgMatches) -> MizeResult<()> {
// TODO NEXT!!!
// - add --system arg
// - add --user arg
// - --config arg
// - --folder
// - to set a location, where there will be a /store and a /sock
// - listen for connections on that sock
// - msg handeling

View File

@@ -5,7 +5,8 @@ use std::path::PathBuf;
use clap::{ArgAction, ArgMatches};
use clap::{Arg, crate_version, Command};
use mize::error::MizeError;
use mize::error::{MizeError, MizeResult};
use mize::item::ItemData;
use tokio::sync::{Mutex, mpsc};
use std::sync::Arc;
use log::Level;
@@ -258,12 +259,9 @@ fn level_from_env(var_name: &str, log_level: LevelFilter) -> (Option<log::LevelF
return (None, log_messages);
}
fn cli_matches() -> clap::ArgMatches {
let folder_arg = Arg::new("folder")
.short('f')
.long("folder")
.help("The folder the Instance stores all it's data and the socket for connections");
let main = Command::new(APPNAME)
.version(crate_version!())
@@ -273,18 +271,37 @@ fn cli_matches() -> clap::ArgMatches {
.long("verbose")
.short('v')
.action(ArgAction::Count)
.global(true)
)
.arg(Arg::new("log-level")
.long("log-level")
.value_name("LOGLEVEL")
.help("set the log-level to one of OFF, ERROR, WARN, INFO, DEBUG, TRACE")
.global(true)
)
.arg(Arg::new("silent")
.long("silent")
.action(ArgAction::SetTrue)
.help("set the log-level to OFF")
.global(true)
)
.arg(Arg::new("folder")
.short('f')
.long("folder")
.help("The folder the Instance stores all it's data and the socket for connections")
.global(true)
)
.arg(Arg::new("config")
.short('c')
.long("config")
.help("overwrite config options")
.global(true)
)
.arg(Arg::new("config-file")
.long("config-file")
.help("specify a config file")
.global(true)
)
.arg(&folder_arg)
.subcommand(
Command::new("run")
.aliases(["r"])
@@ -300,25 +317,21 @@ fn cli_matches() -> clap::ArgMatches {
.subcommand(
Command::new("get")
.aliases(["g"])
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to get"))
)
.subcommand(
Command::new("set")
.aliases(["s"])
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to set"))
)
.subcommand(
Command::new("show")
.aliases(["so"])
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to sub to and show"))
)
.subcommand(
Command::new("call")
.aliases(["c"])
.arg(&folder_arg)
)
.arg_required_else_help(true);

View File

@@ -1,34 +1,109 @@
use ciborium::Value as CborValue;
use serde::Deserialize;
use std::env::VarError;
use std::fs;
use log::{debug, error, warn, trace};
use clap::ArgMatches;
use crate::id::MizeId;
use crate::instance::store::Store;
use crate::instance::Instance;
use crate::error::{IntoMizeResult, MizeError, MizeResult};
use crate::item::{ItemData, IntoItemData};
pub fn os_instance_init<S: Store>(instance: &mut Instance<S>) -> 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 mut config = ItemData::new();
let toml_string = fs::read_to_string(&config_file_path)
.mize_result_msg(format!("Could not read file {} as mize config", &config_file_path))?;
////// load MIZE_CONFIG_FILE as the instance, which is item 0
match std::env::var("MIZE_CONFIG_FILE") {
Ok(config_file_path) => {
config.merge(config_from_file(config_file_path)?);
debug!("env var MIZE_CONFIG_FILE present");
trace!("config after MIZE_CONFIG_FILE env var: {}", config);
}
Err(var_err) => match var_err {
VarError::NotPresent => {
debug!("env var MIZE_CONFIG_FILE NOT present");
},
VarError::NotUnicode(_) => {
warn!("env var MIZE_CONFIG_FILE is not Unicode, so not reading it")
},
}
};
////// load config from MIZE_CONFIG env var
match std::env::var("MIZE_CONFIG") {
Ok(config_string) => {
config.merge(config_from_string(config_string)?);
debug!("env var MIZE_CONFIG present");
trace!("config after MIZE_CONFIG env var: {}", config);
}
Err(var_err) => match var_err {
VarError::NotPresent => {
debug!("env var MIZE_CONFIG NOT present")
},
VarError::NotUnicode(_) => {
warn!("env var MIZE_CONFIG is not Unicode, so not reading it")
},
}
};
instance.set("0", config)?;
return Ok(());
}
pub fn config_from_cli_args(matches: &ArgMatches) -> MizeResult<ItemData> {
let mut config = ItemData::new();
if let Some(config_file_path) = matches.get_one::<String>("config-file") {
config.merge(config_from_file(config_file_path.to_string())?);
trace!("config after --config-file arg: {}", config);
}
if let Some(config_string) = matches.get_one::<String>("config") {
config.merge(config_from_string(config_string.to_string())?);
trace!("config after --config arg: {}", config);
}
return Ok(config);
}
pub fn config_from_file(file_path: String) -> MizeResult<ItemData> {
let toml_string = fs::read_to_string(&file_path)
.mize_result_msg(format!("Could not read file {} as mize 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))?;
.mize_result_msg(format!("Could not deserialize the content of MIZE_CONFIG_FILE at {}", &file_path))?;
instance.set("0", config)?;
return Ok(config.into_item_data());
}
////// load config from MIZE_CONFIG env var
//TODO
return Ok(());
fn config_from_string(config_string: String) -> MizeResult<ItemData> {
let mut config = ItemData::new();
for option in config_string.split(";") {
let path = option.split("=").nth(0)
.ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty path (thing beforee =)", option)))?;
let value = option.split("=").nth(1)
.ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty value (thing after =)", option)))?;
let mut path_vec = vec!["config"];
path_vec.extend(path.split("."));
config.set_path(path_vec, ItemData::parse(value))?;
}
return Ok(config);
}