diff --git a/cli/src/arguments.rs b/cli/src/arguments.rs index 9dd4064..48eb76b 100644 --- a/cli/src/arguments.rs +++ b/cli/src/arguments.rs @@ -23,6 +23,15 @@ pub(crate) enum Commands { requires = "name" )] seperator: Option, + #[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 { diff --git a/cli/src/commands.rs b/cli/src/commands.rs index 0e9ba32..d2f7177 100644 --- a/cli/src/commands.rs +++ b/cli/src/commands.rs @@ -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, 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, + 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; diff --git a/cli/src/main.rs b/cli/src/main.rs index 454ed42..3cdc11f 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -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)?, };