refact: use Result instead of ExitStatus

This commit is contained in:
steven-omaha
2024-04-09 19:28:57 +02:00
parent 025e63647e
commit 2b1e985f2f
10 changed files with 62 additions and 93 deletions
@@ -1,5 +1,5 @@
use std::collections::HashSet;
use std::process::{Command, ExitStatus};
use std::process::Command;
use alpm::Alpm;
use alpm::PackageReason::Explicit;
@@ -7,6 +7,7 @@ use anyhow::{Context, Result};
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::cmd::run_external_command;
use crate::{Group, Package};
#[derive(Debug, Clone)]
@@ -51,7 +52,7 @@ impl Backend for Arch {
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(&self.binary);
cmd.args(self.get_switches_install());
@@ -64,12 +65,11 @@ impl Backend for Arch {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
/// Remove the specified packages.
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(&self.binary);
cmd.args(self.get_switches_remove());
@@ -83,8 +83,7 @@ impl Backend for Arch {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
}
@@ -8,6 +8,7 @@ use rust_apt::new_cache;
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::backend::root::build_base_command_with_privileges;
use crate::cmd::run_external_command;
use crate::{Group, Package};
#[derive(Debug, Clone)]
@@ -51,18 +52,18 @@ impl Backend for Debian {
Ok(result)
}
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, packages: &[Package]) -> Result<()> {
let mut cmd = build_base_command_with_privileges("apt-mark");
cmd.arg("auto");
for p in packages {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = build_base_command_with_privileges(self.get_binary());
cmd.args(self.get_switches_install());
@@ -75,12 +76,11 @@ impl Backend for Debian {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
/// Remove the specified packages.
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = build_base_command_with_privileges(self.get_binary());
cmd.args(self.get_switches_remove());
@@ -92,8 +92,7 @@ impl Backend for Debian {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
}
@@ -1,11 +1,11 @@
use std::collections::HashSet;
use std::process::Command;
use std::process::ExitStatus;
use anyhow::{Context, Result};
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::{Group, Package};
#[derive(Debug, Clone)]
@@ -37,7 +37,7 @@ impl Backend for Flatpak {
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_install());
cmd.args(self.get_switches_runtime());
@@ -50,16 +50,15 @@ impl Backend for Flatpak {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
fn make_dependency(&self, _: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, _: &[Package]) -> Result<()> {
panic!("not supported by {}", BINARY)
}
/// Remove the specified packages.
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_remove());
cmd.args(self.get_switches_runtime());
@@ -72,18 +71,17 @@ impl Backend for Flatpak {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
/// Show information from package manager for package.
fn show_package_info(&self, package: &Package) -> Result<ExitStatus> {
fn show_package_info(&self, package: &Package) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_info());
cmd.args(self.get_switches_runtime());
cmd.arg(format!("{package}"));
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
}
@@ -1,6 +1,5 @@
use std::collections::HashSet;
use std::process::Command;
use std::process::ExitStatus;
use anyhow::Context;
use anyhow::Result;
@@ -53,7 +52,7 @@ impl Backend for Python {
self.extract_packages(output)
}
fn make_dependency(&self, _packages: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, _packages: &[Package]) -> Result<()> {
panic!("not supported by {}", BINARY)
}
}
@@ -2,7 +2,6 @@ use std::collections::HashSet;
use std::fs::read_to_string;
use std::io::ErrorKind::NotFound;
use std::path::PathBuf;
use std::process::ExitStatus;
use anyhow::{bail, Context, Result};
use serde_json::Value;
@@ -54,7 +53,7 @@ impl Backend for Rust {
.context("getting all installed packages")
}
fn make_dependency(&self, _: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, _: &[Package]) -> Result<()> {
panic!("not supported by {}", BINARY)
}
}
@@ -7,8 +7,7 @@ use crate::cmd::run_external_command;
use crate::{Group, Package};
use anyhow::{bail, Context, Result};
use std::collections::HashSet;
use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus};
use std::process::Command;
use self::helpers::{
group_components_by_toolchains, install_components, toolchain_of_component_was_already_removed,
@@ -60,11 +59,11 @@ impl Backend for Rustup {
.context("Getting all installed packages")
}
fn make_dependency(&self, _: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, _: &[Package]) -> Result<()> {
panic!("Not supported by {}", self.get_binary())
}
fn install_packages(&self, packages: &[Package], _: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], _: bool) -> Result<()> {
let packages = RustupPackage::from_pacdef_packages(packages)?;
let (toolchains, components) =
@@ -73,10 +72,10 @@ impl Backend for Rustup {
self.install_toolchains(toolchains)?;
self.install_components(components)?;
Ok(ExitStatus::from_raw(0))
Ok(())
}
fn remove_packages(&self, packages: &[Package], _: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], _: bool) -> Result<()> {
let rustup_packages = RustupPackage::from_pacdef_packages(packages)?;
let (toolchains, components) =
@@ -85,7 +84,8 @@ impl Backend for Rustup {
let removed_toolchains = self.remove_toolchains(toolchains)?;
self.remove_components(components, removed_toolchains)?;
Ok(ExitStatus::from_raw(0))
Ok(())
}
}
+12 -14
View File
@@ -1,12 +1,13 @@
use std::collections::HashSet;
use std::process::{Command, ExitStatus};
use std::process::Command;
use anyhow::{Context, Result};
use anyhow::Result;
use regex::Regex;
use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants;
use crate::backend::root::build_base_command_with_privileges;
use crate::cmd::run_external_command;
use crate::{Group, Package};
#[derive(Debug, Clone)]
@@ -73,7 +74,7 @@ impl Backend for Void {
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = build_base_command_with_privileges(INSTALL_BINARY);
cmd.args(self.get_switches_install());
@@ -85,11 +86,10 @@ impl Backend for Void {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = build_base_command_with_privileges(REMOVE_BINARY);
cmd.args(self.get_switches_remove());
@@ -101,11 +101,10 @@ impl Backend for Void {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, packages: &[Package]) -> Result<()> {
let mut cmd = build_base_command_with_privileges(PKGDB_BINARY);
cmd.args(self.get_switches_make_dependency());
@@ -113,17 +112,16 @@ impl Backend for Void {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
/// Show information from package manager for package.
fn show_package_info(&self, package: &Package) -> Result<ExitStatus> {
fn show_package_info(&self, package: &Package) -> Result<()> {
let mut cmd = Command::new(QUERY_BINARY);
cmd.args(self.get_switches_info());
cmd.arg(format!("{package}"));
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
}
+11 -28
View File
@@ -3,11 +3,12 @@ use std::cmp::{Eq, Ord};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::Hash;
use std::process::{Command, ExitStatus};
use std::process::Command;
use std::rc::Rc;
use anyhow::{Context, Result};
use crate::cmd::run_external_command;
use crate::{Group, Package};
pub(in crate::backend) type Switches = &'static [&'static str];
@@ -93,11 +94,7 @@ pub trait Backend: Debug {
///
/// This function will return an error if the package manager cannot be run or it
/// returns an error.
///
/// # Todo
///
/// The [`ExitStatus`] return type is not really necessary. [`anyhow::Result`] suffices.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_install());
@@ -109,8 +106,7 @@ pub trait Backend: Debug {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
/// Mark the packages as non-explicit / dependency using the underlying
@@ -119,11 +115,7 @@ pub trait Backend: Debug {
/// # Panics
///
/// This method shall panic when the backend does not support depedent packages.
///
/// # Todo
///
/// The [`ExitStatus`] return type is not really necessary. [`anyhow::Result`] suffices.
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
fn make_dependency(&self, packages: &[Package]) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_make_dependency());
@@ -131,16 +123,12 @@ pub trait Backend: Debug {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
/// Remove the specified packages.
///
/// # Todo
///
/// The [`ExitStatus`] return type is not really necessary. [`anyhow::Result`] suffices.
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_remove());
@@ -152,8 +140,7 @@ pub trait Backend: Debug {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
run_external_command(cmd)
}
/// Get missing packages, sorted alphabetically.
@@ -168,16 +155,12 @@ pub trait Backend: Debug {
}
/// Show information from package manager for package.
///
/// # Todo
///
/// The [`ExitStatus`] return type is not really necessary. [`anyhow::Result`] suffices.
fn show_package_info(&self, package: &Package) -> Result<ExitStatus> {
fn show_package_info(&self, package: &Package) -> Result<()> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_info());
cmd.arg(format!("{package}"));
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
run_external_command(cmd)
}
/// Get unmanaged packages, sorted alphabetically.
@@ -1,7 +1,6 @@
use std::fmt::Write;
use std::process::ExitStatus;
use anyhow::{bail, ensure, Context, Result};
use anyhow::{Context, Result};
use super::Backend;
use crate::Package;
@@ -58,21 +57,16 @@ impl ToDoPerBackend {
verb_continuous: &'_ str,
) -> Result<()>
where
F: Fn(&'a dyn Backend, &'a [Package], bool) -> Result<ExitStatus>,
F: Fn(&'a dyn Backend, &'a [Package], bool) -> Result<()>,
{
for (backend, packages) in &self.0 {
if packages.is_empty() {
continue;
}
let exit_status = func(&**backend, packages, noconfirm).with_context(|| {
func(&**backend, packages, noconfirm).with_context(|| {
format!("{verb_continuous} packages for {}", backend.get_section())
})?;
match exit_status.code() {
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
None => bail!("could not {verb} packages for {}", backend.get_section()),
}
}
Ok(())
}
+3 -3
View File
@@ -1,6 +1,6 @@
use std::rc::Rc;
use anyhow::{ensure, Result};
use anyhow::Result;
use crate::backend::Backend;
use crate::{Group, Package};
@@ -30,11 +30,11 @@ impl Strategy {
pub(super) fn execute(self) -> Result<()> {
if !self.delete.is_empty() {
ensure!(self.backend.remove_packages(&self.delete, false)?.success());
self.backend.remove_packages(&self.delete, false)?;
}
if !self.as_dependency.is_empty() {
ensure!(self.backend.make_dependency(&self.as_dependency)?.success());
self.backend.make_dependency(&self.as_dependency)?;
}
if !self.assign_group.is_empty() {