diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 68 |
1 files changed, 25 insertions, 43 deletions
diff --git a/src/main.rs b/src/main.rs index 6099020..a02938a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ mod barbfile; mod executor; +mod output; use jq_rs; -use jsonformat::{format_json, Indentation}; use std::slice::Iter; use barbfile::BarbFile; use executor::{Context, Executor}; +use output::BarbOutput; use clap::Parser; @@ -27,30 +28,14 @@ struct Args { raw: bool, #[clap(short, long)] filter: Option<String>, + #[clap(short, long)] + path: Option<String>, + #[clap(short, long)] + no_color: bool, files: Vec<String>, } impl Args { - pub fn print_req(&self) -> bool { - return !self.body - } - - pub fn print_headers(&self) -> bool { - self.headers || self.all_headers || !self.body - } - - pub fn print_body(&self) -> bool { - !self.headers || self.body - } - - pub fn raw_body(&self) -> bool { - self.raw - } - - pub fn req_headers(&self) -> bool { - self.all_headers - } - pub fn files_iter(&self) -> Iter<String> { self.files.iter() } @@ -58,6 +43,17 @@ impl Args { pub fn jq_filter(&self) -> &Option<String> { &self.filter } + + pub fn output(&self) -> BarbOutput { + BarbOutput::new( + !self.body, + self.all_headers, + self.headers || self.all_headers || !self.body, + !self.headers || self.body, + self.raw, + !self.no_color, + ) + } } fn apply_filters(args: &Args, bfile: &BarbFile, body: String) -> Result<String, String> { @@ -76,30 +72,16 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result< .as_str(), ) .map_err(|_| format!("Failed to parse file '{}'", file_name))?; - let response = executor.execute(&bfile, args.print_req(), args.req_headers())?; - - if args.print_headers() { - println!("{} {}", response.status(), response.status_text()); - for header_name in response.headers_names() { - println!( - "{}: {}", - header_name, - // Header is guaranteed to exist - response.header(header_name.as_str()).unwrap() - ); - } - } + let output = args.output(); + let response = executor.execute(&bfile, &output)?; - if args.print_body() { - let body = apply_filters(args, &bfile, response.into_string().unwrap())?; - println!( - "{}", - match args.raw_body() { - true => body, - false => format_json(body.as_str(), Indentation::Default), - } - ); + output.status(response.status(), response.status_text()); + for header_name in response.headers_names() { + output.resp_hdr(header_name.to_string(), response.header(header_name.as_str()).unwrap()); } + output.end_resp_hdr(); + + output.body(apply_filters(args, &bfile, response.into_string().unwrap())?); Ok(()) } |