aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-21 00:18:38 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-21 00:18:38 +0000
commit883b9c65fce7f8596b7da5616efab8c4ecc616a4 (patch)
tree538ec099430b9e83577b3849a509d924cd3f97ec /src
parentf69d2801069850b48c6424b50d0107799d56e2f8 (diff)
Added support for JQ filtering.
Diffstat (limited to 'src')
-rw-r--r--src/barbfile.rs46
-rw-r--r--src/executor.rs4
-rw-r--r--src/main.rs26
3 files changed, 49 insertions, 27 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs
index 1a17796..19a24cd 100644
--- a/src/barbfile.rs
+++ b/src/barbfile.rs
@@ -80,16 +80,16 @@ impl Header {
}
}
-struct BarbHeader {
+struct BarbPreamble {
pub method: Method,
pub url: String,
pub headers: Vec<Header>,
pub filter: Option<String>,
}
-impl BarbHeader {
+impl BarbPreamble {
fn new(method: Method, url: String, headers: Vec<Header>, filter: Option<String>) -> Self {
- BarbHeader {
+ BarbPreamble {
method: method,
url: url,
headers: headers,
@@ -99,25 +99,29 @@ impl BarbHeader {
}
pub struct BarbFile {
- header: BarbHeader,
+ preamble: BarbPreamble,
body: Option<String>,
}
impl BarbFile {
pub fn headers(&self) -> &Vec<Header> {
- &self.header.headers
+ &self.preamble.headers
}
pub fn method(&self) -> &Method {
- &self.header.method
+ &self.preamble.method
}
pub fn method_as_string(&self) -> String {
- self.header.method.to_string()
+ self.preamble.method.to_string()
}
pub fn url(&self) -> &String {
- &self.header.url
+ &self.preamble.url
+ }
+
+ pub fn filter(&self) -> &Option<String> {
+ &self.preamble.filter
}
pub fn body(&self) -> &Option<String> {
@@ -172,7 +176,7 @@ impl FromStr for BarbFile {
let body = lines.fold(String::from(""), |acc, x| acc + x);
Ok(BarbFile {
- header: BarbHeader::new(method, url, headers, filter),
+ preamble: BarbPreamble::new(method, url, headers, filter),
body: if body == "" { None } else { Some(body) },
})
}
@@ -214,12 +218,12 @@ mod tests {
let barbfile =
BarbFile::from_str("#GET^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n")
.unwrap();
- assert!(matches!(barbfile.header.method, Method::GET));
- assert_eq!(barbfile.header.url, "https://blah.com/api/blah");
- assert_eq!(barbfile.header.filter, Some(String::from("filtr")));
- assert_eq!(barbfile.header.headers.len(), 1);
- assert_eq!(barbfile.header.headers[0].name, "Authorization");
- assert_eq!(barbfile.header.headers[0].value, "BLAH");
+ assert!(matches!(barbfile.preamble.method, Method::GET));
+ assert_eq!(barbfile.preamble.url, "https://blah.com/api/blah");
+ assert_eq!(barbfile.preamble.filter, Some(String::from("filtr")));
+ assert_eq!(barbfile.preamble.headers.len(), 1);
+ assert_eq!(barbfile.preamble.headers[0].name, "Authorization");
+ assert_eq!(barbfile.preamble.headers[0].value, "BLAH");
assert_eq!(barbfile.body, None);
}
@@ -228,12 +232,12 @@ mod tests {
let barbfile =
BarbFile::from_str("#POST^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n\n{\"key\":\"value\"}\n")
.unwrap();
- assert!(matches!(barbfile.header.method, Method::POST));
- assert_eq!(barbfile.header.url, "https://blah.com/api/blah");
- assert_eq!(barbfile.header.filter, Some(String::from("filtr")));
- assert_eq!(barbfile.header.headers.len(), 1);
- assert_eq!(barbfile.header.headers[0].name, "Authorization");
- assert_eq!(barbfile.header.headers[0].value, "BLAH");
+ assert!(matches!(barbfile.preamble.method, Method::POST));
+ assert_eq!(barbfile.preamble.url, "https://blah.com/api/blah");
+ assert_eq!(barbfile.preamble.filter, Some(String::from("filtr")));
+ assert_eq!(barbfile.preamble.headers.len(), 1);
+ assert_eq!(barbfile.preamble.headers[0].name, "Authorization");
+ assert_eq!(barbfile.preamble.headers[0].value, "BLAH");
assert_eq!(barbfile.body, Some(String::from("{\"key\":\"value\"}")))
}
}
diff --git a/src/executor.rs b/src/executor.rs
index 4930c5f..bd9767b 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -95,8 +95,8 @@ impl Executor {
}
}
- pub fn execute(&mut self, bfile: BarbFile, print_headers: bool) -> Result<ureq::Response, String> {
- self.run(&bfile, self.make_req(&bfile, print_headers))
+ pub fn execute(&mut self, bfile: &BarbFile, print_headers: bool) -> Result<ureq::Response, String> {
+ self.run(bfile, self.make_req(&bfile, print_headers))
}
}
diff --git a/src/main.rs b/src/main.rs
index f3891ad..a35ad7d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,7 @@
mod barbfile;
mod executor;
+use jq_rs;
use jsonformat::{format_json, Indentation};
use std::slice::Iter;
@@ -24,6 +25,8 @@ struct Args {
body: bool,
#[clap(short, long)]
raw: bool,
+ #[clap(short, long)]
+ filter: Option<String>,
files: Vec<String>,
}
@@ -48,6 +51,20 @@ impl Args {
{
self.files.iter()
}
+
+ pub fn jq_filter(&self) -> &Option<String>
+ {
+ &self.filter
+ }
+}
+
+fn apply_filters(args: &Args, bfile: &BarbFile, body: String) -> Result<String, String> {
+ if let Some(filter) = args.jq_filter() {
+ return Ok(jq_rs::run(filter.as_str(), body.as_str()).map_err(|x| x.to_string())?);
+ } else if let Some(filter) = bfile.filter() {
+ return Ok(jq_rs::run(filter.as_str(), body.as_str()).map_err(|x| x.to_string())?);
+ }
+ Ok(String::from(body))
}
fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<(), String> {
@@ -56,7 +73,7 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
.map_err(|_| format!("Failed to read file '{}'", file_name))?
.as_str(),
).map_err(|_| format!("Failed to parse file '{}'", file_name))?;
- let response = executor.execute(bfile, args.req_headers())?;
+ let response = executor.execute(&bfile, args.req_headers())?;
if args.print_headers() {
println!("{} {}", response.status(), response.status_text());
@@ -71,12 +88,13 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
}
if args.print_body() {
+ let body = apply_filters(args, &bfile, response.into_string().unwrap())?;
println!(
"{}",
match args.raw_body() {
- true => String::from(response.into_string().unwrap().as_str()),
+ true => body,
false => format_json(
- response.into_string().unwrap().as_str(),
+ body.as_str(),
Indentation::Default
),
}
@@ -92,7 +110,7 @@ fn main() {
let mut executor = Executor::new(Context::new(env::vars()));
for file in args.files_iter() {
- match run_file(&args, &mut executor, &file) {
+ match run_file(&args, &mut executor, file) {
Ok(()) => (),
Err(err) => println!("{}", err)
}