add pacdef clean

This commit is contained in:
steven-omaha
2023-01-05 15:53:50 +01:00
parent 7ebedc8faf
commit 6956164bed
6 changed files with 34 additions and 3 deletions
+1
View File
@@ -1,3 +1,4 @@
pub(crate) const CLEAN: &str = "clean";
pub(crate) const EDIT: &str = "edit";
pub(crate) const GROUPS: &str = "groups";
pub(crate) const SYNC: &str = "sync";
+1
View File
@@ -8,6 +8,7 @@ fn get_arg_parser() -> Command<'static> {
.version("1.0.0-alpha")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(Command::new(CLEAN).about("remove unmanaged packages"))
.subcommand(
Command::new(EDIT)
.about("edit one or more existing group files")
+9
View File
@@ -23,3 +23,12 @@ pub fn run_install_command(diff: Vec<Package>) {
}
cmd.exec();
}
pub fn run_remove_command(unmanaged: Vec<Package>) {
let mut cmd = Command::new("paru");
cmd.arg("-Rsn");
for p in unmanaged {
cmd.arg(format!("{p}"));
}
cmd.exec();
}
+19 -1
View File
@@ -5,8 +5,9 @@ use anyhow::{bail, Context, Result};
use clap::ArgMatches;
use crate::action;
use crate::cmd::{run_edit_command, run_install_command};
use crate::cmd::{run_edit_command, run_install_command, run_remove_command};
use crate::db::{get_all_installed_packages, get_explicitly_installed_packages};
use crate::ui::get_user_confirmation;
use crate::Group;
use crate::Package;
@@ -61,8 +62,10 @@ impl Pacdef {
run_install_command(diff);
}
#[allow(clippy::unit_arg)]
pub fn run_action_from_arg(self) -> Result<()> {
match self.args.subcommand() {
Some((action::CLEAN, _)) => Ok(self.clean_packages()),
Some((action::EDIT, groups)) => self.edit_group_files(groups).context("editing"),
Some((action::GROUPS, _)) => Ok(self.show_groups()),
Some((action::SYNC, _)) => Ok(self.install_packages()),
@@ -122,4 +125,19 @@ impl Pacdef {
println!("{}", g.name);
}
}
fn clean_packages(mut self) {
let unmanaged = self.get_unmanaged_packages();
if unmanaged.is_empty() {
println!("nothing to do");
return;
}
println!("Would remove the following packages and their dependencies:");
for p in &unmanaged {
println!(" {p}");
}
get_user_confirmation();
run_remove_command(unmanaged);
}
}
+3 -1
View File
@@ -25,7 +25,9 @@ impl Group {
let packages = Package::from_lines(reader.lines());
result.insert(Group {
name: name.into_string().map_err(|e| anyhow!(e))?,
name: name
.into_string()
.map_err(|e| anyhow!("could not get group name, {e:?}"))?,
packages,
});
}
+1 -1
View File
@@ -4,7 +4,7 @@ use pacdef::{args, Group, Pacdef};
fn main() -> Result<()> {
let args = args::get_args();
let groups = Group::load_from_dir();
let groups = Group::load_from_dir()?;
let pacdef = Pacdef::new(args, groups);
pacdef.run_action_from_arg().context("running action")
}