add cargo backend

This commit is contained in:
Dr. Matthias Ratajczak
2023-01-05 17:25:39 +01:00
parent ec9fb8875d
commit 4c22f1cc89
3 changed files with 88 additions and 9 deletions
+8 -5
View File
@@ -1,4 +1,5 @@
mod pacman;
mod rust;
use std::collections::HashSet;
use std::os::unix::process::CommandExt;
@@ -7,16 +8,18 @@ use std::process::Command;
use crate::Package;
pub use pacman::Pacman;
type Switches = &'static [&'static str];
type Binary = &'static str;
pub trait Backend {
/// The binary that should be called to run the associated package manager.
const BINARY: &'static str;
const BINARY: Binary;
/// The switches that signals the `BINARY` that the packages should be installed.
const SWITCH_INSTALL: &'static str;
const SWITCHES_INSTALL: Switches;
/// The switches that signals the `BINARY` that the packages should be removed.
const SWITCH_REMOVE: &'static str;
const SWITCHES_REMOVE: Switches;
/// Get all packages that are installed in the system.
fn get_all_installed_packages() -> HashSet<Package>;
@@ -27,7 +30,7 @@ pub trait Backend {
/// Install the specified packages.
fn install_packages(packages: Vec<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.arg(Self::SWITCH_INSTALL);
cmd.args(Self::SWITCHES_INSTALL);
for p in packages {
cmd.arg(format!("{p}"));
}
@@ -37,7 +40,7 @@ pub trait Backend {
/// Remove the specified packages.
fn remove_packages(packages: Vec<Package>) {
let mut cmd = Command::new(Self::BINARY);
cmd.arg(Self::SWITCH_REMOVE);
cmd.args(Self::SWITCHES_REMOVE);
for p in packages {
cmd.arg(format!("{p}"));
}
+4 -4
View File
@@ -3,15 +3,15 @@ use std::collections::HashSet;
use alpm::Alpm;
use alpm::PackageReason::Explicit;
use super::Backend;
use super::{Backend, Binary, Switches};
use crate::Package;
pub struct Pacman;
impl Backend for Pacman {
const BINARY: &'static str = "paru";
const SWITCH_INSTALL: &'static str = "-S";
const SWITCH_REMOVE: &'static str = "-Rsn";
const BINARY: Binary = "paru";
const SWITCHES_INSTALL: Switches = &["-S"];
const SWITCHES_REMOVE: Switches = &["-Rsn"];
fn get_all_installed_packages() -> HashSet<Package> {
convert_to_pacdef_packages(get_all_installed_packages_from_alpm())
+76
View File
@@ -0,0 +1,76 @@
use std::{collections::HashSet, process::Command};
use super::{Backend, Binary, Switches};
use crate::Package;
pub struct Rust;
impl Backend for Rust {
const BINARY: Binary = "cargo";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_REMOVE: Switches = &["uninstall"];
fn get_all_installed_packages() -> HashSet<Package> {
extract_packages_names(&run_cargo_install_list())
.map(Package::from)
.collect()
}
fn get_explicitly_installed_packages() -> HashSet<Package> {
Self::get_all_installed_packages()
}
}
fn run_cargo_install_list() -> String {
let stdout = Command::new("cargo")
.args(["install", "--list"])
.output()
.unwrap()
.stdout;
String::from_utf8(stdout).unwrap()
}
fn extract_packages_names(output: &str) -> impl Iterator<Item = String> + '_ {
output
.lines()
.filter(|line| !line.starts_with(char::is_whitespace))
.map(|line| line.split_whitespace().next().unwrap().to_owned())
}
#[cfg(test)]
mod tests {
use super::extract_packages_names;
#[test]
fn test_extract_packages() {
const OUTPUT: &str = "cargo-audit v0.17.4:
cargo-audit
cargo-cache v0.8.3:
cargo-cache
cargo-criterion v1.1.0:
cargo-criterion
cargo-update v11.1.1:
cargo-install-update
cargo-install-update-config
flamegraph v0.6.2:
cargo-flamegraph
flamegraph
topgrade v10.1.2 (/home/ratajc72/tmp/topgrade):
topgrade
wthrr v0.6.1:
wthrr";
let extracted: Vec<String> = extract_packages_names(OUTPUT).collect();
assert_eq!(
&extracted,
&[
"cargo-audit",
"cargo-cache",
"cargo-criterion",
"cargo-update",
"flamegraph",
"topgrade",
"wthrr"
]
);
}
}