feat: improved output, allow pretty print

This commit is contained in:
əlemi 2023-06-15 17:10:24 +02:00
parent 1c103fd22d
commit afda46073a
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -51,11 +51,27 @@ pub enum PostWomanActions {
/// isolate each request client from others /// isolate each request client from others
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]
isolated: bool, isolated: bool,
/// pretty-print json outputs
#[arg(short, long, default_value_t = false)]
pretty: bool,
}, },
/// list saved requests /// list saved requests
Show {}, Show {},
} }
enum TestResult {
Success {
url: String,
method: String,
response: reqwest::Response,
},
Failure {
url: String,
method: String,
err: reqwest::Error,
}
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -122,7 +138,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// if args.verbose { println!(" │╵") } // if args.verbose { println!(" │╵") }
// }, // },
PostWomanActions::Test { isolated } => { PostWomanActions::Test { isolated, pretty } => {
let reqs = collection.requests(); let reqs = collection.requests();
let mut tasks = Vec::new(); let mut tasks = Vec::new();
@ -137,20 +153,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} else { _c }; } else { _c };
let url = req.url().as_str().to_string(); let url = req.url().as_str().to_string();
let method = req.method().as_str().to_string(); let method = req.method().as_str().to_string();
let r = c.execute(req).await?; let response = c.execute(req).await;
println!("{} {} >> {}", method, url, r.status()); match response {
if args.verbose { Ok(response) => TestResult::Success { url, method, response },
println!("{}", r.text().await?.replace("\n", "\n")); Err(err) => TestResult::Failure { url, method, err }
} }
Ok::<(), reqwest::Error>(())
}); });
tasks.push(t); tasks.push(t);
} }
for t in tasks { for t in tasks {
match t.await? { match t.await? {
Ok(_) => {}, TestResult::Success { url, method, response } => {
Err(e) => eprintln!("{}", e), println!(" ├ ✓ {} {} >> {}", method, url, response.status());
if args.verbose {
let mut body = response.text().await?;
if pretty {
body = serde_json::to_string_pretty(&serde_json::from_str::<serde_json::Value>(&body)?)?;
}
println!("{}", body.replace("\n", "\n"));
println!("");
}
},
TestResult::Failure { url, method, err } => {
println!("× {} {} >> ERROR", method, url);
if args.verbose {
println!("{}", err);
println!("");
}
}
} }
} }
}, },