live-cli/src/command.rs

64 lines
1.5 KiB
Rust
Raw Normal View History

2022-12-15 23:19:17 -05:00
use std::{
io::Write,
process::{Command, Stdio},
};
use crate::App;
2022-12-16 21:28:40 -05:00
pub struct CommandResult {
2022-12-15 23:19:17 -05:00
pub status_success: bool,
2022-12-16 21:28:40 -05:00
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
2022-12-15 23:19:17 -05:00
}
2022-12-16 21:28:40 -05:00
impl Default for CommandResult {
2022-12-15 23:19:17 -05:00
fn default() -> Self {
Self {
status_success: true,
2022-12-16 21:28:40 -05:00
stdout: vec![],
stderr: vec![],
2022-12-15 23:19:17 -05:00
}
}
}
pub fn run(app: &mut App) {
2022-12-16 20:41:16 -05:00
let args = match shellwords::split(&app.cmdline) {
Ok(a) => a,
Err(e) => {
app.command_result = CommandResult {
status_success: false,
2022-12-16 21:28:40 -05:00
stdout: vec![],
stderr: format!("Argument error: {e:?}").as_bytes().to_vec(),
2022-12-16 20:41:16 -05:00
};
return;
}
};
let mut child = Command::new(&app.hidden_command)
.args(&app.hidden_options)
// .arg(&app.cmdline)
.args(args)
2022-12-15 23:19:17 -05:00
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Could not spawn child process");
let mut stdin = child.stdin.take().expect("Could not take stdin");
let text_orig_clone = app.text_orig.clone();
std::thread::spawn(move || {
stdin
.write_all(text_orig_clone.as_bytes())
.expect("Failed to write to stdin");
});
// Collect the output
let output = child.wait_with_output().expect("Failed to read stdout");
app.command_result = CommandResult {
status_success: output.status.success(),
2022-12-16 21:28:40 -05:00
stdout: output.stdout,
stderr: output.stderr,
2022-12-15 23:19:17 -05:00
}
}