add a seperator character

this means you can print both the url and the name at the same time
with a seperator character for easy parsing.
This commit is contained in:
Fries 2023-07-01 20:34:28 -07:00
parent b58b0cd78f
commit 7f36560e25
3 changed files with 39 additions and 28 deletions

View File

@ -15,6 +15,14 @@ pub(crate) enum Commands {
Print {
#[command(flatten)]
group: PrintGroup,
#[arg(
long,
short,
help = "a seperator character to seperate the url from the name. defaults to ,",
requires = "url",
requires = "name"
)]
seperator: Option<char>,
},
#[command(about = "add a site to the webring")]
Add {
@ -48,8 +56,8 @@ pub(crate) enum Commands {
#[derive(Args, Debug)]
#[group(required = false)]
pub struct PrintGroup {
#[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "name", help = "print the url only")]
#[arg(long, short, action = clap::ArgAction::SetTrue, help = "print the url only")]
pub(crate) url: bool,
#[arg(long, short, action = clap::ArgAction::SetTrue, conflicts_with = "url", help = "print the name only")]
#[arg(long, short, action = clap::ArgAction::SetTrue, help = "print the name only")]
pub(crate) name: bool,
}

View File

@ -1,42 +1,43 @@
use std::path::Path;
use shared::{
errors::{Error, ErrorStatus},
names::Site,
};
use shared::names;
use shared::{errors::Error, names::Site};
use crate::arguments::PrintGroup;
fn read_names(path: &Path) -> Result<Vec<Site>, Error> {
match shared::names::read_names_file(path) {
Ok(names) => {
return shared::names::load_names(names);
}
Err(err) => Err(Error {
status: ErrorStatus::IOError,
data: err.data,
}),
}
}
pub(crate) fn print(path: &Path, group: &PrintGroup) -> Result<(), Error> {
let names = read_names(path)?;
pub(crate) fn print(path: &Path, group: &PrintGroup, seperator: &Option<char>) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let names = names::load_names(names_file)?;
Ok(for site in names {
if group.name {
log::info!("{}", site.name.unwrap_or_default());
if !group.url && !group.name {
log::info!("{:?}", site);
continue;
}
let mut string = String::new();
let delimiter = seperator.unwrap_or(',');
if group.url {
log::info!("{}", site.url);
continue;
string += &site.url;
}
log::info!("{:?}", site);
if group.name {
if !string.is_empty() {
string += &format!("{}{}", delimiter, site.name.unwrap_or("None".to_string()))
} else {
string += &site.name.unwrap_or("None".to_string());
}
}
log::info!("{}", string);
})
}
pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<(), Error> {
let mut names = read_names(path)?;
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
let site = Site {
url: url.to_string(),
name: name.to_owned(),
@ -50,7 +51,9 @@ pub(crate) fn add(path: &Path, url: &String, name: &Option<String>) -> Result<()
}
pub(crate) fn remove(path: &Path, url: &String) -> Result<(), Error> {
let mut names = read_names(path)?;
let names_file = names::read_names_file(path)?;
let mut names = names::load_names(names_file)?;
names.retain(|site| {
if &site.url == url {
log::info!("removing {:?} from names.json", site);

View File

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