feat(cli): implement tsv format for queries

feat/type-attributes
Tomáš Mládek 2023-05-04 19:32:08 +02:00
parent 9817fbf42f
commit b88d859c98
1 changed files with 27 additions and 8 deletions

View File

@ -10,6 +10,7 @@ use regex::Captures;
use regex::Regex;
use reqwest::Url;
use serde_json::json;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::path::Path;
use std::path::PathBuf;
@ -196,16 +197,34 @@ fn main() -> Result<()> {
let client = reqwest::blocking::Client::new();
let response = client.post(api_url).body(query).send()?;
match response.error_for_status_ref() {
Ok(_) => match format {
OutputFormat::Json | OutputFormat::Raw => Ok(println!("{}", response.text()?)),
OutputFormat::Tsv => todo!(),
},
Err(err) => {
error!("{}", response.text()?);
Err(err.into())
response.error_for_status_ref()?;
match format {
OutputFormat::Json | OutputFormat::Raw => println!("{}", response.text()?),
OutputFormat::Tsv => {
eprintln!(
"entity\tattribute\tvalue\ttimestamp\tprovenance"
);
response
.json::<HashMap<String, serde_json::Value>>()?
.iter()
.for_each(|(_, entry)| {
println!(
"{}\t{}\t{}\t{}\t{}",
entry.get("entity").and_then(|e| e.as_str()).unwrap(),
entry.get("attribute").and_then(|a| a.as_str()).unwrap(),
entry
.get("value")
.and_then(|v| v.get("c"))
.unwrap(),
entry.get("timestamp").and_then(|t| t.as_str()).unwrap(),
entry.get("provenance").and_then(|p| p.as_str()).unwrap(),
)
})
}
}
Ok(())
}
Commands::Insert {
url,