aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs45
1 files changed, 3 insertions, 42 deletions
diff --git a/src/main.rs b/src/main.rs
index bbe2bf0..2973b27 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,18 +2,14 @@ mod barbfile;
mod executor;
mod output;
-use jq_rs;
use std::slice::Iter;
-use barbfile::BarbFile;
use executor::{Context, Executor};
use output::BarbOutput;
use clap::Parser;
use std::env;
-use std::fs;
-use std::str::FromStr;
use dotenv::dotenv;
@@ -58,50 +54,15 @@ impl Args {
}
}
-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> {
- let bfile = BarbFile::from_str(
- fs::read_to_string(file_name.as_str())
- .map_err(|_| format!("Failed to read file '{}'", file_name))?
- .as_str(),
- )
- .map_err(|_| format!("Failed to parse file '{}'", file_name))?;
- let output = args.output();
- let response = executor.execute(&bfile, &output)?;
-
- 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(())
-}
-
fn main() {
let args = Args::parse();
dotenv().ok();
let mut executor = Executor::new(Context::new(env::vars()));
+ let output = args.output();
for file in args.files_iter() {
- match run_file(&args, &mut executor, file) {
+ match executor.execute(file, &output, args.jq_filter()) {
+ //match run_file(&args, &mut executor, file) {
Ok(()) => (),
Err(err) => println!("{}", err),
}