refactor: use scoped thread to avoid copy

This commit is contained in:
iff 2025-01-03 15:57:58 +01:00
parent d044164f26
commit 46c1aa62ce

View file

@ -263,32 +263,32 @@ pub fn get_error(shell: &str, command: &str) -> String {
pub fn error_output_threaded(shell: &str, command: &str) -> String { pub fn error_output_threaded(shell: &str, command: &str) -> String {
let (sender, receiver) = channel(); let (sender, receiver) = channel();
let _shell = shell.to_owned(); thread::scope(|s| {
let _command = command.to_owned(); s.spawn(|| {
thread::spawn(move || { sender
sender .send(
.send( std::process::Command::new(shell)
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");
.expect("failed to send output"); });
});
match receiver.recv_timeout(Duration::from_secs(3)) { match receiver.recv_timeout(Duration::from_secs(3)) {
Ok(output) => match output.stderr.is_empty() { Ok(output) => match output.stderr.is_empty() {
true => String::from_utf8_lossy(&output.stdout).to_lowercase(), true => String::from_utf8_lossy(&output.stdout).to_lowercase(),
false => String::from_utf8_lossy(&output.stderr).to_lowercase(), false => String::from_utf8_lossy(&output.stderr).to_lowercase(),
}, },
Err(_) => { Err(_) => {
use colored::*; use colored::*;
eprintln!("Timeout while executing command: {}", command.red()); eprintln!("Timeout while executing command: {}", command.red());
exit(1); exit(1);
}
} }
} })
} }
pub fn command_output(shell: &str, command: &str) -> String { pub fn command_output(shell: &str, command: &str) -> String {