feat: implement --noconfirm

This commit is contained in:
steven-omaha
2023-05-12 16:05:49 +02:00
parent f03805446e
commit 7d9f444ea6
10 changed files with 94 additions and 29 deletions
+11 -2
View File
@@ -21,6 +21,7 @@ const SECTION: Text = "arch";
const SWITCHES_INFO: Switches = &["--query", "--info"];
const SWITCHES_INSTALL: Switches = &["--sync"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &["--database", "--asdeps"];
const SWITCHES_NOCONFIRM: Switches = &["--noconfirm"];
const SWITCHES_REMOVE: Switches = &["--remove", "--recursive"];
const SUPPORTS_AS_DEPENDENCY: bool = true;
@@ -49,11 +50,15 @@ impl Backend for Arch {
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new(&self.binary);
cmd.args(self.get_switches_install());
if noconfirm {
cmd.args(self.get_switches_noconfirm());
}
for p in packages {
cmd.arg(format!("{p}"));
}
@@ -63,7 +68,7 @@ impl Backend for Arch {
}
/// Remove the specified packages.
fn remove_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new(&self.binary);
cmd.args(self.get_switches_remove());
@@ -71,6 +76,10 @@ impl Backend for Arch {
cmd.args(rm_args);
}
if noconfirm {
cmd.args(self.get_switches_noconfirm());
}
for p in packages {
cmd.arg(format!("{p}"));
}
@@ -20,6 +20,7 @@ const SECTION: Text = "debian";
const SWITCHES_INFO: Switches = &["show"];
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[]; // not needed
const SWITCHES_NOCONFIRM: Switches = &["--yes"];
const SWITCHES_REMOVE: Switches = &["remove"];
const SUPPORTS_AS_DEPENDENCY: bool = true;
@@ -20,6 +20,7 @@ const SECTION: Text = "python";
const SWITCHES_INFO: Switches = &["show"];
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[]; // not needed
const SWITCHES_NOCONFIRM: Switches = &[]; // not needed
const SWITCHES_REMOVE: Switches = &["uninstall"];
const SUPPORTS_AS_DEPENDENCY: bool = false;
@@ -20,6 +20,7 @@ const SECTION: Text = "rust";
const SWITCHES_INSTALL: Switches = &["install"];
const SWITCHES_INFO: Switches = &["search", "--limit", "1"];
const SWITCHES_MAKE_DEPENDENCY: Switches = &[];
const SWITCHES_NOCONFIRM: Switches = &[]; // not needed
const SWITCHES_REMOVE: Switches = &["uninstall"];
const SUPPORTS_AS_DEPENDENCY: bool = false;
@@ -37,6 +37,10 @@ pub trait Backend: Debug {
/// Get CLI switches for the package manager to install packages.
fn get_switches_install(&self) -> Switches;
/// Get CLI switches for the package manager to perform `sync` and `clean` without
/// confirmation.
fn get_switches_noconfirm(&self) -> Switches;
/// Get CLI switches for the package manager to remove packages.
fn get_switches_remove(&self) -> Switches;
@@ -73,12 +77,18 @@ pub trait Backend: Debug {
}
/// Install the specified packages.
fn install_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_install());
if noconfirm {
cmd.args(self.get_switches_noconfirm());
}
for p in packages {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command {cmd:?}"))
}
@@ -92,20 +102,28 @@ pub trait Backend: Debug {
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_make_dependency());
for p in packages {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
}
/// Remove the specified packages.
fn remove_packages(&self, packages: &[Package]) -> Result<ExitStatus> {
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new(self.get_binary());
cmd.args(self.get_switches_remove());
if noconfirm {
cmd.args(self.get_switches_noconfirm());
}
for p in packages {
cmd.arg(format!("{p}"));
}
cmd.status()
.with_context(|| format!("running command [{cmd:?}]"))
}
+4
View File
@@ -19,6 +19,10 @@ macro_rules! impl_backend_constants {
SWITCHES_INSTALL
}
fn get_switches_noconfirm(&self) -> Switches {
SWITCHES_NOCONFIRM
}
fn get_switches_remove(&self) -> Switches {
SWITCHES_REMOVE
}
@@ -35,31 +35,37 @@ impl ToDoPerBackend {
self.0.iter().all(|(_, diff)| diff.is_empty())
}
pub(crate) fn install_missing_packages(&self) -> Result<()> {
self.handle_backend_command(Backend::install_packages, "install", "installing")
.context("installing packages")
pub(crate) fn install_missing_packages(&self, noconfirm: bool) -> Result<()> {
self.handle_backend_command(
Backend::install_packages,
noconfirm,
"install",
"installing",
)
.context("installing packages")
}
pub(crate) fn remove_unmanaged_packages(&self) -> Result<()> {
self.handle_backend_command(Backend::remove_packages, "remove", "removing")
pub(crate) fn remove_unmanaged_packages(&self, noconfirm: bool) -> Result<()> {
self.handle_backend_command(Backend::remove_packages, noconfirm, "remove", "removing")
.context("removing packages")
}
fn handle_backend_command<'a, F>(
&'a self,
func: F,
noconfirm: bool,
verb: &'_ str,
verb_continuous: &'_ str,
) -> Result<()>
where
F: Fn(&'a dyn Backend, &'a [Package]) -> Result<ExitStatus>,
F: Fn(&'a dyn Backend, &'a [Package], bool) -> Result<ExitStatus>,
{
for (backend, packages) in &self.0 {
if packages.is_empty() {
continue;
}
let exit_status = func(&**backend, packages).with_context(|| {
let exit_status = func(&**backend, packages, noconfirm).with_context(|| {
format!("{verb_continuous} packages for {}", backend.get_section())
})?;