mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 14:30:10 +01:00
feat: support doas
This commit is contained in:
parent
6bd55fb316
commit
ad0eaa17fc
4 changed files with 46 additions and 18 deletions
|
|
@ -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.");
|
||||
std::io::stdin().read_line(&mut String::new()).unwrap();
|
||||
|
||||
if command.starts_with("sudo") {
|
||||
std::process::Command::new("sudo")
|
||||
let privilege = Vec::from(["sudo", "doas"]);
|
||||
|
||||
for p in privilege {
|
||||
if command.starts_with(p){
|
||||
let command = command.replace(p, "");
|
||||
std::process::Command::new(p)
|
||||
.arg(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
|
|
@ -101,7 +105,10 @@ pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
|
|||
.expect("failed to execute process")
|
||||
.wait()
|
||||
.expect("failed to wait on process");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::process::Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
|
|
@ -109,5 +116,4 @@ pub fn confirm_correction(shell: &str, command: &str, last_command: &str) {
|
|||
.expect("failed to execute process")
|
||||
.wait()
|
||||
.expect("failed to wait on process");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
src/main.rs
13
src/main.rs
|
|
@ -2,13 +2,24 @@ mod corrections;
|
|||
mod shell;
|
||||
mod style;
|
||||
|
||||
use shell::get_privilege;
|
||||
|
||||
fn main() {
|
||||
std::env::set_var("LC_ALL", "C");
|
||||
|
||||
let shell = shell::find_shell();
|
||||
let last_command = shell::find_last_command(&shell);
|
||||
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);
|
||||
} else {
|
||||
println!("No correction found.");
|
||||
|
|
|
|||
10
src/shell.rs
10
src/shell.rs
|
|
@ -71,3 +71,13 @@ fn shell_default_history_file(shell: &str) -> String {
|
|||
let file = shell_file_map.get(shell).expect("Unsupported shell.");
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
use colored::*;
|
||||
|
||||
pub fn highlight_difference(corrected_command: &str, last_command: &str) -> String {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue