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.
This commit is contained in:
steven-omaha
2023-05-23 17:54:08 +02:00
parent 008892b7ce
commit 654be10518
3 changed files with 31 additions and 7 deletions
+24
View File
@@ -92,6 +92,10 @@ impl Pacdef {
fn get_missing_packages(&mut self) -> Result<ToDoPerBackend> {
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<ToDoPerBackend> {
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 {
+2 -7
View File
@@ -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)
}
}
+5
View File
@@ -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<Group>) -> 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![];