diff options
author | Guillaume Pasquet <dev@etenil.net> | 2022-02-12 22:19:01 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2022-02-12 22:19:01 +0000 |
commit | 411e83ef44c4fd9ea0d4489f29b35bb2d47c388b (patch) | |
tree | fdaa61168741906758c01e7d53d3d52983c8c858 /src | |
parent | 9101f32f7d258984fb8f8247de8bc661acd69d47 (diff) |
Added command-line args, execution in context
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 92 |
1 files changed, 55 insertions, 37 deletions
diff --git a/src/main.rs b/src/main.rs index 182376c..25749de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,54 +2,72 @@ mod barbfile; use jsonformat::{format_json, Indentation}; -// use std::collections::HashMap; -// use std::env; +use barbfile::BarbFile; + +use clap::Parser; + +use std::collections::HashMap; +use std::env; use std::fs; use std::str::FromStr; use ureq; -// struct Context { -// vars: HashMap<String, String>, -// } - -// impl Context { -// pub fn new() -> Context { -// Context { -// vars: HashMap::new(), -// } -// } - -// pub fn get_var(&self, name: String) -> Option<String> { -// self.vars -// .get(&name) -// .map(|val| val.clone()) -// .or_else(|| env::var(name).ok()) -// } -// } +struct Context { + vars: HashMap<String, String>, +} + +impl Context { + pub fn new() -> Context { + Context { + vars: HashMap::new(), + } + } + + pub fn get_var(&self, name: String) -> Option<String> { + self.vars + .get(&name) + .map(|val| val.clone()) + .or_else(|| env::var(name).ok()) + } + + pub fn execute(&self, bfile: BarbFile) -> String { + let req = ureq::request(bfile.method_as_string().as_str(), &bfile.url()); + + let resp = match bfile.method().takes_body() { + true => match bfile.body() { + Some(body) => req.send_string(body.as_str()), + None => req.call(), + }, + false => req.call(), + }; + + resp.unwrap().into_string().unwrap() + } +} + +#[derive(Parser, Debug)] +#[clap(version)] +struct Args { + #[clap(short, long)] + headers: bool, + #[clap(short, long)] + body: bool, + #[clap(short, long)] + raw: bool, + files: Vec<String>, +} fn main() { - // let mut context = Context::new(); - let bfile = barbfile::BarbFile::from_str( + let args = Args::parse(); + + let mut context = Context::new(); + let bfile = BarbFile::from_str( fs::read_to_string("test-post.barb") .expect("Failed to read file") .as_str(), ) .expect("Failed to parse file"); - - let req = ureq::request(bfile.method_as_string().as_str(), &bfile.url()); - - let resp = match bfile.method().takes_body() { - true => match bfile.body() { - Some(body) => req.send_string(body.as_str()), - None => req.call() - }, - false => req.call(), - }; - - let result = resp.unwrap() - .into_string() - .unwrap(); + let result = context.execute(bfile); println!("{}", format_json(result.as_str(), Indentation::Default)); } - |