feat: support doas

This commit is contained in:
iff 2023-07-31 01:58:37 +02:00
parent 6bd55fb316
commit ad0eaa17fc
4 changed files with 46 additions and 18 deletions

View file

@ -92,8 +92,12 @@ pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
println!("Press enter to execute the corrected command. Or press Ctrl+C to exit."); println!("Press enter to execute the corrected command. Or press Ctrl+C to exit.");
std::io::stdin().read_line(&mut String::new()).unwrap(); std::io::stdin().read_line(&mut String::new()).unwrap();
if command.starts_with("sudo") { let privilege = Vec::from(["sudo", "doas"]);
std::process::Command::new("sudo")
for p in privilege {
if command.starts_with(p){
let command = command.replace(p, "");
std::process::Command::new(p)
.arg(shell) .arg(shell)
.arg("-c") .arg("-c")
.arg(command) .arg(command)
@ -101,7 +105,10 @@ pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
.expect("failed to execute process") .expect("failed to execute process")
.wait() .wait()
.expect("failed to wait on process"); .expect("failed to wait on process");
} else { return;
}
}
std::process::Command::new(shell) std::process::Command::new(shell)
.arg("-c") .arg("-c")
.arg(command) .arg(command)
@ -110,4 +117,3 @@ pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
.wait() .wait()
.expect("failed to wait on process"); .expect("failed to wait on process");
} }
}

View file

@ -2,13 +2,24 @@ mod corrections;
mod shell; mod shell;
mod style; mod style;
use shell::get_privilege;
fn main() { fn main() {
std::env::set_var("LC_ALL", "C"); std::env::set_var("LC_ALL", "C");
let shell = shell::find_shell(); let shell = shell::find_shell();
let last_command = shell::find_last_command(&shell); let last_command = shell::find_last_command(&shell);
let corrected_command = corrections::correct_command(&shell, &last_command); let corrected_command = corrections::correct_command(&shell, &last_command);
if let Some(corrected_command) = corrected_command {
if let Some(mut corrected_command) = corrected_command {
if corrected_command.starts_with("sudo") {
let privilege = get_privilege();
if let Some(privilege) = privilege {
if privilege != "sudo" {
corrected_command = corrected_command.replacen("sudo", &privilege, 1);
}
}
}
corrections::confirm_correction(&shell, &corrected_command, &last_command); corrections::confirm_correction(&shell, &corrected_command, &last_command);
} else { } else {
println!("No correction found."); println!("No correction found.");

View file

@ -71,3 +71,13 @@ fn shell_default_history_file(shell: &str) -> String {
let file = shell_file_map.get(shell).expect("Unsupported shell."); let file = shell_file_map.get(shell).expect("Unsupported shell.");
format!("{}/{}", std::env::var("HOME").unwrap(), file) format!("{}/{}", std::env::var("HOME").unwrap(), file)
} }
pub fn get_privilege() -> Option<String> {
let list = vec!["sudo", "doas"];
for command in list {
if std::process::Command::new(command).output().is_ok() {
return Some(command.to_string());
}
}
None
}

View file

@ -1,3 +1,4 @@
use colored::*; use colored::*;
pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String { pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String {