Files
mize/apps/os/src/main.rs
2026-10-11 22:20:46 +02:00

168 lines
5.2 KiB
Rust

#![allow(warnings)]
use clap::builder::OsStr;
use clap::{crate_version, Arg, Command};
use clap::{ArgAction, ArgMatches};
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::exit;
use mize::error::{MizeError, MizeResult};
use mize::item::ItemData;
use mize::platform::os::config_from_cli_args;
use mize::platform::os::logging::init_logger;
use mize::{Mize, instance, mize_err};
use std::env;
use std::io::Write;
use std::sync::Arc;
use tracing::{debug, error, info, trace, warn};
mod cli;
static APPNAME: &str = "mize";
macro_rules! compile_in_parts {
(
$mize:ident,
$(
$name:literal : $part:path
),+ $(,)?
) => {
let enable = $mize.get_config("enable")?;
let mut enable_str = Vec::new();
for name in enable.cbor().as_array().ok_or(mize_err!("config(enable) not a list"))? {
enable_str.push(name.as_text().ok_or("part name not a str")?);
}
$(
if enable_str.contains(&$name) {
tracing::info!("enabling compiled_in_part: {}", $name);
if let Err(e) = $part(&mut $mize) {
print!("part '{}' failed with: ", $name);
e.log();
}
}
)+
};
}
fn main() {
if let Err(e) = tried_main() {
e.log();
}
}
fn tried_main() -> MizeResult<()> {
let cli_matches = cli_matches();
// logging
init_logger(&cli_matches);
// init mize
let mut mize = Mize::with_config(config_from_cli_args(&cli_matches)?)?;
// compiled in parts (which for now is all)
compile_in_parts! {
mize,
"cli": marts::cli,
"auth": marts::part_client_auth,
"spacetime": marts::spacetime,
//"matrix": marts::part_matrix,
"ppc.server": marts::ppc::server::part_ppc_server,
"c2vi": marts::part_c2vi,
"sci.timetrack": marts::timetrack::part_timetrack,
};
// match subcommand
match cli_matches.subcommand() {
Some(("run", sub_matches)) => cli::run(sub_matches, &mut mize),
Some(("status", sub_matches)) => cli::status(sub_matches, &mut mize),
Some(("info", sub_matches)) => cli::info(sub_matches, &mut mize),
Some(("get", sub_matches)) => cli::get(sub_matches, &mut mize),
Some(("set", sub_matches)) => cli::set(sub_matches, &mut mize),
Some(("show", sub_matches)) => cli::show(sub_matches, &mut mize),
Some(("call", sub_matches)) => cli::call(sub_matches, &mut mize),
Some(("create", sub_matches)) => cli::create(sub_matches, &mut mize),
Some(("format-cbor", sub_matches)) => cli::format_cbor(sub_matches, &mut mize),
Some((cmd, sub_matches)) => cli::run_from_part(cmd, sub_matches, &mut mize),
None => Err(MizeError::new()
.msg("No subcommand was passed. use --help to list availavle comamnds.")),
}?;
Ok(())
}
fn cli_matches() -> clap::ArgMatches {
let main = Command::new(APPNAME)
.version(crate_version!())
.author("Sebastian Moser")
.about("MiZe")
.allow_external_subcommands(true)
.arg(
Arg::new("verbose")
.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("log")
.long("log")
.help("log file to use")
.global(true),
)
.arg(
Arg::new("config")
.long("config")
.help("config file path")
.global(true),
)
.arg(
Arg::new("secrets")
.long("secrets")
.help("file path to store secrets at")
.global(true),
)
.arg(
Arg::new("opt")
.short('o')
.long("opt")
.help("overwrite a config option")
.global(true),
)
.subcommand(
Command::new("get")
.aliases(["g"])
.arg(Arg::new("id").help("The id or path to get"))
.arg(Arg::new("recurse").short('r').action(ArgAction::Count)),
)
.subcommand(
Command::new("set")
.aliases(["s"])
.arg(Arg::new("id").help("The id or path to set"))
.arg(Arg::new("value").help("The value to set the path to")),
)
.subcommand(
Command::new("show")
.aliases(["so"])
.arg(Arg::new("id").help("The id or path to sub to and show")),
)
.subcommand(Command::new("call").aliases(["c"]))
.subcommand(Command::new("create").aliases(["cr"]))
.subcommand(Command::new("status").aliases(["st"]))
.subcommand(Command::new("run").allow_external_subcommands(true))
.subcommand(Command::new("format-cbor").aliases(["fc"]))
.arg_required_else_help(true);
return main.get_matches();
}