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 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,8 +139,22 @@ 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));
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 => { true => {
let result = command_output(shell, &format!("{} {}", package_manager, executable)); let result = command_output(shell, &format!("{} {}", package_manager, executable));
if result.is_empty() { if result.is_empty() {
@ -147,11 +168,10 @@ pub fn get_packages(
.collect(); .collect();
return Some(packages); return Some(packages);
} }
return None; None
}, }
false => unreachable!("Unsupported package manager"), 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), "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") {
true => {
match package.starts_with("Command") {
false => package.to_string(), false => package.to_string(),
true => { true => {
let split = package.split_whitespace().collect::<Vec<&str>>(); 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); data.update_command(&new_command);
format!("apt install {}", package) format!("apt install {}", package)
} }
}
}, },
false => unreachable!("Unsupported package manager"), false => unreachable!("Unsupported package manager"),
}
}, },
}; };
elevate(data, &mut install); elevate(data, &mut install);