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,22 +92,28 @@ 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")
.arg(shell) for p in privilege {
.arg("-c") if command.starts_with(p){
.arg(command) let command = command.replace(p, "");
.spawn() std::process::Command::new(p)
.expect("failed to execute process") .arg(shell)
.wait() .arg("-c")
.expect("failed to wait on process"); .arg(command)
} else { .spawn()
std::process::Command::new(shell) .expect("failed to execute process")
.arg("-c") .wait()
.arg(command) .expect("failed to wait on process");
.spawn() return;
.expect("failed to execute process") }
.wait()
.expect("failed to wait on process");
} }
std::process::Command::new(shell)
.arg("-c")
.arg(command)
.spawn()
.expect("failed to execute process")
.wait()
.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 {