add json string support to the cli's print command

this let's you print a json string.
This commit is contained in:
Fries 2023-07-01 20:49:59 -07:00
parent e93b201d77
commit f37034b8d8
3 changed files with 27 additions and 1 deletions

View file

@ -23,6 +23,15 @@ pub(crate) enum Commands {
requires = "name"
)]
seperator: Option<char>,
#[arg(
long,
short,
conflicts_with = "url",
conflicts_with = "name",
conflicts_with = "seperator",
help = "print the data out as a json string"
)]
json: bool,
},
#[command(about = "add a site to the webring")]
Add {

View file

@ -1,5 +1,6 @@
use std::path::Path;
use shared::errors::ErrorStatus;
use shared::names;
use shared::{errors::Error, names::Site};
@ -24,15 +25,31 @@ fn group_printing(seperator: &Option<char>, site: Site, group: &PrintGroup) {
log::info!("{}", string);
}
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() }),
}
}
pub(crate) fn print(
path: &Path,
group: &PrintGroup,
seperator: &Option<char>,
json: bool,
) -> Result<(), Error> {
let names_file = names::read_names_file(path)?;
let names = names::load_names(names_file)?;
for site in names {
if json {
json_printing(site)?;
continue;
}
if !group.url && !group.name {
log::info!("{:?}", site);
continue;

View file

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