better error handeling for the terminal
This commit is contained in:
parent
c77752c05c
commit
877beb06f4
|
@ -7,46 +7,54 @@ use std::io::BufReader;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::os::fd::AsRawFd;
|
use std::os::fd::AsRawFd;
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use std::{env, path::Path, process::Command};
|
use std::{env, path::Path, path::PathBuf, process::Command};
|
||||||
|
|
||||||
|
pub struct Buffer {
|
||||||
|
pub output_buffer: BufReader<std::process::ChildStdout>,
|
||||||
|
pub error_buffer: BufReader<std::process::ChildStderr>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CommandEntry {
|
pub struct CommandEntry {
|
||||||
pub env: String,
|
pub env: String,
|
||||||
pub command: String,
|
pub command: String,
|
||||||
pub output: String,
|
pub output: String,
|
||||||
pub error: String,
|
pub error: String,
|
||||||
pub output_buffer: BufReader<std::process::ChildStdout>,
|
pub buffer: Option<Buffer>,
|
||||||
pub error_buffer: BufReader<std::process::ChildStderr>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandEntry {
|
impl CommandEntry {
|
||||||
pub fn new(command: String) -> Self {
|
pub fn new(command: String) -> Self {
|
||||||
let (stdout_reader, stderr_reader) = execute(command.clone());
|
let (buffer, error) = match execute(command.clone()) {
|
||||||
|
Ok(command_buffer) => (Some(command_buffer), String::new()),
|
||||||
|
Err(err) => (None, format!("failed to get results: {}", err)),
|
||||||
|
};
|
||||||
|
|
||||||
CommandEntry {
|
CommandEntry {
|
||||||
env: format_path(&env::current_dir().expect("Could not find Shell Environnment")),
|
env: format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))),
|
||||||
command,
|
command,
|
||||||
output: String::new(),
|
output: String::new(),
|
||||||
error: String::new(),
|
error,
|
||||||
output_buffer: stdout_reader,
|
buffer,
|
||||||
error_buffer: stderr_reader,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
for line in self.output_buffer.by_ref().lines() {
|
if let Some(buffer) = &mut self.buffer {
|
||||||
|
for line in buffer.output_buffer.by_ref().lines() {
|
||||||
match line {
|
match line {
|
||||||
Ok(line) => self.output += &format!("{}\n", line),
|
Ok(line) => self.output += &format!("{}\n", line),
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for line in self.error_buffer.by_ref().lines() {
|
for line in buffer.error_buffer.by_ref().lines() {
|
||||||
match line {
|
match line {
|
||||||
Ok(line) => self.error += &format!("{}\n", line),
|
Ok(line) => self.error += &format!("{}\n", line),
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_command(command: String) -> CommandEntry {
|
pub fn send_command(command: String) -> CommandEntry {
|
||||||
|
@ -85,30 +93,34 @@ pub fn send_command(command: String) -> CommandEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn execute(
|
pub fn execute(command: String) -> Result<Buffer, std::io::Error> {
|
||||||
command: String,
|
|
||||||
) -> (
|
|
||||||
BufReader<std::process::ChildStdout>,
|
|
||||||
BufReader<std::process::ChildStderr>,
|
|
||||||
) {
|
|
||||||
let mut child = Command::new("sh")
|
let mut child = Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(command.clone())
|
.arg(command.clone())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
.spawn()
|
.spawn()?;
|
||||||
.expect("failed to execute process");
|
|
||||||
|
|
||||||
let stdout = child.stdout.take().unwrap();
|
let stdout = child
|
||||||
let stderr = child.stderr.take().unwrap();
|
.stdout
|
||||||
|
.take()
|
||||||
|
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stdout"))?;
|
||||||
|
let stderr = child
|
||||||
|
.stderr
|
||||||
|
.take()
|
||||||
|
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "Failed to open stderr"))?;
|
||||||
|
|
||||||
let stdout_fd = stdout.as_raw_fd();
|
let stdout_fd = stdout.as_raw_fd();
|
||||||
let stderr_fd = stderr.as_raw_fd();
|
let stderr_fd = stderr.as_raw_fd();
|
||||||
|
|
||||||
fcntl(stdout_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))
|
fcntl(stdout_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?;
|
||||||
.expect("Failed to set non-blocking mode");
|
fcntl(stderr_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))?;
|
||||||
fcntl(stderr_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))
|
|
||||||
.expect("Failed to set non-blocking mode");
|
|
||||||
|
|
||||||
(BufReader::new(stdout), BufReader::new(stderr))
|
let output_buffer = BufReader::new(stdout);
|
||||||
|
let error_buffer = BufReader::new(stderr);
|
||||||
|
|
||||||
|
Ok(Buffer {
|
||||||
|
output_buffer,
|
||||||
|
error_buffer,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue