show error if any arg of 'group show' does not exist
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
# To Do
|
# To Do
|
||||||
|
|
||||||
- tutorial
|
- tutorial
|
||||||
* group file not found (e.g. group edit)
|
|
||||||
* invalid group name (e.g. '.')
|
* invalid group name (e.g. '.')
|
||||||
* building on non-Arch
|
* building on non-Arch
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::fs::{remove_file, File};
|
|||||||
use std::os::unix::fs::symlink;
|
use std::os::unix::fs::symlink;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, ensure, Context, Result};
|
use anyhow::{bail, ensure, Context, Result};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use const_format::formatcp;
|
use const_format::formatcp;
|
||||||
|
|
||||||
@@ -206,20 +206,40 @@ impl Pacdef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn show_group_content(&self, groups: &ArgMatches) -> Result<()> {
|
fn show_group_content(&self, groups: &ArgMatches) -> Result<()> {
|
||||||
let mut iter = groups
|
let args: Vec<_> = groups
|
||||||
.get_many::<String>("groups")
|
.get_many::<String>("groups")
|
||||||
.context("getting groups from args")?
|
.context("getting groups from args")?
|
||||||
.peekable();
|
.collect();
|
||||||
|
|
||||||
let show_more_than_one_group = iter.size_hint().0 > 1;
|
let mut errors = vec![];
|
||||||
|
let mut groups = vec![];
|
||||||
|
|
||||||
while let Some(arg_group) = iter.next() {
|
// make sure all args exist before doing anything
|
||||||
let group = self
|
for arg_group in &args {
|
||||||
.groups
|
let group = self.groups.iter().find(|g| g.name == **arg_group);
|
||||||
.iter()
|
|
||||||
.find(|g| g.name == *arg_group)
|
|
||||||
.ok_or_else(|| anyhow!(crate::Error::GroupFileNotFound(g.name)))?;
|
|
||||||
|
|
||||||
|
let group = match group {
|
||||||
|
Some(g) => g,
|
||||||
|
None => {
|
||||||
|
errors.push((*arg_group).clone());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
groups.push(group);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return an error if any arg was not found
|
||||||
|
ensure!(
|
||||||
|
errors.is_empty(),
|
||||||
|
crate::Error::MultipleGroupsNotFound(errors)
|
||||||
|
);
|
||||||
|
|
||||||
|
let show_more_than_one_group = args.len() > 1;
|
||||||
|
|
||||||
|
let mut iter = groups.into_iter().peekable();
|
||||||
|
|
||||||
|
while let Some(group) = iter.next() {
|
||||||
if show_more_than_one_group {
|
if show_more_than_one_group {
|
||||||
let name = &group.name;
|
let name = &group.name;
|
||||||
println!("{name}");
|
println!("{name}");
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::error::Error as ErrorTrait;
|
use std::error::Error as ErrorTrait;
|
||||||
use std::fmt::Display;
|
use std::fmt::{Display, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// Error types for pacdef.
|
/// Error types for pacdef.
|
||||||
@@ -16,6 +16,8 @@ pub enum Error {
|
|||||||
GroupAlreadyExists(PathBuf),
|
GroupAlreadyExists(PathBuf),
|
||||||
/// Invalid group name ('.' or '..')
|
/// Invalid group name ('.' or '..')
|
||||||
InvalidGroupName(String),
|
InvalidGroupName(String),
|
||||||
|
/// Multiple groups not found.
|
||||||
|
MultipleGroupsNotFound(Vec<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
@@ -31,6 +33,17 @@ impl Display for Error {
|
|||||||
Self::InvalidGroupName(name) => {
|
Self::InvalidGroupName(name) => {
|
||||||
f.write_str(&format!("group name '{name}' is not valid"))
|
f.write_str(&format!("group name '{name}' is not valid"))
|
||||||
}
|
}
|
||||||
|
Self::MultipleGroupsNotFound(vec) => {
|
||||||
|
f.write_str("could not find the following groups:\n")?;
|
||||||
|
let mut iter = vec.iter().peekable();
|
||||||
|
while let Some(group) = iter.next() {
|
||||||
|
f.write_str(&format!(" {group}"))?;
|
||||||
|
if iter.peek().is_some() {
|
||||||
|
f.write_char('\n')?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user