2024-12-07 23:43:51 +01:00
|
|
|
use crate::shell::{Data, command_output, elevate};
|
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));
|
2024-12-08 00:00:38 +01:00
|
|
|
if result.is_empty() {
|
|
|
|
|
return None
|
|
|
|
|
}
|
2024-12-07 22:26:10 +01:00
|
|
|
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));
|
2024-12-08 00:00:38 +01:00
|
|
|
if result.is_empty() {
|
|
|
|
|
return None
|
|
|
|
|
}
|
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() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"emerge" => {
|
|
|
|
|
if !data.has_executable("e-file") {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let result = command_output(shell, &format!("e-file /usr/bin/{}", executable));
|
2024-12-08 00:00:38 +01:00
|
|
|
if result.is_empty() {
|
|
|
|
|
return None
|
|
|
|
|
}
|
2024-12-07 22:26:10 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2024-12-08 00:00:38 +01:00
|
|
|
let result = command_output(shell, &format!("nix-locate 'bin/{}'", executable));
|
|
|
|
|
if result.is_empty() {
|
|
|
|
|
return None
|
|
|
|
|
}
|
2024-12-07 23:27:18 +01:00
|
|
|
let packages: Vec<String> = result
|
|
|
|
|
.lines()
|
2024-12-08 00:00:38 +01:00
|
|
|
.map(|line| line.split_whitespace().next().unwrap().to_string())
|
2024-12-07 23:27:18 +01:00
|
|
|
.collect();
|
|
|
|
|
if packages.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(packages)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-27 20:38:37 +01:00
|
|
|
"pacman" => {
|
2024-12-07 23:43:51 +01:00
|
|
|
// somehow it tries to always update, so very slow
|
|
|
|
|
// let result = if data.has_executable("pkgfile") {
|
|
|
|
|
// command_output(shell, &format!("pkgfile -b {}", executable))
|
|
|
|
|
let result = command_output(shell, &format!("pacman -Fq /usr/bin/{}", executable));
|
2024-12-08 00:00:38 +01:00
|
|
|
if result.is_empty() {
|
|
|
|
|
return None
|
|
|
|
|
}
|
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"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-07 23:43:51 +01:00
|
|
|
pub fn install_package(data: &mut Data, package_manager: &str, package: &str) -> bool {
|
|
|
|
|
let shell = &data.shell.clone();
|
|
|
|
|
let mut install = match package_manager {
|
|
|
|
|
"apt" => format!("apt install {}", package),
|
|
|
|
|
"dnf" => format!("dnf install {}", package),
|
|
|
|
|
"emerge" => format!("emerge {}", package),
|
2024-12-08 00:00:38 +01:00
|
|
|
"nix-env" => format!("nix-env -iA nixpkgs.{}", package),
|
2024-12-07 23:43:51 +01:00
|
|
|
"pacman" => format!("pacman -S {}", package),
|
|
|
|
|
"pkg" => format!("pkg install {}", package),
|
|
|
|
|
"yum" => format!("yum install {}", package),
|
|
|
|
|
"zypper" => format!("zypper install {}", package),
|
2024-11-27 20:38:37 +01:00
|
|
|
_ => unreachable!("Unsupported package manager"),
|
2024-12-07 22:26:10 +01:00
|
|
|
};
|
2024-12-07 23:43:51 +01:00
|
|
|
elevate(data, &mut install);
|
2024-12-07 22:26:10 +01:00
|
|
|
|
|
|
|
|
let result = Command::new(shell)
|
|
|
|
|
.arg("-c")
|
|
|
|
|
.arg(install)
|
2024-12-07 23:43:51 +01:00
|
|
|
.stdout(stderr())
|
|
|
|
|
.stderr(Stdio::inherit())
|
2024-12-07 22:26:10 +01:00
|
|
|
.status()
|
|
|
|
|
.expect("failed to execute process");
|
|
|
|
|
|
|
|
|
|
result.success()
|
2024-11-27 20:38:37 +01:00
|
|
|
}
|