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 | |
parent | 9101f32f7d258984fb8f8247de8bc661acd69d47 (diff) |
Added command-line args, execution in context
-rw-r--r-- | Cargo.lock | 140 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 92 |
3 files changed, 193 insertions, 41 deletions
@@ -23,9 +23,16 @@ dependencies = [ ] [[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] name = "barb" version = "0.1.0" dependencies = [ + "clap 3.0.14", "jsonformat", "serde", "serde_json", @@ -77,13 +84,43 @@ dependencies = [ "ansi_term", "atty", "bitflags", - "strsim", - "textwrap", + "strsim 0.8.0", + "textwrap 0.11.0", "unicode-width", "vec_map", ] [[package]] +name = "clap" +version = "3.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim 0.10.0", + "termcolor", + "textwrap 0.14.2", +] + +[[package]] +name = "clap_derive" +version = "3.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "form_urlencoded" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -94,6 +131,18 @@ dependencies = [ ] [[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] name = "hermit-abi" version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -114,6 +163,16 @@ dependencies = [ ] [[package]] +name = "indexmap" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] name = "itoa" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -134,7 +193,7 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413d4928831a72ed2fdc794d1425be18e5f5ea822f92e557c52f150bcc182f35" dependencies = [ - "clap", + "clap 2.33.3", ] [[package]] @@ -165,18 +224,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] name = "once_cell" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" [[package]] +name = "os_str_bytes" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64" +dependencies = [ + "memchr", +] + +[[package]] name = "percent-encoding" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] name = "proc-macro2" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -267,6 +365,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] name = "syn" version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -278,6 +382,15 @@ dependencies = [ ] [[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] name = "textwrap" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -287,6 +400,12 @@ dependencies = [ ] [[package]] +name = "textwrap" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0066c8d12af8b5acd21e00547c3797fde4e8677254a7ee429176ccebbe93dd80" + +[[package]] name = "tinyvec" version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -371,6 +490,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] name = "wasm-bindgen" version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -470,6 +595,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -7,7 +7,7 @@ edition = "2018" [dependencies] ureq = { version = "2.3.1", features = ["json"] } +clap = { version = "3.0.14", features = ["derive"] } serde = "*" serde_json = "*" jsonformat = "1.2.0" - 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)); } - |