fix: some error handlings

This commit is contained in:
iff 2023-08-07 20:21:02 +02:00
parent 1444eeab84
commit 15c1ba3370
7 changed files with 49 additions and 37 deletions

View file

@ -1,7 +1,6 @@
use std::io::prelude::*;
use std::process::exit;
use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;
@ -9,30 +8,30 @@ use std::time::Duration;
pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
pub fn command_output(shell: &str, command: &str) -> String {
let (sender, receiver) = channel();
let _shell = shell.to_owned();
let _command = command.to_owned();
thread::spawn(move || {
sender.send(std::process::Command::new(_shell)
.arg("-c")
.arg(_command)
.env("LC_ALL", "C")
.output()
.expect("failed to execute process"))
sender
.send(
std::process::Command::new(_shell)
.arg("-c")
.arg(_command)
.env("LC_ALL", "C")
.output()
.expect("failed to execute process"),
)
.expect("failed to send output");
});
match receiver.recv_timeout(Duration::from_secs(3)) {
Ok(output) => {
String::from_utf8_lossy(&output.stderr)
.to_string()
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ")
.to_lowercase()
}
Ok(output) => String::from_utf8_lossy(&output.stderr)
.to_string()
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ")
.to_lowercase(),
Err(_) => {
use colored::*;
eprintln!("Timeout while executing command: {}", command.red());
@ -46,7 +45,7 @@ fn last_command(shell: &str) -> String {
match shell {
"bash" => {
let first_line = last_command.lines().next().unwrap().trim();
first_line.split_once(" ").unwrap().1.to_string()
first_line.split_once(' ').unwrap().1.to_string()
}
"zsh" => last_command,
"fish" => last_command,