2024-12-07 22:26:10 +01:00
|
|
|
use crate::shell::{Data, command_output};
|
2024-11-27 20:38:37 +01:00
|
|
|
use std::io::stderr;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use std::process::Stdio;
|
|
|
|
|
|
2024-12-07 17:14:47 +01:00
|
|
|
pub fn get_package_manager(data: &mut Data) -> Option<String> {
|
2024-12-07 22:26:10 +01:00
|
|
|
let package_managers = vec![
|
|
|
|
|
"apt",
|
|
|
|
|
"dnf",
|
|
|
|
|
"emerge",
|
2024-12-07 23:27:18 +01:00
|
|
|
"nix-env",
|
2024-12-07 22:26:10 +01:00
|
|
|
"pacman",
|
|
|
|
|
// "pkg",
|
|
|
|
|
// "yum",
|
|
|
|
|
// "zypper",
|
|
|
|
|
];
|
2024-11-27 20:38:37 +01:00
|
|
|
|
|
|
|
|
for package_manager in package_managers {
|
2024-12-07 17:14:47 +01:00
|
|
|
if data.has_executable(package_manager) {
|
2024-11-27 20:38:37 +01:00
|
|
|
return Some(package_manager.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 21:48:54 +01:00
|
|
|
pub fn get_packages(data: &mut Data, package_manager: &str, executable: &str) -> Option<Vec<String>> {
|
|
|
|
|
let shell = &data.shell.clone();
|
2024-11-27 20:38:37 +01:00
|
|
|
match package_manager {
|
2024-12-07 22:26:10 +01:00
|
|
|
"apt" => {
|
|
|
|
|
if !data.has_executable("dpkg") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let result = command_output(shell, &format!("dpkg -S '*/bin/{}'", executable));
|
|
|
|
|
let packages: Vec<String> = result
|
|
|
|
|
.lines()
|
|
|
|
|
.map(|line| line[..line.find(':').unwrap()].to_string())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if packages.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"dnf" => {
|
|
|
|
|
let result = command_output(shell, &format!("dnf provides {}", executable));
|
|
|
|
|
let packages: Vec<String> = result
|
|
|
|
|
.lines()
|
|
|
|
|
.map(|line| line.split_whitespace().next().unwrap().to_string())
|
|
|
|
|
.collect();
|
|
|
|
|
if packages.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"emerge" => {
|
|
|
|
|
if !data.has_executable("e-file") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let result = command_output(shell, &format!("e-file /usr/bin/{}", executable));
|
|
|
|
|
let mut packages = vec![];
|
|
|
|
|
for line in result.lines() {
|
|
|
|
|
if !line.starts_with(" ") {
|
|
|
|
|
packages.push(line.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if packages.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-12-07 23:27:18 +01:00
|
|
|
"nix-env" => {
|
|
|
|
|
if !data.has_executable("nix-locate") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let result = command_output(shell, &format!("nix-locate /usr/bin/{}", executable));
|
|
|
|
|
let packages: Vec<String> = result
|
|
|
|
|
.lines()
|
|
|
|
|
.map(|line| line.split_whitespace().next().unwrap().trim_end_matches(".out").to_string())
|
|
|
|
|
.collect();
|
|
|
|
|
if packages.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-27 20:38:37 +01:00
|
|
|
"pacman" => {
|
2024-12-07 21:48:54 +01:00
|
|
|
let result = if data.has_executable("pkgfile") {
|
2024-12-07 22:26:10 +01:00
|
|
|
command_output(shell, &format!("pkgfile -b {}", executable))
|
2024-12-07 21:48:54 +01:00
|
|
|
} else {
|
2024-12-07 22:26:10 +01:00
|
|
|
command_output(shell, &format!("pacman -Fq /usr/bin/{}", executable))
|
2024-12-07 21:48:54 +01:00
|
|
|
};
|
2024-12-07 22:26:10 +01:00
|
|
|
let packages: Vec<String> = result
|
|
|
|
|
.lines()
|
|
|
|
|
.map(|line| line.split_whitespace().next().unwrap().to_string())
|
|
|
|
|
.collect();
|
|
|
|
|
if packages.is_empty() {
|
2024-11-27 20:38:37 +01:00
|
|
|
None
|
2024-12-07 22:26:10 +01:00
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
2024-11-27 20:38:37 +01:00
|
|
|
}
|
2024-12-07 22:26:10 +01:00
|
|
|
},
|
2024-11-27 20:38:37 +01:00
|
|
|
_ => unreachable!("Unsupported package manager"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn install_package(shell: &str, package_manager: &str, package: &str) -> bool {
|
2024-12-07 22:26:10 +01:00
|
|
|
let install = match package_manager {
|
|
|
|
|
"apt" => format!("sudo apt install {}", package),
|
|
|
|
|
"dnf" => format!("sudo dnf install {}", package),
|
|
|
|
|
"emerge" => format!("sudo emerge {}", package),
|
|
|
|
|
"nix-env" => format!("nix-env -iA {}", package),
|
|
|
|
|
"pacman" => format!("sudo pacman -S {}", package),
|
|
|
|
|
"pkg" => format!("sudo pkg install {}", package),
|
|
|
|
|
"yum" => format!("sudo yum install {}", package),
|
|
|
|
|
"zypper" => format!("sudo zypper install {}", package),
|
2024-11-27 20:38:37 +01:00
|
|
|
_ => unreachable!("Unsupported package manager"),
|
2024-12-07 22:26:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let result = Command::new(shell)
|
|
|
|
|
.arg("-c")
|
|
|
|
|
.arg(install)
|
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
|
.status()
|
|
|
|
|
.expect("failed to execute process");
|
|
|
|
|
|
|
|
|
|
result.success()
|
2024-11-27 20:38:37 +01:00
|
|
|
}
|