Backend trait refactors

- Refactor the confusing/bodgy consts backend macro to use a single
  `BackendInfo` struct
- Combine `supports_as_dependency()` and the `switches_dependency` into a single `Option` type as that is a better representation as it is an optional backend feature.
- Add more types like `Packages`, `Sections` for uniformity.
- Added a crate-private prelude and switches all imports to use a
  prelude glob import (excluding functions because it's nice to only be
  importing types)
- Refactored Backend section name in logging by implementing Display for
  `AnyBackend` that just prints the `section`
This commit is contained in:
ripytide
2024-04-24 17:03:42 +01:00
parent 15b50d8cb0
commit 17e42ca435
26 changed files with 524 additions and 560 deletions
+36 -38
View File
@@ -1,22 +1,15 @@
use std::collections::HashSet;
use std::process::Command;
use anyhow::Result;
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::cmd::run_external_command;
use crate::Package;
use crate::prelude::*;
#[derive(Debug, Clone)]
pub struct Fedora {
pub packages: HashSet<Package>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Fedora {}
impl Fedora {
pub fn new() -> Self {
Self {
packages: HashSet::new(),
}
Self {}
}
}
impl Default for Fedora {
@@ -25,16 +18,9 @@ impl Default for Fedora {
}
}
const BINARY: Text = "dnf";
const SECTION: Text = "fedora";
const SWITCHES_INFO: Switches = &["info"];
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[];
const SWITCHES_NOCONFIRM: Switches = &["--assumeyes"];
const SWITCHES_REMOVE: Switches = &["remove"];
const SUPPORTS_AS_DEPENDENCY: bool = false;
/// These repositories are ignored when storing the packages
/// as these are present by default on any sane fedora system
const DEFAULT_REPOS: [&str; 5] = ["koji", "fedora", "updates", "anaconda", "@"];
/// These switches are responsible for
/// getting the packages explicitly installed by the user
@@ -54,15 +40,21 @@ const SWITCHES_FETCH_GLOBAL: Switches = &[
"%{from_repo}/%{name}",
];
/// These repositories are ignored when storing the packages
/// as these are present by default on any sane fedora system
const DEFAULT_REPOS: [&str; 5] = ["koji", "fedora", "updates", "anaconda", "@"];
impl Backend for Fedora {
impl_backend_constants!();
fn backend_info(&self) -> BackendInfo {
BackendInfo {
binary: "dnf".to_string(),
section: "fedora",
switches_info: &["info"],
switches_install: &["install"],
switches_noconfirm: &["--assumeyes"],
switches_remove: &["remove"],
switches_make_dependency: None,
}
}
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
let mut cmd = Command::new(self.get_binary());
fn get_all_installed_packages(&self) -> Result<Packages> {
let mut cmd = Command::new(self.backend_info().binary);
cmd.args(SWITCHES_FETCH_GLOBAL);
let output = String::from_utf8(cmd.output()?.stdout)?;
@@ -71,8 +63,8 @@ impl Backend for Fedora {
Ok(packages)
}
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
let mut cmd = Command::new(self.get_binary());
fn get_explicitly_installed_packages(&self) -> Result<Packages> {
let mut cmd = Command::new(self.backend_info().binary);
cmd.args(SWITCHES_FETCH_USER);
let output = String::from_utf8(cmd.output()?.stdout)?;
@@ -83,12 +75,14 @@ impl Backend for Fedora {
/// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let backend_info = self.backend_info();
let mut cmd = Command::new("sudo");
cmd.arg(self.get_binary());
cmd.args(self.get_switches_install());
cmd.arg(backend_info.binary);
cmd.args(backend_info.switches_install);
if noconfirm {
cmd.args(self.get_switches_noconfirm());
cmd.args(backend_info.switches_noconfirm);
}
for p in packages {
@@ -107,12 +101,14 @@ impl Backend for Fedora {
/// Show information from package manager for package.
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let backend_info = self.backend_info();
let mut cmd = Command::new("sudo");
cmd.arg(self.get_binary());
cmd.args(self.get_switches_remove());
cmd.arg(backend_info.binary);
cmd.args(backend_info.switches_remove);
if noconfirm {
cmd.args(self.get_switches_noconfirm());
cmd.args(backend_info.switches_noconfirm);
}
for p in packages {
@@ -123,8 +119,10 @@ impl Backend for Fedora {
}
fn show_package_info(&self, package: &Package) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_info());
let backend_info = self.backend_info();
let mut cmd = Command::new(backend_info.binary);
cmd.args(backend_info.switches_info);
cmd.arg(&package.name);
run_external_command(cmd)