From ec0886e338cb4422afbc86a93493c7117adfc547 Mon Sep 17 00:00:00 2001 From: Przemyslaw Walski Date: Fri, 27 Oct 2023 08:11:46 +0200 Subject: [PATCH] Runtime framework start with '--model' arg. --- runtimes/dummy/src/main.rs | 4 +++- src/main.rs | 17 +++++++++++++++-- src/process.rs | 6 ++++-- src/process/dummy.rs | 11 ++++++++--- src/process/win.rs | 1 + 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/runtimes/dummy/src/main.rs b/runtimes/dummy/src/main.rs index 74c5ab7..a83cf56 100644 --- a/runtimes/dummy/src/main.rs +++ b/runtimes/dummy/src/main.rs @@ -1,9 +1,11 @@ +use std::env; use std::io; fn main() { let mut input = String::new(); + let args: Vec = env::args().collect(); - println!("Dummy"); + println!("Dummy runtime. Args: {args:?}"); loop { input.clear(); diff --git a/src/main.rs b/src/main.rs index a43df34..36d9ff9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -238,10 +238,11 @@ async fn run(cli: Cli) -> anyhow: }); } ExeScriptCommand::Start { args, .. } => { - log::debug!("Start cmd args: {args:?}"); - let args = process::RuntimeArgs::new(args).map_err(|e| { + log::debug!("Raw Start cmd args: {args:?}"); + let args = T::parse_args(args).map_err(|e| { RpcMessageError::Activity(format!("invalid args: {}", e)) })?; + log::debug!("Start cmd model: {}", args.model); send_state( &report_url, @@ -255,6 +256,7 @@ async fn run(cli: Cli) -> anyhow: .start(&args) .await .map_err(|e| RpcMessageError::Activity(e.to_string()))?; + log::debug!("Started process"); send_state( &report_url, @@ -337,3 +339,14 @@ async fn run(cli: Cli) -> anyhow: Ok(()) } + +#[cfg(test)] +mod tests { + use crate::process::{self, RuntimeArgs}; + + #[test] + fn arg_test() { + let args = vec!["--model".into(), "dummy".into()]; + process::RuntimeArgs::new(&args).unwrap(); + } +} diff --git a/src/process.rs b/src/process.rs index 9eabe5f..769aeff 100644 --- a/src/process.rs +++ b/src/process.rs @@ -20,6 +20,8 @@ pub struct Shares { } pub trait Runtime { + fn parse_args(args: &[String]) -> anyhow::Result; + fn start(args: &RuntimeArgs) -> anyhow::Result; fn run(stdout: ChildStdout, report_fn: ReportFn); @@ -33,8 +35,8 @@ pub struct RuntimeArgs { } impl RuntimeArgs { - pub fn new(args: &[String]) -> anyhow::Result { - Ok(Self::try_parse_from(args)?) + pub fn new(cmd: &String, args: &[String]) -> anyhow::Result { + Ok(Self::try_parse_from(std::iter::once(cmd).chain(args))?) } } diff --git a/src/process/dummy.rs b/src/process/dummy.rs index 6c66c39..f6fdc58 100644 --- a/src/process/dummy.rs +++ b/src/process/dummy.rs @@ -11,14 +11,19 @@ pub struct Dummy {} impl Unpin for Dummy {} impl Runtime for Dummy { - fn start(_args: &RuntimeArgs) -> anyhow::Result { + fn parse_args(args: &[String]) -> anyhow::Result { + RuntimeArgs::new(&"dummy.exe".into(), args) + } + + fn start(args: &RuntimeArgs) -> anyhow::Result { let exe = super::find_exe("dummy.exe")?; let mut cmd = Command::new(&exe); let work_dir = exe.parent().unwrap(); cmd.stdout(Stdio::piped()) .stdin(Stdio::null()) - .current_dir(work_dir); - + .current_dir(work_dir) + .arg("--model") + .arg(&args.model); Ok(cmd.kill_on_drop(true).spawn()?) } diff --git a/src/process/win.rs b/src/process/win.rs index cbb480f..820c26d 100644 --- a/src/process/win.rs +++ b/src/process/win.rs @@ -7,6 +7,7 @@ use winapi::um::handleapi::INVALID_HANDLE_VALUE; #[cfg(target_os = "windows")] use winapi::um::winnt::HANDLE; +#[cfg(target_os = "windows")] use std::{mem, ptr}; #[cfg(target_os = "windows")]