166 lines
4.6 KiB
Rust
166 lines
4.6 KiB
Rust
use clap::ArgMatches;
|
|
use marts::CliPart;
|
|
use core::result::Result::Err;
|
|
use core::unimplemented;
|
|
use mize::flume::bounded;
|
|
use mize::{instance, mize_err};
|
|
use std::ffi::OsString;
|
|
use std::io::Read;
|
|
use std::path::PathBuf;
|
|
use std::str::FromStr;
|
|
use tracing::debug;
|
|
use mize::ciborium;
|
|
|
|
use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait};
|
|
use mize::instance::subscription::Subscription;
|
|
use mize::instance::subscription::Update;
|
|
use mize::instance::Mize;
|
|
use mize::item::{IntoItemData, ItemData};
|
|
use mize::platform::os::config_from_cli_args;
|
|
use mize::platform::os::fsstore::FileStore;
|
|
|
|
pub fn get(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
|
|
let id = sub_matches
|
|
.get_one::<String>("id")
|
|
.ok_or(MizeError::new().msg("No id Argument specified"))?;
|
|
|
|
let item = instance.get(id)?;
|
|
|
|
let recurse_count = sub_matches.get_count("recurse");
|
|
|
|
println!("{}", item.as_data_full()?);
|
|
return Ok(());
|
|
|
|
if recurse_count > 0 {
|
|
println!("{}", item.id());
|
|
println!("{}", item.as_data_full()?);
|
|
} else {
|
|
println!("{}", item);
|
|
};
|
|
|
|
return Ok(());
|
|
}
|
|
|
|
pub fn call(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
println!("call cmd not implemented");
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
|
|
let item = instance.new_item()?;
|
|
|
|
println!("id: {}", item.id());
|
|
println!("with namespace: {}", item.id().namespace().as_real_string());
|
|
|
|
return Ok(());
|
|
}
|
|
|
|
pub fn status(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
println!("not implemented");
|
|
Err(mize_err!("not implemented"))
|
|
}
|
|
|
|
pub fn info(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
println!("not implemented");
|
|
Err(mize_err!("not implemented"))
|
|
}
|
|
|
|
pub fn run(sub_matches: &ArgMatches, mize: &mut Mize) -> MizeResult<()> {
|
|
match sub_matches.subcommand() {
|
|
None => { // run a mize instance (does whatever it's config will say)
|
|
mize.wait();
|
|
}
|
|
Some((runnable, sub_matches)) => { // run smth from a part. for now registered cli commands
|
|
let mut ext_args: Vec<String> = sub_matches
|
|
.get_many::<OsString>("")
|
|
.unwrap_or_default()
|
|
.map(|v| v.to_string_lossy().to_string())
|
|
.collect();
|
|
let mut ext_cmdline = vec![runnable.to_owned()];
|
|
ext_cmdline.append(&mut ext_args);
|
|
|
|
let mut cli = mize.get_part_native::<CliPart>("cli")?;
|
|
cli.run(ext_cmdline)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn set(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
let id = sub_matches
|
|
.get_one::<String>("id")
|
|
.ok_or(MizeError::new().msg("No id Argument specified"))?;
|
|
|
|
let value = sub_matches
|
|
.get_one::<String>("value")
|
|
.ok_or(MizeError::new().msg("No value Argument specified"))?;
|
|
|
|
instance.set_blocking(id, value.into_item_data())?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn show(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
println!("not implemented");
|
|
// let instance = Mize::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"))?;
|
|
|
|
// let item = instance.get(id)?;
|
|
|
|
// let (tx, rx) = bounded::<Update>(4);
|
|
// let sub = Subscription::from_sender(tx);
|
|
// instance.sub(id, sub)?;
|
|
|
|
// println!("item: {}", item.as_data_full()?);
|
|
|
|
// for update in rx {
|
|
// println!("####### GOT UPDATE #######");
|
|
// println!("item: {}", update.new_item()?.as_data_full()?);
|
|
// }
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn format_cbor(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> {
|
|
let mut input: Vec<u8> = Vec::new();
|
|
let stdin = std::io::stdin();
|
|
let mut handle = stdin.lock();
|
|
//handle.read_to_end(&mut input);
|
|
|
|
let cbor_data = ciborium::from_reader(handle)?;
|
|
let item_data = ItemData::from_cbor(cbor_data);
|
|
|
|
println!("{}", item_data);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn run_from_part(cmd: &str, sub_matches: &ArgMatches, instance: &mut Mize) -> 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)?);
|
|
|
|
debug!("loading module '{}'", cmd);
|
|
instance.load_part(cmd)?;
|
|
|
|
println!("running cli from part not implemented");
|
|
//instance
|
|
//.get_part(cmd)?
|
|
//.run_cli(&instance, sub_cmd_line)
|
|
//.ok_or(mize_err!(
|
|
//"The Part '{cmd}' does not implement the run_cli() functionality"
|
|
//))??;
|
|
|
|
Ok(())
|
|
}
|