feat: add detach option on run

This commit is contained in:
zawz 2023-08-28 14:18:07 +02:00
parent 11509109be
commit 1a5bb96e41
4 changed files with 23 additions and 4 deletions

View file

@ -33,7 +33,9 @@ impl DeviceConfig {
let mut r = Vec::new();
if let Some(ev) = v {
for e in ev {
r.push( e.run(Event::new().make_env(None, false)?.to_map(e.envconf.as_ref()))? ) ;
if let Some(v) = e.run(Event::new().make_env(None, false)?.to_map(e.envconf.as_ref()))? {
r.push(v);
}
}
}
Ok(r)

View file

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::process::Command;
use std::process::{Command, ExitStatus};
use super::serializer::RunConfigSerializer;
use super::EventEnvMap;
@ -8,15 +8,24 @@ use super::EventEnvMap;
pub struct RunConfig {
pub args: Vec<String>,
pub envconf: Option<EventEnvMap>,
pub detach: bool,
}
impl RunConfig {
pub fn run(&self, env: HashMap<&str, String>) -> Result<std::process::ExitStatus, std::io::Error> {
pub fn run(&self, env: HashMap<&str, String>) -> Result<Option<ExitStatus>, std::io::Error> {
let mut c = Command::new(&self.args[0]);
if self.args.len() > 1 {
c.args(&self.args[1..]);
}
c.envs(env).status()
c.envs(env);
if self.detach {
std::thread::spawn(move || {
c.status()
});
Ok(None)
} else {
c.status().map(|v| Some(v))
}
}
}
@ -36,6 +45,7 @@ impl TryFrom<RunConfigSerializer> for RunConfig {
RunConfig {
args,
envconf: v.envconf,
detach: v.detach.unwrap_or(false),
}
)
}

View file

@ -8,4 +8,5 @@ pub struct RunConfigSerializer {
pub args: Option<Vec<String>>,
pub cmd: Option<String>,
pub envconf: Option<EventEnvSerializer>,
pub detach: Option<bool>,
}

View file

@ -23,6 +23,12 @@ devices:
channel: 0
run:
- cmd: "echo [$channel] NoteOn $id $value $raw"
- type: NoteOff
id: 30-127
channel: 0
run:
- cmd: "for I in $(seq 1 3) ; do echo wait $I ; sleep 1; done"
detach: true
- type: PitchBend
remap: 0-100
float: true