improve command running
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::Command;
|
||||
use std::process::{Command, ExitStatus};
|
||||
|
||||
use alpm::Alpm;
|
||||
use alpm::PackageReason::Explicit;
|
||||
@@ -38,27 +37,28 @@ impl Backend for Pacman {
|
||||
}
|
||||
|
||||
/// Install the specified packages.
|
||||
fn install_packages(&self, packages: &[Package]) {
|
||||
if packages.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
fn install_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
|
||||
let mut cmd = Command::new(&self.binary);
|
||||
|
||||
cmd.args(self.get_switches_install());
|
||||
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
cmd.exec();
|
||||
|
||||
cmd.status()
|
||||
.with_context(|| format!("running command {cmd:?}"))
|
||||
}
|
||||
|
||||
/// Remove the specified packages.
|
||||
fn remove_packages(&self, packages: Vec<Package>) {
|
||||
fn remove_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
|
||||
let mut cmd = Command::new(&self.binary);
|
||||
cmd.args(self.get_switches_remove());
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
cmd.exec();
|
||||
cmd.status()
|
||||
.with_context(|| format!("running command [{cmd:?}]"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::process::Command;
|
||||
use std::{collections::HashSet, process::ExitStatus};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
@@ -24,27 +23,25 @@ pub(crate) trait Backend {
|
||||
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>>;
|
||||
|
||||
/// Install the specified packages.
|
||||
fn install_packages(&self, packages: &[Package]) {
|
||||
if packages.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
fn install_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
|
||||
let mut cmd = Command::new(self.get_binary());
|
||||
cmd.args(self.get_switches_install());
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
cmd.exec();
|
||||
cmd.status()
|
||||
.with_context(|| format!("running command {cmd:?}"))
|
||||
}
|
||||
|
||||
/// Remove the specified packages.
|
||||
fn remove_packages(&self, packages: Vec<Package>) {
|
||||
fn remove_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
|
||||
let mut cmd = Command::new(self.get_binary());
|
||||
cmd.args(self.get_switches_remove());
|
||||
for p in packages {
|
||||
cmd.arg(format!("{p}"));
|
||||
}
|
||||
cmd.exec();
|
||||
cmd.status()
|
||||
.with_context(|| format!("running command [{cmd:?}]"))
|
||||
}
|
||||
|
||||
/// extract packages from its own section as read from group files
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use anyhow::{bail, ensure, Context, Result};
|
||||
|
||||
use super::Backend;
|
||||
use crate::Package;
|
||||
|
||||
@@ -12,10 +14,6 @@ impl ToDoPerBackend {
|
||||
self.0.push(item);
|
||||
}
|
||||
|
||||
pub(crate) fn into_iter(self) -> impl Iterator<Item = (Box<dyn Backend>, Vec<Package>)> {
|
||||
self.0.into_iter()
|
||||
}
|
||||
|
||||
pub(crate) fn iter(&self) -> impl Iterator<Item = &(Box<dyn Backend>, Vec<Package>)> {
|
||||
self.0.iter()
|
||||
}
|
||||
@@ -24,10 +22,34 @@ impl ToDoPerBackend {
|
||||
self.0.iter().all(|(_, diff)| diff.is_empty())
|
||||
}
|
||||
|
||||
pub(crate) fn install_missing_packages(&self) {
|
||||
self.0
|
||||
.iter()
|
||||
.for_each(|(backend, diff)| backend.install_packages(diff));
|
||||
// TODO try to combine these methods into one
|
||||
pub(crate) fn install_missing_packages(&self) -> Result<()> {
|
||||
for (backend, packages) in &self.0 {
|
||||
let exit_status = backend
|
||||
.install_packages(packages)
|
||||
.with_context(|| format!("installing packages for {}", backend.get_binary()))?;
|
||||
|
||||
match exit_status.code() {
|
||||
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
|
||||
None => bail!("could not install packages for {}", backend.get_binary()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO this one
|
||||
pub(crate) fn remove_unmanaged_packages(&self) -> Result<()> {
|
||||
for (backend, packages) in &self.0 {
|
||||
let exit_status = backend
|
||||
.remove_packages(packages)
|
||||
.with_context(|| format!("removing packages for {}", backend.get_binary()))?;
|
||||
|
||||
match exit_status.code() {
|
||||
Some(val) => ensure!(val == 0, "command returned with exit code {val}"),
|
||||
None => bail!("could not remove packages for {}", backend.get_binary()),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn is_empty(&self) -> bool {
|
||||
|
||||
Reference in New Issue
Block a user