add a filter argument to the print command

this lets you put a url in and the print command will filter it to that.
This commit is contained in:
Fries 2023-07-01 22:10:58 -07:00
parent f37034b8d8
commit a08502cc73
4 changed files with 43 additions and 10 deletions

View File

@ -13,6 +13,8 @@ pub(crate) struct Arguments {
pub(crate) enum Commands { pub(crate) enum Commands {
#[command(about = "print the current webring sites and their names")] #[command(about = "print the current webring sites and their names")]
Print { Print {
#[arg(help = "url you want to filter to")]
filter: Option<String>,
#[command(flatten)] #[command(flatten)]
group: PrintGroup, group: PrintGroup,
#[arg( #[arg(

View File

@ -6,7 +6,7 @@ use shared::{errors::Error, names::Site};
use crate::arguments::PrintGroup; use crate::arguments::PrintGroup;
fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) { fn group_printing(seperator: &Option<char>, site: &Site, group: &PrintGroup) {
let mut string = String::new(); let mut string = String::new();
let delimiter = seperator.unwrap_or(','); let delimiter = seperator.unwrap_or(',');
@ -16,37 +16,67 @@ fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) {
if group.name { if group.name {
if !string.is_empty() { if !string.is_empty() {
string += &format!("{}{}", delimiter, site.name.unwrap_or("None".to_string())) string += &format!(
"{}{}",
delimiter,
site.name.as_ref().unwrap_or(&"None".into())
)
} else { } else {
string += &site.name.unwrap_or("None".to_string()); string += &site.name.as_ref().unwrap_or(&"None".into());
} }
} }
log::info!("{}", string); log::info!("{}", string);
} }
fn json_printing(site: Site) -> Result<(), Error> { fn json_printing(site: &Site) -> Result<(), Error> {
match serde_json::to_string(&site) { match serde_json::to_string(&site) {
Ok(json) => { Ok(json) => {
log::info!("{}", json); log::info!("{}", json);
Ok(()) Ok(())
} }
Err(err) => Err(Error { status: ErrorStatus::ParsingError, data: err.to_string() }), Err(err) => Err(Error {
status: ErrorStatus::ParsingError,
data: err.to_string(),
}),
} }
} }
fn filter_site(site: &Site, json: bool, seperator: &Option<char>, group: &PrintGroup) -> Result<(), Error> {
if json {
json_printing(site)?;
return Ok(());
}
if !group.url && !group.name {
log::info!("{:?}", site);
return Ok(());
}
return Ok(group_printing(seperator, site, group));
}
pub(crate) fn print( pub(crate) fn print(
path: &Path, path: &Path,
filter: &Option<String>,
group: &PrintGroup, group: &PrintGroup,
seperator: &Option<char>, seperator: &Option<char>,
json: bool, json: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let names_file = names::read_names_file(path)?; let names_file = names::read_names_file(path)?;
let names = names::load_names(names_file)?; let mut names = names::load_names(names_file)?;
if let Some(filter) = filter {
names.retain(|f| &f.url == filter);
if names.len() == 0 {
return Err(Error { status: ErrorStatus::NotFoundError, data: "this url was not found in names.json".into() })
}
return filter_site(&names[0], json, seperator, group);
}
for site in names { for site in names {
if json { if json {
json_printing(site)?; json_printing(&site)?;
continue; continue;
} }
@ -55,7 +85,7 @@ pub(crate) fn print(
continue; continue;
} }
group_printing(seperator, site, group); group_printing(seperator, &site, group);
} }
Ok(()) Ok(())

View File

@ -20,7 +20,7 @@ fn main() -> Result<(), Error> {
}; };
match &args.command { match &args.command {
Commands::Print { group, seperator, json } => print(path, group, seperator, *json)?, Commands::Print { filter, group, seperator, json, } => print(path, filter, group, seperator, *json)?,
Commands::Add { url, name } => add(path, url, name)?, Commands::Add { url, name } => add(path, url, name)?,
Commands::Remove { url } => remove(path, url)?, Commands::Remove { url } => remove(path, url)?,
}; };

View File

@ -3,7 +3,8 @@ pub enum ErrorStatus {
IOError, IOError,
ParsingError, ParsingError,
DirectoriesError, DirectoriesError,
LoggerInitializationError LoggerInitializationError,
NotFoundError
} }
pub struct Error { pub struct Error {