From 654be105183665caef8bcf712fa2d36e5b4b8374 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 23 May 2023 17:54:08 +0200 Subject: [PATCH] feat: warn about missing group only when relevant So far we have warned the user when we first visited the group dir and there were no group files. This lead to spurious warnings, e.g. when the user wanted to import or create a group. We now defer the check to when we actually need some content in the groups, and the program should not perform the respective action without any groups present. This causes some code duplication, but enhances UE. Closes #31. --- crates/pacdef_core/src/core.rs | 24 ++++++++++++++++++++++++ crates/pacdef_core/src/grouping/group.rs | 9 ++------- crates/pacdef_core/src/search.rs | 5 +++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 157296e..f73aa7a 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -92,6 +92,10 @@ impl Pacdef { fn get_missing_packages(&mut self) -> Result { let mut to_install = ToDoPerBackend::new(); + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + for mut backend in Backends::iter() { if self .config @@ -157,6 +161,10 @@ impl Pacdef { } fn edit_groups(&self, groups: &[String]) -> Result<()> { + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + let group_files = get_group_file_paths_matching_args(groups, &self.groups) .context("getting group files for args")?; @@ -193,6 +201,10 @@ impl Pacdef { /// /// This function will propagate errors. fn get_unmanaged_packages(&mut self) -> Result { + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + let mut result = ToDoPerBackend::new(); for mut backend in Backends::iter() { @@ -220,6 +232,10 @@ impl Pacdef { } fn show_groups(self) -> Result<()> { + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + let mut vec: Vec<_> = self.groups.iter().collect(); vec.sort_unstable(); for g in vec { @@ -250,6 +266,10 @@ impl Pacdef { } fn show_group_content(&self, args: &[String]) -> Result<()> { + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + let mut errors = vec![]; let mut groups = vec![]; @@ -328,6 +348,10 @@ impl Pacdef { } fn remove_groups(&self, groups: &[String]) -> Result<()> { + if self.groups.is_empty() { + eprintln!("WARNING: no group files found"); + } + let paths = get_group_file_paths_matching_args(groups, &self.groups)?; for file in paths { diff --git a/crates/pacdef_core/src/grouping/group.rs b/crates/pacdef_core/src/grouping/group.rs index 5c39fcf..ea59891 100644 --- a/crates/pacdef_core/src/grouping/group.rs +++ b/crates/pacdef_core/src/grouping/group.rs @@ -25,9 +25,8 @@ pub struct Group { impl Group { /// Load all group files from the pacdef group dir by traversing through the group dir. /// - /// This method will print a warning if - /// - there are no files under `group_dir`, or - /// - `warn_not_symlinks` is true and a group file is not a symlink. + /// This method will print a warning if `warn_not_symlinks` is true and a group + /// file is not a symlink. /// /// # Errors /// @@ -65,10 +64,6 @@ impl Group { result.insert(group); } - if result.is_empty() { - eprintln!("WARNING: no group files found"); - } - Ok(result) } } diff --git a/crates/pacdef_core/src/search.rs b/crates/pacdef_core/src/search.rs index 7d3e160..02eabb8 100644 --- a/crates/pacdef_core/src/search.rs +++ b/crates/pacdef_core/src/search.rs @@ -17,6 +17,11 @@ use crate::grouping::{Group, Package, Section}; /// - an invalid regex was provided, or /// - no matching packages could be found. pub fn search_packages(regex_str: &str, groups: &HashSet) -> Result<()> { + if groups.is_empty() { + eprintln!("WARNING: no group files found"); + bail!(crate::errors::Error::NoPackagesFound); + } + let re = Regex::new(regex_str)?; let mut vec = vec![];