mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-13 06:50:09 +01:00
feat: timeout if the execution takes too long
This commit is contained in:
parent
18f5627587
commit
985963c230
1 changed files with 34 additions and 12 deletions
28
src/shell.rs
28
src/shell.rs
|
|
@ -1,16 +1,31 @@
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
|
|
||||||
|
use std::sync::mpsc::channel;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
|
pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"];
|
||||||
|
|
||||||
pub fn command_output(shell: &str, command: &str) -> String {
|
pub fn command_output(shell: &str, command: &str) -> String {
|
||||||
let output = std::process::Command::new(shell)
|
|
||||||
|
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("-c")
|
||||||
.arg(command)
|
.arg(_command)
|
||||||
.env("LC_ALL", "C")
|
.env("LC_ALL", "C")
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process");
|
.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)
|
String::from_utf8_lossy(&output.stderr)
|
||||||
.to_string()
|
.to_string()
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
|
|
@ -18,6 +33,13 @@ pub fn command_output(shell: &str, command: &str) -> String {
|
||||||
.join(" ")
|
.join(" ")
|
||||||
.to_lowercase()
|
.to_lowercase()
|
||||||
}
|
}
|
||||||
|
Err(_) => {
|
||||||
|
use colored::*;
|
||||||
|
eprintln!("Timeout while executing command: {}", command.red());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn last_command(shell: &str) -> String {
|
fn last_command(shell: &str) -> String {
|
||||||
let last_command = std::env::var("_PR_LAST_COMMAND").expect("No _PR_LAST_COMMAND in environment. Did you aliased the command with the correct argument?");
|
let last_command = std::env::var("_PR_LAST_COMMAND").expect("No _PR_LAST_COMMAND in environment. Did you aliased the command with the correct argument?");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue