Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: JSON output #15

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tmf-cli"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -9,6 +9,8 @@ edition = "2021"
clap = { version = "4.5.27", features = ["derive"]}
env_logger = "0.11.6"
log = "0.4.25"
serde = "1.0.217"
serde_json = "1.0.137"
tmf-client = "0.1.5"
# tmf-client = { git = "https://github.com/rruckley/tmf-client.git" }
tmflib = "0.1.25"
31 changes: 25 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ use tmf::tmf674::{
handle_tmf674,
};

pub enum Output {
Text,
Json,
}

#[derive(Parser,Debug)]
#[command(version, about = "CLI tool for interacting with TMF APIs", author = "Ryan Ruckley")]
struct Args {
Expand All @@ -54,6 +59,10 @@ struct Args {
#[clap(global = true)]
#[arg(short = 'n', long)]
name: Option<String>,

#[clap(global = true)]
#[arg(short = 'j', long)]
json: Option<bool>,
}

#[derive(Subcommand,Debug)]
Expand Down Expand Up @@ -108,6 +117,16 @@ fn main() -> Result<(),TMFError> {
opts = opts.name(n);
}

let output = match args.json {
Some(j) => {
match j {
true => Output::Json,
false => Output::Text,
}
},
None => Output::Text,
};

// Find a host
let host = match args.hostname {
Some(h) => h,
Expand All @@ -120,25 +139,25 @@ fn main() -> Result<(),TMFError> {

match args.tmf {
TMFModules::TMF620 { module } => {
handle_tmf620(&mut client, module, Some(opts))
handle_tmf620(&mut client, module, Some(opts),output)
},
TMFModules::TMF622 { module } => {
handle_tmf622(&mut client, module, Some(opts))
},
TMFModules::TMF629 { module } => {
handle_tmf629(&mut client, module, Some(opts))
handle_tmf629(&mut client, module, Some(opts),output)
},
TMFModules::TMF632 { module } => {
handle_tmf632(&mut client, module, Some(opts))
handle_tmf632(&mut client, module, Some(opts),output)
},
TMFModules::TMF633 { module } => {
handle_tmf633(&mut client, module, Some(opts))
handle_tmf633(&mut client, module, Some(opts),output)
},
TMFModules::TMF648 { module } => {
handle_tmf648(&mut client, module, Some(opts))
handle_tmf648(&mut client, module, Some(opts),output)
},
TMFModules::TMF674 { module } => {
handle_tmf674(&mut client, module, Some(opts))
handle_tmf674(&mut client, module, Some(opts),output)
}
}
}
27 changes: 21 additions & 6 deletions src/tmf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use tmflib::{
HasName,
HasDescription,
};
use serde::Serialize;

use crate::Output;

pub mod tmf620;
pub mod tmf622;
Expand All @@ -25,10 +28,18 @@ pub enum TMFOperation {
Delete
}

pub fn iterate_name<T : HasId + HasName>(items : &Vec<T>) {
items.iter().for_each(|i| {
println!("Item: [{}] {} [{}]",T::get_class(),i.get_name(),i.get_id());
});
pub fn iterate_name<T : HasId + HasName + Serialize>(items : &Vec<T>,output : Output) {
match output {
Output::Text => {
items.iter().for_each(|i| {
println!("Item: [{}] {} [{}]",T::get_class(),i.get_name(),i.get_id());
});
},
Output::Json => {
display_json(items);
}
}

}

pub fn iterate_desc<T : HasId + HasDescription>(items : &Vec<T>) {
Expand All @@ -42,10 +53,9 @@ pub fn display_id<T: HasId>(item : &T) {
println!("Href:\t{}",item.get_href());
}

pub fn display_name<T: HasId + HasName>(item : &T) {
pub fn display_name<T: HasId + HasName + Serialize>(item : &T) {
display_id(item);
println!("Name:\t{}",item.get_name());

}

pub fn display_desc<T : HasId + HasDescription>(item : &T) {
Expand All @@ -58,4 +68,9 @@ pub fn display_opt(label : &str, field : &Option<String>) {
Some(v) => println!("{}:\t{}",label,v),
None => println!("{}:\tNot Set",label),
}
}

pub fn display_json<T : Serialize>(item : T) {
let json = serde_json::to_string(&item).expect("Could not create JSON");
println!("{}",json);
}
25 changes: 17 additions & 8 deletions src/tmf/tmf620.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_desc, display_name, iterate_name, TMFOperation
display_desc,
display_name,
display_json,
iterate_name,
TMFOperation
};

use tmf_client::common::tmf_error::TMFError;
Expand Down Expand Up @@ -36,19 +42,22 @@ pub enum TMF620Modules {
},
}

pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Option<QueryOptions>, output : Output) -> Result<(),TMFError> {
match module {
TMF620Modules::Catalog { op } => {
match op {
TMFOperation::List => {
let catalogs = client.tmf620().catalog().list(opts)?;
iterate_name(&catalogs);
iterate_name(&catalogs,output);
Ok(())
},
TMFOperation::Get { id } => {
let catalog = client.tmf620().catalog().get(id)?;
let the_first = catalog.first().unwrap();
display_name(the_first);
match output {
Output::Json => display_json(the_first),
Output::Text => display_name(the_first),
}
Ok(())
}
_ => {
Expand All @@ -60,7 +69,7 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
match op {
TMFOperation::List => {
let categories = client.tmf620().category().list(opts)?;
iterate_name(&categories);
iterate_name(&categories,output);
Ok(())
},
_ => {
Expand All @@ -72,7 +81,7 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
match op {
TMFOperation::List => {
let offerings = client.tmf620().product_offering().list(opts)?;
iterate_name(&offerings);
iterate_name(&offerings,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand All @@ -90,7 +99,7 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
match op {
TMFOperation::List => {
let specifications = client.tmf620().product_specification().list(opts)?;
iterate_name(&specifications);
iterate_name(&specifications,output);
Ok(())
},
_ => {
Expand All @@ -103,7 +112,7 @@ pub fn handle_tmf620(client : &mut TMFClient, module : TMF620Modules, opts : Opt
match op {
TMFOperation::List => {
let prices = client.tmf620().product_offering_price().list(opts)?;
iterate_name(&prices);
iterate_name(&prices,output);
Ok(())
},
_ => {
Expand Down
6 changes: 4 additions & 2 deletions src/tmf/tmf629.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_name,
display_opt,
Expand All @@ -20,13 +22,13 @@ pub enum TMF629Modules {
},
}

pub fn handle_tmf629(client : &mut TMFClient, module : TMF629Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf629(client : &mut TMFClient, module : TMF629Modules, opts : Option<QueryOptions>,output : Output) -> Result<(),TMFError> {
match module {
TMF629Modules::Customer { op } => {
match op {
TMFOperation::List => {
let customers = client.tmf629().customer().list(opts)?;
iterate_name(&customers);
iterate_name(&customers,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand Down
8 changes: 5 additions & 3 deletions src/tmf/tmf632.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_name, display_opt, iterate_name, TMFOperation
};
Expand All @@ -21,13 +23,13 @@ pub enum TMF632Modules {
},
}

pub fn handle_tmf632(client : &mut TMFClient, module : TMF632Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf632(client : &mut TMFClient, module : TMF632Modules, opts : Option<QueryOptions>,output : Output) -> Result<(),TMFError> {
match module {
TMF632Modules::Individual { op } => {
match op {
TMFOperation::List => {
let individuals = client.tmf632().individual().list(opts)?;
iterate_name(&individuals);
iterate_name(&individuals,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand All @@ -51,7 +53,7 @@ pub fn handle_tmf632(client : &mut TMFClient, module : TMF632Modules, opts : Opt
match op {
TMFOperation::List => {
let organization = client.tmf632().organization().list(opts)?;
iterate_name(&organization);
iterate_name(&organization,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand Down
12 changes: 7 additions & 5 deletions src/tmf/tmf633.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_desc, display_name, iterate_name, TMFOperation
};
Expand Down Expand Up @@ -33,13 +35,13 @@ pub enum TMF633Modules {
},
}

pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Option<QueryOptions>, output : Output) -> Result<(),TMFError> {
match module {
TMF633Modules::Catalog { op } => {
match op {
TMFOperation::List => {
let catalogs = client.tmf633().catalog().list(opts)?;
iterate_name(&catalogs);
iterate_name(&catalogs,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand All @@ -57,7 +59,7 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
match op {
TMFOperation::List => {
let categories = client.tmf633().category().list(opts)?;
iterate_name(&categories);
iterate_name(&categories,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand All @@ -75,7 +77,7 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
match op {
TMFOperation::List => {
let candidates = client.tmf633().candidate().list(opts)?;
iterate_name(&candidates);
iterate_name(&candidates,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand All @@ -93,7 +95,7 @@ pub fn handle_tmf633(client : &mut TMFClient, module : TMF633Modules, opts : Opt
match op {
TMFOperation::List => {
let specifications = client.tmf633().specification().list(opts)?;
iterate_name(&specifications);
iterate_name(&specifications,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand Down
6 changes: 4 additions & 2 deletions src/tmf/tmf648.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_desc,
display_opt,
Expand All @@ -22,13 +24,13 @@ pub enum TMF648Modules {
},
}

pub fn handle_tmf648(client : &mut TMFClient, module : TMF648Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf648(client : &mut TMFClient, module : TMF648Modules, opts : Option<QueryOptions>,output : Output) -> Result<(),TMFError> {
match module {
TMF648Modules::Quote { op } => {
match op {
TMFOperation::List => {
let quotes = client.tmf648().quote().list(opts)?;
iterate_name(&quotes);
iterate_name(&quotes,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand Down
6 changes: 4 additions & 2 deletions src/tmf/tmf674.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use clap::Subcommand;

use crate::Output;

use super::{
display_name,
display_opt,
Expand All @@ -20,13 +22,13 @@ pub enum TMF674Modules {
},
}

pub fn handle_tmf674(client : &mut TMFClient, module : TMF674Modules, opts : Option<QueryOptions>) -> Result<(),TMFError> {
pub fn handle_tmf674(client : &mut TMFClient, module : TMF674Modules, opts : Option<QueryOptions>,output : Output) -> Result<(),TMFError> {
match module {
TMF674Modules::Site { op } => {
match op {
TMFOperation::List => {
let sites = client.tmf674().site().list(opts)?;
iterate_name(&sites);
iterate_name(&sites,output);
Ok(())
},
TMFOperation::Get { id } => {
Expand Down
Loading