many changes
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use clap::ArgMatches;
|
||||
use home::home_dir;
|
||||
use flume::bounded;
|
||||
use mize::mize_err;
|
||||
use std::io::Read;
|
||||
use tracing::debug;
|
||||
|
||||
use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait};
|
||||
use mize::instance::Instance;
|
||||
@@ -158,4 +162,19 @@ pub fn format_cbor(sub_matches: &ArgMatches) -> MizeResult<()> {
|
||||
}
|
||||
|
||||
|
||||
pub fn run_from_module(cmd: &str, sub_matches: &ArgMatches) -> MizeResult<()> {
|
||||
let default = OsString::default();
|
||||
let mut sub_cmd_line: Vec<OsString> = sub_matches.get_many::<OsString>("").unwrap().map(|e| e.to_owned()).collect();
|
||||
sub_cmd_line.insert(0, OsString::from_str(cmd)?);
|
||||
|
||||
let mut instance = Instance::with_config(config_from_cli_args(sub_matches)?)?;
|
||||
|
||||
debug!("loading module '{}'", cmd);
|
||||
instance.load_module(cmd)?;
|
||||
|
||||
instance.get_module(cmd)?.run_cli(&instance, sub_cmd_line)
|
||||
.ok_or(mize_err!("The Module '{cmd}' does not implement the run_cli() functionality"))??;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
|
||||
#![ allow( warnings ) ]
|
||||
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use clap::builder::OsStr;
|
||||
use clap::{ArgAction, ArgMatches};
|
||||
use clap::{Arg, crate_version, Command};
|
||||
|
||||
use mize::error::{MizeError, MizeResult};
|
||||
use mize::{instance, Instance};
|
||||
use mize::item::ItemData;
|
||||
use tokio::sync::{Mutex, mpsc};
|
||||
use std::sync::Arc;
|
||||
@@ -70,7 +73,7 @@ fn main() {
|
||||
Some(("format-cbor", sub_matches)) => cli::format_cbor(sub_matches),
|
||||
|
||||
// some unknown command passed
|
||||
Some((cmd, sub_matches)) => Err(MizeError::new().msg(format!("The subcommand: {} is not known. use --help to list availavle commands", cmd))),
|
||||
Some((cmd, sub_matches)) => cli::run_from_module(cmd, sub_matches),
|
||||
|
||||
None => Err(MizeError::new().msg("No subcommand was passed. use --help to list availavle comamnds.")),
|
||||
};
|
||||
@@ -81,6 +84,8 @@ fn main() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
fn cli_matches() -> clap::ArgMatches {
|
||||
|
||||
|
||||
@@ -88,6 +93,7 @@ fn cli_matches() -> clap::ArgMatches {
|
||||
.version(crate_version!())
|
||||
.author("Sebastian Moser")
|
||||
.about("The MiZe Command line tool")
|
||||
.allow_external_subcommands(true)
|
||||
.arg(Arg::new("verbose")
|
||||
.long("verbose")
|
||||
.short('v')
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::id::MizeId;
|
||||
use crate::instance::store::Store;
|
||||
use crate::instance::Instance;
|
||||
use crate::error::{IntoMizeResult, MizeError, MizeResult};
|
||||
use crate::item::{ItemData, IntoItemData};
|
||||
use crate::item::{data_from_string, IntoItemData, ItemData};
|
||||
use crate::memstore::MemStore;
|
||||
use crate::{mize_err, Module};
|
||||
use crate::instance::module::EmptyModule;
|
||||
@@ -57,7 +57,7 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
|
||||
////// load config from MIZE_CONFIG env var
|
||||
match std::env::var("MIZE_CONFIG") {
|
||||
Ok(config_string) => {
|
||||
let config = config_from_string(config_string)?;
|
||||
let config = data_from_string(config_string)?;
|
||||
debug!("env var MIZE_CONFIG present");
|
||||
instance.set_blocking("0", config)?;
|
||||
trace!("config after MIZE_CONFIG env var: {}", instance.get("0/config")?);
|
||||
@@ -282,7 +282,7 @@ pub fn config_from_cli_args(matches: &ArgMatches) -> MizeResult<ItemData> {
|
||||
}
|
||||
|
||||
if let Some(config_string) = matches.get_one::<String>("config") {
|
||||
config.merge(config_from_string(config_string.to_string())?);
|
||||
config.merge(data_from_string(config_string.to_string())?);
|
||||
trace!("config after --config arg: {}", config);
|
||||
}
|
||||
|
||||
@@ -300,27 +300,6 @@ pub fn config_from_file(file_path: String) -> MizeResult<ItemData> {
|
||||
return Ok(item_data);
|
||||
}
|
||||
|
||||
fn config_from_string(config_string: String) -> MizeResult<ItemData> {
|
||||
|
||||
let mut config = ItemData::new();
|
||||
|
||||
for option in config_string.split(":") {
|
||||
if option == "".to_owned() {
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user