fix(group): symlink warning for package operation

Until now, when symlink warnings were enabled, these were printed
independent from the operation that was to be executed. This was
annoying especially when 'pacdef group export' was used to fix exactly
that.

Now this warning is only printed when subcommands from 'pacdef package'
are used.
This commit is contained in:
steven-omaha
2024-04-09 14:50:18 +02:00
parent 9758257957
commit 025e63647e
2 changed files with 33 additions and 10 deletions
+25 -1
View File
@@ -87,7 +87,10 @@ impl Pacdef {
match args {
Clean(args::Noconfirm(noconfirm)) => self.clean_packages(*noconfirm),
Review => review::review(self.get_unmanaged_packages()?, self.groups),
Search(args::Regex(regex)) => search::search_packages(regex, &self.groups),
Search(args::Regex(regex)) => {
self.warn_about_groups_that_arent_symlinks();
search::search_packages(regex, &self.groups)
}
Sync(args::Noconfirm(noconfirm)) => self.install_packages(*noconfirm),
Unmanaged => self.show_unmanaged_packages(),
}
@@ -150,6 +153,8 @@ impl Pacdef {
}
fn install_packages(&mut self, noconfirm: bool) -> Result<()> {
self.warn_about_groups_that_arent_symlinks();
let to_install = self.get_missing_packages()?;
if to_install.nothing_to_do_for_all_backends() {
@@ -170,7 +175,20 @@ impl Pacdef {
to_install.install_missing_packages(noconfirm)
}
fn warn_about_groups_that_arent_symlinks(&self) {
for group in &self.groups {
if group.warn_symlink {
eprintln!(
"WARNING: group file {} is not a symlink",
group.path.to_string_lossy()
);
}
}
}
fn edit_groups(&self, groups: &[String]) -> Result<()> {
self.warn_about_groups_that_arent_symlinks();
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
@@ -211,6 +229,8 @@ impl Pacdef {
///
/// This function will propagate errors from the individual backends.
fn get_unmanaged_packages(&mut self) -> Result<ToDoPerBackend> {
self.warn_about_groups_that_arent_symlinks();
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
@@ -247,6 +267,8 @@ impl Pacdef {
/// with other methods.
#[allow(clippy::unnecessary_wraps)]
fn show_groups(self) -> Result<()> {
self.warn_about_groups_that_arent_symlinks();
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
@@ -282,6 +304,8 @@ impl Pacdef {
}
fn show_group_content(&self, args: &[String]) -> Result<()> {
self.warn_about_groups_that_arent_symlinks();
if self.groups.is_empty() {
eprintln!("WARNING: no group files found");
}
+8 -9
View File
@@ -23,6 +23,8 @@ pub struct Group {
pub(crate) sections: HashSet<Section>,
/// The absolute path of the original file.
pub(crate) path: PathBuf,
/// Whether the main program should warn this group being loaded from a symlink.
pub(crate) warn_symlink: bool,
}
impl Group {
@@ -57,15 +59,11 @@ impl Group {
continue;
}
if warn_not_symlinks && !path.is_symlink() && !is_child_of_any_dir(&path, &symlink_dirs)
{
eprintln!(
"WARNING: group file {} is not a symlink",
path.to_string_lossy()
);
}
let should_warn_about_symlinks = warn_not_symlinks
&& !path.is_symlink()
&& !is_child_of_any_dir(&path, &symlink_dirs);
let group = Self::try_from(path.as_path(), group_dir)
let group = Self::try_from(path.as_path(), group_dir, should_warn_about_symlinks)
.with_context(|| format!("reading group file {path:?}"))?;
result.insert(group);
@@ -132,7 +130,7 @@ impl Group {
/// # Errors
///
/// This function will return an error if the group file cannot be read.
fn try_from<P>(path: P, group_dir: P) -> Result<Self>
fn try_from<P>(path: P, group_dir: P, warn_symlink: bool) -> Result<Self>
where
P: AsRef<Path>,
{
@@ -167,6 +165,7 @@ impl Group {
name,
sections,
path,
warn_symlink,
})
}