mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-11 22:10:09 +01:00
feat: more package managers support
This commit is contained in:
parent
d10c3a55d3
commit
aaad3a17b0
2 changed files with 99 additions and 34 deletions
15
src/shell.rs
15
src/shell.rs
|
|
@ -173,12 +173,12 @@ pub fn get_error(shell: &str, command: &str) -> String {
|
|||
std::env::remove_var("_PR_ERROR_MSG");
|
||||
error_msg
|
||||
} else {
|
||||
command_output(shell, command)
|
||||
command_output_threaded(shell, command)
|
||||
};
|
||||
error.split_whitespace().collect::<Vec<&str>>().join(" ")
|
||||
}
|
||||
|
||||
pub fn command_output(shell: &str, command: &str) -> String {
|
||||
pub fn command_output_threaded(shell: &str, command: &str) -> String {
|
||||
let (sender, receiver) = channel();
|
||||
|
||||
let _shell = shell.to_owned();
|
||||
|
|
@ -209,6 +209,17 @@ pub fn command_output(shell: &str, command: &str) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn command_output(shell: &str, command: &str) -> String {
|
||||
let output = std::process::Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(command)
|
||||
.env("LC_ALL", "C")
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
String::from_utf8_lossy(&output.stdout).to_string()
|
||||
}
|
||||
|
||||
pub fn last_command(shell: &str) -> String {
|
||||
let last_command = match std::env::var("_PR_LAST_COMMAND") {
|
||||
Ok(command) => command,
|
||||
|
|
|
|||
118
src/system.rs
118
src/system.rs
|
|
@ -1,10 +1,19 @@
|
|||
use crate::shell::Data;
|
||||
use crate::shell::{Data, command_output};
|
||||
use std::io::stderr;
|
||||
use std::process::Command;
|
||||
use std::process::Stdio;
|
||||
|
||||
pub fn get_package_manager(data: &mut Data) -> Option<String> {
|
||||
let package_managers = vec!["pacman"];
|
||||
let package_managers = vec![
|
||||
"apt",
|
||||
"dnf",
|
||||
"emerge",
|
||||
// "nix-env",
|
||||
"pacman",
|
||||
// "pkg",
|
||||
// "yum",
|
||||
// "zypper",
|
||||
];
|
||||
|
||||
for package_manager in package_managers {
|
||||
if data.has_executable(package_manager) {
|
||||
|
|
@ -17,46 +26,91 @@ pub fn get_package_manager(data: &mut Data) -> Option<String> {
|
|||
pub fn get_packages(data: &mut Data, package_manager: &str, executable: &str) -> Option<Vec<String>> {
|
||||
let shell = &data.shell.clone();
|
||||
match package_manager {
|
||||
"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)
|
||||
}
|
||||
},
|
||||
"pacman" => {
|
||||
let result = if data.has_executable("pkgfile") {
|
||||
Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(format!("pkgfile -b {}", executable))
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
command_output(shell, &format!("pkgfile -b {}", executable))
|
||||
} else {
|
||||
Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(format!("pacman -Fq /usr/bin/{}", executable))
|
||||
.output()
|
||||
.expect("failed to execute process")
|
||||
command_output(shell, &format!("pacman -Fq /usr/bin/{}", executable))
|
||||
};
|
||||
if result.status.success() {
|
||||
let output = String::from_utf8_lossy(&result.stdout)
|
||||
.lines()
|
||||
.map(|line| line.split_whitespace().next().unwrap().to_string())
|
||||
.collect();
|
||||
Some(output)
|
||||
} else {
|
||||
let packages: Vec<String> = result
|
||||
.lines()
|
||||
.map(|line| line.split_whitespace().next().unwrap().to_string())
|
||||
.collect();
|
||||
if packages.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(packages)
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => unreachable!("Unsupported package manager"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn install_package(shell: &str, package_manager: &str, package: &str) -> bool {
|
||||
match package_manager {
|
||||
"pacman" => Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(format!("sudo pacman -S {}", package))
|
||||
.stdout(stderr())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()
|
||||
.expect("failed to execute process")
|
||||
.wait()
|
||||
.unwrap()
|
||||
.success(),
|
||||
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),
|
||||
_ => unreachable!("Unsupported package manager"),
|
||||
}
|
||||
};
|
||||
|
||||
let result = Command::new(shell)
|
||||
.arg("-c")
|
||||
.arg(install)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.expect("failed to execute process");
|
||||
|
||||
result.success()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue