basic get command works with items from 0/config defined in test-config.toml

This commit is contained in:
Sebastian Moser
2024-05-30 14:59:47 +02:00
parent 557ac09bc7
commit 38f967db76
23 changed files with 402 additions and 120 deletions

View File

@@ -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(())
}

View File

@@ -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::<String>("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::<String>("id")
.ok_or(MizeError::new().msg("No id Argument specified"))?;
let item = instance.get(id)?;
println!("{}", item);
return Ok(());
}

View File

@@ -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(())
}

View File

@@ -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::<String>("store") {
Some(a) => PathBuf::from(a),
@@ -63,4 +64,5 @@ pub fn daemon(sub_matches: &ArgMatches) {
},
}
*/
return Ok(());
}

View File

@@ -1,4 +0,0 @@
use clap::ArgMatches;
pub fn serve(sub_matches: &ArgMatches) {
}

View File

@@ -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(())
}

View File

@@ -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(())
}

View File

@@ -0,0 +1,6 @@
use clap::ArgMatches;
use mize::error::MizeResult;
pub fn stop(sub_matches: &ArgMatches) -> MizeResult<()> {
Ok(())
}

View File

@@ -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")

32
src/platform/os/mod.rs Normal file
View File

@@ -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<S: Store>(instance: 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 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(());
}