live-cli/src/command.rs

108 lines
3.2 KiB
Rust
Raw Normal View History

2022-12-17 12:02:15 -05:00
use crate::event::EventMessage;
use crate::CommandOptions;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
2022-12-17 12:02:15 -05:00
use crossbeam::channel::Receiver;
use crossbeam::channel::Sender;
use parking_lot::RwLock;
2022-12-20 21:35:11 -05:00
use ropey::Rope;
use std::sync::Arc;
2022-12-15 23:19:17 -05:00
use std::{
io::Write,
process::{Command, Stdio},
};
pub type CommandRequest = (Arc<RwLock<CommandOptions>>, Arc<String>);
2022-12-15 23:19:17 -05:00
#[derive(Debug)]
pub enum CommandCompleted {
Success(CommandResult),
2022-12-20 21:35:11 -05:00
Failure(Rope),
2022-12-17 12:02:15 -05:00
}
#[derive(Debug)]
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-20 21:35:11 -05:00
pub stdout: Rope,
pub stderr: Rope,
2022-12-15 23:19:17 -05:00
}
pub fn command_event_loop(
command_request_receiver: Receiver<CommandRequest>,
event_sender: Sender<EventMessage>,
) -> Result<()> {
loop {
match command_request_receiver.recv() {
2022-12-22 14:30:12 -05:00
// TODO: Drain the command request channel so we only run the command once with the latest value
// Could use https://docs.rs/single_value_channel/latest/single_value_channel/
Ok(command_request) => {
event_sender.send(EventMessage::CommandCompleted(
2022-12-20 21:59:54 -05:00
match run_inner(&command_request) {
Ok(c) => {
// If there was no stdout and the command failed, don't touch stdout
2022-12-20 21:35:11 -05:00
if !c.status_success && c.stdout.len_bytes() == 0 {
CommandCompleted::Failure(c.stderr)
} else {
CommandCompleted::Success(c)
}
}
2022-12-20 21:35:11 -05:00
Err(e) => CommandCompleted::Failure(Rope::from_str(&e.to_string())),
},
))?;
}
Err(e) => {
event_sender.send(EventMessage::CommandLoopStopped)?;
bail!("Crossterm read error: {}", e);
}
}
}
}
2022-12-20 21:59:54 -05:00
fn run_inner(command_request: &CommandRequest) -> Result<CommandResult> {
// Spawn the child
let mut child = {
let request = command_request.0.read();
2022-12-16 20:41:16 -05:00
let mut command = Command::new(&request.command);
command
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.args(&request.hidden_options);
2022-12-17 11:25:15 -05:00
if request.wordsplit {
match shellwords::split(&request.cmdline) {
Ok(a) => {
command.args(a);
}
Err(e) => {
bail!("Argument error: {e:?}");
}
2022-12-17 11:25:15 -05:00
}
} else {
// TODO: Avoid cloning here
command.arg(&request.cmdline);
2022-12-17 11:25:15 -05:00
}
std::mem::drop(request);
command.spawn()
}?;
2022-12-15 23:19:17 -05:00
let mut stdin = child.stdin.take().context("Could not take stdin")?;
let text_orig_clone = command_request.1.clone();
2022-12-15 23:19:17 -05:00
std::thread::spawn(move || {
2022-12-17 00:10:17 -05:00
let _result = stdin.write_all(text_orig_clone.as_bytes());
2022-12-15 23:19:17 -05:00
});
// Collect the output
let output = child.wait_with_output()?;
2022-12-15 23:19:17 -05:00
Ok(CommandResult {
2022-12-15 23:19:17 -05:00
status_success: output.status.success(),
2022-12-20 21:35:11 -05:00
stdout: Rope::from_reader(&output.stdout[..])?,
stderr: Rope::from_reader(&output.stderr[..])?,
})
2022-12-15 23:19:17 -05:00
}