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

@ -1,14 +1,12 @@
use crate::shell::{command_output, elevate, Data};
use colored::Colorize;
use std::io::stderr;
use std::process::Command;
use std::process::Stdio;
use colored::Colorize;
pub fn get_package_manager(data: &mut Data) -> Option<String> {
let package_managers = vec![
"apt", "dnf", "emerge", "nix", "pacman",
// "pkg",
// "yum",
"apt", "dnf", "emerge", "nix", "pacman", "yum",
// "zypper",
];
@ -29,7 +27,10 @@ pub fn get_packages(
match package_manager {
"apt" => {
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;
}
let result = command_output(
@ -67,7 +68,10 @@ pub fn get_packages(
}
"emerge" => {
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;
}
let result = command_output(shell, &format!("e-file /usr/bin/{}", executable));
@ -88,7 +92,10 @@ pub fn get_packages(
}
"nix" => {
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;
}
let result = command_output(shell, &format!("nix-locate 'bin/{}'", executable));
@ -132,8 +139,22 @@ pub fn get_packages(
Some(packages)
}
}
_ => {
match package_manager.ends_with("command-not-found") {
"yum" => {
let result = command_output(shell, &format!("yum provides {}", executable));
if result.is_empty() {
return None;
}
let packages: Vec<String> = result
.lines()
.map(|line| line.split_whitespace().next().unwrap().to_string())
.collect();
if packages.is_empty() {
None
} else {
Some(packages)
}
}
_ => match package_manager.ends_with("command-not-found") {
true => {
let result = command_output(shell, &format!("{} {}", package_manager, executable));
if result.is_empty() {
@ -147,11 +168,10 @@ pub fn get_packages(
.collect();
return Some(packages);
}
return None;
},
None
}
false => unreachable!("Unsupported package manager"),
}
}
},
}
}
@ -166,10 +186,8 @@ pub fn install_package(data: &mut Data, package_manager: &str, package: &str) ->
"pkg" => format!("pkg install {}", package),
"yum" => format!("yum install {}", package),
"zypper" => format!("zypper install {}", package),
_ => {
match package_manager.ends_with("command-not-found") {
true => {
match package.starts_with("Command") {
_ => match package_manager.ends_with("command-not-found") {
true => match package.starts_with("Command") {
false => package.to_string(),
true => {
let split = package.split_whitespace().collect::<Vec<&str>>();
@ -179,10 +197,8 @@ pub fn install_package(data: &mut Data, package_manager: &str, package: &str) ->
data.update_command(&new_command);
format!("apt install {}", package)
}
}
},
false => unreachable!("Unsupported package manager"),
}
},
};
elevate(data, &mut install);