diff --git a/apps/os/src/cli.rs b/apps/os/src/cli.rs index 824b125..9beca8e 100644 --- a/apps/os/src/cli.rs +++ b/apps/os/src/cli.rs @@ -1,4 +1,5 @@ use clap::ArgMatches; +use marts::CliPart; use core::result::Result::Err; use core::unimplemented; use mize::flume::bounded; @@ -66,20 +67,22 @@ pub fn info(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> { Err(mize_err!("not implemented")) } -pub fn run(sub_matches: &ArgMatches, instance: &mut Mize) -> MizeResult<()> { +pub fn run(sub_matches: &ArgMatches, mize: &mut Mize) -> MizeResult<()> { match sub_matches.subcommand() { - None => { - instance.wait(); + None => { // run a mize instance (does whatever it's config will say) + mize.wait(); } - Some((runnable, sub_matches)) => { - let ext_args: Vec = sub_matches + Some((runnable, sub_matches)) => { // run smth from a part. for now registered cli commands + let mut ext_args: Vec = sub_matches .get_many::("") .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); - println!("runnable: {}", runnable); - println!("args: {:?}", ext_args); + let mut cli = mize.get_part_native::("cli")?; + cli.run(ext_cmdline)?; } } diff --git a/apps/os/src/main.rs b/apps/os/src/main.rs index 95dbc25..e154abc 100644 --- a/apps/os/src/main.rs +++ b/apps/os/src/main.rs @@ -37,7 +37,10 @@ macro_rules! compile_in_parts { $( if enable_str.contains(&$name) { tracing::info!("enabling compiled_in_part: {}", $name); - $part(&mut $mize); + if let Err(e) = $part(&mut $mize) { + print!("part '{}' failed with: ", $name); + e.log(); + } } )+ }; @@ -66,6 +69,7 @@ fn tried_main() -> MizeResult<()> { //"matrix": marts::part_matrix, "ppc.server": marts::ppc::server::part_ppc_server, "c2vi": marts::part_c2vi, + "sci.timetrack": marts::timetrack::part_timetrack, }; diff --git a/marts/src/auth/client.rs b/marts/src/auth/client.rs index cffb9c0..490ccdf 100644 --- a/marts/src/auth/client.rs +++ b/marts/src/auth/client.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use std::fs::OpenOptions; use std::io::Write; use std::{path::PathBuf, thread}; -use tracing::info; +use tracing::{info, warn}; // console_log macro // that can be copied into other files for debugging purposes @@ -658,6 +658,11 @@ fn update_spacetimedb_token(new_token: &str) -> MizeResult<()> { let file_path = config_dir.join("spacetime").join("cli.toml"); + if !file_path.exists() { + warn!("spacetime config file does not exist, not updating token there"); + return Ok(()); + }; + // Read existing file (or create empty document if it doesn't exist) let content = std::fs::read_to_string(&file_path).unwrap_or_default(); let mut data = ItemData::from_toml(content.as_str())?; diff --git a/marts/src/cli/mod.rs b/marts/src/cli/mod.rs index 1633a56..173e11b 100644 --- a/marts/src/cli/mod.rs +++ b/marts/src/cli/mod.rs @@ -28,7 +28,17 @@ pub fn cli_spawn_setup(mize: Mize, conn_id: u64, wait_rx: flume::Receiver<()>) - }) } -pub fn cli_spawn(cmd: String) -> MizeResult<()> { +pub fn cli_spawn(mize: &mut Mize, cmd: String) -> MizeResult<()> { + // spawn locally + if !mize.get_config("cli.daemonMode").unwrap_or(ItemData::new()).as_bool().unwrap_or(false) { + std::process::Command::new("/bin/sh") + .arg("-c") + .arg(&cmd) + .status()?; + return Ok(()); + } + + // for spawning in the client if we are running in the daemon (which is eg attached to the users tty) MIZE_CLI_SPAWN.with(|cell| { if let Some(spawn) = cell.borrow().as_ref() { let conn_id = spawn.conn_id.ok_or(mize_err!("no conn_id set"))?; @@ -143,8 +153,8 @@ fn cli_msg_handler( mize: &mut Mize, spawn_wait_rx: flume::Receiver<()>, ) -> MizeResult<()> { - let binding = msg.data()?; - let data = binding + let tmp = msg.data()?; + let data = tmp .cbor() .as_array() .ok_or(mize_err!("cli message data was not an array"))?; @@ -274,4 +284,16 @@ impl CliPart { self.cmds.insert(self.current_cmd_name.clone(), cmd); Ok(()) } + pub fn run(&mut self, cmdline: Vec) -> MizeResult<()> { + let cmd_name = cmdline.iter().nth(0).ok_or(mize_err!("empty cmdline"))?; + let cmd = self.cmds.remove(cmd_name).ok_or(mize_err!("cmd {} not found in CliPart", cmd_name))?; + + let matches = cmd.get_matches_from(cmdline); + let sub_name = matches.subcommand_name().ok_or(mize_err!("subcommand not found"))?; + let sub_matches = matches.subcommand_matches(sub_name).ok_or(mize_err!("subcommand matches not found"))?; + let action = self.actions.remove(sub_name).ok_or(mize_err!("action for subcommand not found"))?; + action(sub_matches, self.mize.clone())?; + + Ok(()) + } } diff --git a/marts/src/timetrack/mod.rs b/marts/src/timetrack/mod.rs index 51f832a..94891cb 100644 --- a/marts/src/timetrack/mod.rs +++ b/marts/src/timetrack/mod.rs @@ -22,10 +22,10 @@ pub struct TimeTrack { mize: Mize, } -pub fn timetrack(mize: &mut Mize) -> MizeResult<()> { +pub fn part_timetrack(mize: &mut Mize) -> MizeResult<()> { #[cfg(feature = "target-os")] { let mut cli = mize.get_part_native::("cli")?; - cli.set_command("ppc"); + cli.set_command("sci"); cli.subcommand( Command::new("editActionList").alias("aal").arg( @@ -271,7 +271,7 @@ fn command_edit_action_list(matches: &ArgMatches, mut mize: Mize) -> MizeResult< .map_err(|e| mize_err!("Failed to write temp file: {}", e))?; // open $EDITOR blocking - crate::cli_spawn(format!("nvim {}", tmp_file.display()))?; + crate::cli_spawn(&mut mize, format!("nvim {}", tmp_file.display()))?; // read file and update db let new_file_content = std::fs::read_to_string(&tmp_file) @@ -381,7 +381,7 @@ fn command_edit_activities(_: &ArgMatches, mut mize: Mize) -> MizeResult<()> { loop { // open $EDITOR blocking - crate::cli_spawn(format!("nvim {}", tmp_file.display()))?; + crate::cli_spawn(&mut mize, format!("nvim {}", tmp_file.display()))?; let new_file_content = std::fs::read_to_string(&tmp_file) .map_err(|e| mize_err!("Failed to read temp file: {}", e))?; diff --git a/src/lib.rs b/src/lib.rs index 914dcb7..32dde86 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ thread_local! { macro_rules! out { ($($arg:tt)*) => { // format_args! handles string interpolation safely at compile time - if let Err(e) = $crate::write_to_stdout(format_args!($($arg)*)) { + if let Err(e) = $crate::write_to_mize_stdout(format_args!($($arg)*)) { // Choose your error handling behavior here (panic, print to stderr, or ignore) eprintln!("out! macro failed to write!!"); } @@ -31,7 +31,7 @@ pub fn out_macro_stdout() -> MizeResult<()> { Ok(()) } -pub fn write_to_stdout(args: std::fmt::Arguments) -> MizeResult<()> { +pub fn write_to_mize_stdout(args: std::fmt::Arguments) -> MizeResult<()> { MIZE_STDOUT.with(|stdout| { if let Some(fun) = stdout.borrow().as_ref() { fun(format!("{}", args));