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> { fn get_missing_packages(&mut self) -> Result<ToDoPerBackend> {
let mut to_install = ToDoPerBackend::new(); let mut to_install = ToDoPerBackend::new();
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
for mut backend in Backends::iter() { for mut backend in Backends::iter() {
if self if self
.config .config
@@ -157,6 +161,10 @@ impl Pacdef {
} }
fn edit_groups(&self, groups: &[String]) -> Result<()> { 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) let group_files = get_group_file_paths_matching_args(groups, &self.groups)
.context("getting group files for args")?; .context("getting group files for args")?;
@@ -193,6 +201,10 @@ impl Pacdef {
/// ///
/// This function will propagate errors. /// This function will propagate errors.
fn get_unmanaged_packages(&mut self) -> Result<ToDoPerBackend> { fn get_unmanaged_packages(&mut self) -> Result<ToDoPerBackend> {
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
let mut result = ToDoPerBackend::new(); let mut result = ToDoPerBackend::new();
for mut backend in Backends::iter() { for mut backend in Backends::iter() {
@@ -220,6 +232,10 @@ impl Pacdef {
} }
fn show_groups(self) -> Result<()> { fn show_groups(self) -> Result<()> {
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
let mut vec: Vec<_> = self.groups.iter().collect(); let mut vec: Vec<_> = self.groups.iter().collect();
vec.sort_unstable(); vec.sort_unstable();
for g in vec { for g in vec {
@@ -250,6 +266,10 @@ impl Pacdef {
} }
fn show_group_content(&self, args: &[String]) -> Result<()> { 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 errors = vec![];
let mut groups = vec![]; let mut groups = vec![];
@@ -328,6 +348,10 @@ impl Pacdef {
} }
fn remove_groups(&self, groups: &[String]) -> Result<()> { 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)?; let paths = get_group_file_paths_matching_args(groups, &self.groups)?;
for file in paths { for file in paths {
+2 -7
View File
@@ -25,9 +25,8 @@ pub struct Group {
impl Group { impl Group {
/// Load all group files from the pacdef group dir by traversing through the group dir. /// Load all group files from the pacdef group dir by traversing through the group dir.
/// ///
/// This method will print a warning if /// This method will print a warning if `warn_not_symlinks` is true and a group
/// - there are no files under `group_dir`, or /// file is not a symlink.
/// - `warn_not_symlinks` is true and a group file is not a symlink.
/// ///
/// # Errors /// # Errors
/// ///
@@ -65,10 +64,6 @@ impl Group {
result.insert(group); result.insert(group);
} }
if result.is_empty() {
eprintln!("WARNING: no group files found");
}
Ok(result) Ok(result)
} }
} }
+5
View File
@@ -17,6 +17,11 @@ use crate::grouping::{Group, Package, Section};
/// - an invalid regex was provided, or /// - an invalid regex was provided, or
/// - no matching packages could be found. /// - no matching packages could be found.
pub fn search_packages(regex_str: &str, groups: &HashSet<Group>) -> Result<()> { 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 re = Regex::new(regex_str)?;
let mut vec = vec![]; let mut vec = vec![];