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 {
#[command(about = "print the current webring sites and their names")]
Print {
#[arg(help = "url you want to filter to")]
filter: Option<String>,
#[command(flatten)]
group: PrintGroup,
#[arg(

View File

@ -6,7 +6,7 @@ use shared::{errors::Error, names::Site};
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 delimiter = seperator.unwrap_or(',');
@ -16,37 +16,67 @@ fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) {
if group.name {
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 {
string += &site.name.unwrap_or("None".to_string());
string += &site.name.as_ref().unwrap_or(&"None".into());
}
}
log::info!("{}", string);
}
fn json_printing(site: Site) -> Result<(), Error> {
fn json_printing(site: &Site) -> Result<(), Error> {
match serde_json::to_string(&site) {
Ok(json) => {
log::info!("{}", json);
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(
path: &Path,
filter: &Option<String>,
group: &PrintGroup,
seperator: &Option<char>,
json: bool,
) -> Result<(), Error> {
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 {
if json {
json_printing(site)?;
json_printing(&site)?;
continue;
}
@ -55,7 +85,7 @@ pub(crate) fn print(
continue;
}
group_printing(seperator, site, group);
group_printing(seperator, &site, group);
}
Ok(())

View File

@ -20,7 +20,7 @@ fn main() -> Result<(), Error> {
};
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::Remove { url } => remove(path, url)?,
};

View File

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