1 | | - | use std::io::{self, BufRead}; |
2 | | - | use std::process::Command; |
| 1 | + | use std::io::{self, BufRead, Write}; |
| 2 | + | use std::process::{Command, Stdio}; |
3 | 3 | | |
4 | 4 | | use clap::Parser; |
5 | 5 | | use netstat2::*; |
| skipped 120 lines |
126 | 126 | | /// Poll variable by name at regular intervals. |
127 | 127 | | #[clap(long)] |
128 | 128 | | poll_variable: Option<String>, |
129 | | - | /// If specified along with --poll-variable, the variable value will be passed to this command |
| 129 | + | /// If specified along with --poll-variable, this command will be executed and variable values passed to its standard input. |
130 | 130 | | #[clap(long)] |
131 | 131 | | poll_command: Option<String>, |
132 | 132 | | } |
| skipped 103 lines |
236 | 236 | | client.send_message(&Message::text(payload)).unwrap(); |
237 | 237 | | |
238 | 238 | | println!("payload sent!"); |
239 | | - | |
240 | | - | println!("reading events, press ctrl+c to exit ...\n"); |
241 | | - | |
242 | | - | // first read Runtime.eval result |
243 | | - | println!("{:?}", client.recv_message().unwrap()); |
244 | 239 | | |
245 | 240 | | let varialble_payload = args.poll_variable.map(|name| { |
246 | 241 | | serde_json::to_string(&protocol::requests::RuntimeEval::new(&format!( |
| skipped 3 lines |
250 | 245 | | .unwrap() |
251 | 246 | | }); |
252 | 247 | | |
253 | | - | println!(""); |
| 248 | + | let mut poll_process = match args.poll_command { |
| 249 | + | Some(cmd) => { |
| 250 | + | println!("starting process '{}'", cmd); |
| 251 | + | Some(Command::new(cmd).stdin(Stdio::piped()).spawn().unwrap()) |
| 252 | + | } |
| 253 | + | None => None, |
| 254 | + | }; |
| 255 | + | |
| 256 | + | println!("reading events, press ctrl+c to exit ...\n"); |
| 257 | + | |
| 258 | + | // first read Runtime.eval result |
| 259 | + | println!("{:?}", client.recv_message().unwrap()); |
| 260 | + | println!(); |
254 | 261 | | |
255 | 262 | | loop { |
256 | 263 | | if let Some(poll_payload) = &varialble_payload { |
| skipped 3 lines |
260 | 267 | | if let OwnedMessage::Text(data) = resp { |
261 | 268 | | let result: protocol::responses::ResultMessage = |
262 | 269 | | serde_json::from_str(&data).unwrap(); |
263 | | - | if let Some(ref poll_command) = args.poll_command { |
264 | | - | // println!("passing variable value to {} ...", poll_command); |
265 | | - | Command::new(poll_command) |
266 | | - | .arg(&result.result.result.value.unwrap_or("".to_owned())) |
267 | | - | .spawn() |
| 270 | + | |
| 271 | + | let res_value = result.result.result.value.unwrap_or_else(|| "".to_owned()); |
| 272 | + | |
| 273 | + | if let Some(ref mut child) = poll_process { |
| 274 | + | child |
| 275 | + | .stdin |
| 276 | + | .as_mut() |
268 | 277 | | .unwrap() |
269 | | - | .wait() |
| 278 | + | .write_all(format!("{}\n", &res_value).as_bytes()) |
270 | 279 | | .unwrap(); |
271 | 280 | | } else { |
272 | | - | println!("{}", result.result.result.value.unwrap_or("".to_owned())); |
| 281 | + | println!("{}", &res_value); |
273 | 282 | | } |
274 | 283 | | } else { |
275 | 284 | | println!("got non text message: {:?}", resp); |
| skipped 9 lines |