feat: adding yum integration

This commit is contained in:
iff 2024-12-08 11:32:39 +01:00
parent 43dc34a24f
commit c55c714b4b
2 changed files with 60 additions and 44 deletions

View file

@ -235,9 +235,9 @@ pub fn command_output(shell: &str, command: &str) -> String {
.output() .output()
.expect("failed to execute process"); .expect("failed to execute process");
match output.stdout.is_empty() { match output.stdout.is_empty() {
false => String::from_utf8_lossy(&output.stdout).to_lowercase(), false => String::from_utf8_lossy(&output.stdout).to_lowercase(),
true => String::from_utf8_lossy(&output.stderr).to_lowercase(), true => String::from_utf8_lossy(&output.stderr).to_lowercase(),
} }
} }

View file

@ -1,14 +1,12 @@
use crate::shell::{command_output, elevate, Data}; use crate::shell::{command_output, elevate, Data};
use colored::Colorize;
use std::io::stderr; use std::io::stderr;
use std::process::Command; use std::process::Command;
use std::process::Stdio; use std::process::Stdio;
use colored::Colorize;
pub fn get_package_manager(data: &mut Data) -> Option<String> { pub fn get_package_manager(data: &mut Data) -> Option<String> {
let package_managers = vec![ let package_managers = vec![
"apt", "dnf", "emerge", "nix", "pacman", "apt", "dnf", "emerge", "nix", "pacman", "yum",
// "pkg",
// "yum",
// "zypper", // "zypper",
]; ];
@ -29,7 +27,10 @@ pub fn get_packages(
match package_manager { match package_manager {
"apt" => { "apt" => {
if !data.has_executable("apt-file") { if !data.has_executable("apt-file") {
eprintln!("{}: apt-file is required to find packages", "pay-respects".yellow()); eprintln!(
"{}: apt-file is required to find packages",
"pay-respects".yellow()
);
return None; return None;
} }
let result = command_output( let result = command_output(
@ -67,7 +68,10 @@ pub fn get_packages(
} }
"emerge" => { "emerge" => {
if !data.has_executable("e-file") { if !data.has_executable("e-file") {
eprintln!("{}: pfl is required to find packages", "pay-respects".yellow()); eprintln!(
"{}: pfl is required to find packages",
"pay-respects".yellow()
);
return None; return None;
} }
let result = command_output(shell, &format!("e-file /usr/bin/{}", executable)); let result = command_output(shell, &format!("e-file /usr/bin/{}", executable));
@ -88,7 +92,10 @@ pub fn get_packages(
} }
"nix" => { "nix" => {
if !data.has_executable("nix-locate") { if !data.has_executable("nix-locate") {
eprintln!("{}: nix-index is required to find packages", "pay-respects".yellow()); eprintln!(
"{}: nix-index is required to find packages",
"pay-respects".yellow()
);
return None; return None;
} }
let result = command_output(shell, &format!("nix-locate 'bin/{}'", executable)); let result = command_output(shell, &format!("nix-locate 'bin/{}'", executable));
@ -132,26 +139,39 @@ pub fn get_packages(
Some(packages) Some(packages)
} }
} }
_ => { "yum" => {
match package_manager.ends_with("command-not-found") { let result = command_output(shell, &format!("yum provides {}", executable));
true => { if result.is_empty() {
let result = command_output(shell, &format!("{} {}", package_manager, executable)); return None;
if result.is_empty() { }
return None; let packages: Vec<String> = result
} .lines()
if result.contains("did you mean") || result.contains("is not installed") { .map(|line| line.split_whitespace().next().unwrap().to_string())
let packages = result .collect();
.lines() if packages.is_empty() {
.skip(1) None
.map(|line| line.to_string()) } else {
.collect(); Some(packages)
return Some(packages);
}
return None;
},
false => unreachable!("Unsupported package manager"),
} }
} }
_ => match package_manager.ends_with("command-not-found") {
true => {
let result = command_output(shell, &format!("{} {}", package_manager, executable));
if result.is_empty() {
return None;
}
if result.contains("did you mean") || result.contains("is not installed") {
let packages = result
.lines()
.skip(1)
.map(|line| line.to_string())
.collect();
return Some(packages);
}
None
}
false => unreachable!("Unsupported package manager"),
},
} }
} }
@ -166,23 +186,19 @@ pub fn install_package(data: &mut Data, package_manager: &str, package: &str) ->
"pkg" => format!("pkg install {}", package), "pkg" => format!("pkg install {}", package),
"yum" => format!("yum install {}", package), "yum" => format!("yum install {}", package),
"zypper" => format!("zypper install {}", package), "zypper" => format!("zypper install {}", package),
_ => { _ => match package_manager.ends_with("command-not-found") {
match package_manager.ends_with("command-not-found") { true => match package.starts_with("Command") {
false => package.to_string(),
true => { true => {
match package.starts_with("Command") { let split = package.split_whitespace().collect::<Vec<&str>>();
false => package.to_string(), let command = split[1];
true => { let package = split[split.len() - 1];
let split = package.split_whitespace().collect::<Vec<&str>>(); let new_command = data.command.clone().replacen(command, package, 1);
let command = split[1]; data.update_command(&new_command);
let package = split[split.len() - 1]; format!("apt install {}", package)
let new_command = data.command.clone().replacen(command, package, 1); }
data.update_command(&new_command); },
format!("apt install {}", package) false => unreachable!("Unsupported package manager"),
}
}
},
false => unreachable!("Unsupported package manager"),
}
}, },
}; };
elevate(data, &mut install); elevate(data, &mut install);