Compare commits

..

2 commits

Author SHA1 Message Date
b9271ad47d
feat: add --dry-run option, add -M for multithread 2024-10-20 06:11:19 +02:00
083e605255
fix: preserve discovery order 2024-10-20 05:56:38 +02:00
2 changed files with 29 additions and 14 deletions

2
Cargo.lock generated
View file

@ -815,7 +815,7 @@ checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]] [[package]]
name = "postwoman" name = "postwoman"
version = "0.3.1" version = "0.3.2"
dependencies = [ dependencies = [
"base64", "base64",
"chrono", "chrono",

View file

@ -2,10 +2,11 @@ mod model;
mod errors; mod errors;
mod ext; mod ext;
use std::{collections::HashMap, str::FromStr}; use std::str::FromStr;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use indexmap::IndexMap;
pub use model::PostWomanCollection; pub use model::PostWomanCollection;
pub use errors::PostWomanError; pub use errors::PostWomanError;
@ -24,7 +25,7 @@ struct PostWomanArgs {
action: Option<PostWomanActions>, action: Option<PostWomanActions>,
/// start a multi-thread runtime, with multiple worker threads /// start a multi-thread runtime, with multiple worker threads
#[arg(long, default_value_t = false)] #[arg(short = 'M', long, default_value_t = false)]
multi_threaded: bool, multi_threaded: bool,
} }
@ -42,6 +43,10 @@ pub enum PostWomanActions {
/// force debug extractor on all routes /// force debug extractor on all routes
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]
debug: bool, debug: bool,
/// print matched routes but don't perform requests
#[arg(long, default_value_t = false)]
dry_run: bool,
}, },
/// show all registered routes in current collection /// show all registered routes in current collection
@ -66,7 +71,7 @@ fn main() {
} }
} }
let mut collections = HashMap::new(); let mut collections = IndexMap::new();
if !load_collections(&mut collections, args.collection.clone()) { if !load_collections(&mut collections, args.collection.clone()) {
return; return;
@ -104,7 +109,7 @@ fn main() {
} }
} }
fn load_collections(store: &mut HashMap<String, PostWomanCollection>, mut path: std::path::PathBuf) -> bool { fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path: std::path::PathBuf) -> bool {
let collection_raw = match std::fs::read_to_string(&path) { let collection_raw = match std::fs::read_to_string(&path) {
Ok(x) => x, Ok(x) => x,
Err(e) => { Err(e) => {
@ -122,6 +127,7 @@ fn load_collections(store: &mut HashMap<String, PostWomanCollection>, mut path:
}; };
let name = path.to_string_lossy().to_string(); let name = path.to_string_lossy().to_string();
let mut to_include = Vec::new();
if let Some(ref includes) = collection.include { if let Some(ref includes) = collection.include {
path.pop(); path.pop();
@ -129,14 +135,18 @@ fn load_collections(store: &mut HashMap<String, PostWomanCollection>, mut path:
let mut base = path.clone(); let mut base = path.clone();
let new = std::path::PathBuf::from_str(include).expect("infallible"); let new = std::path::PathBuf::from_str(include).expect("infallible");
base.push(new); base.push(new);
if !load_collections(store, base) { to_include.push(base);
return false;
}
} }
} }
store.insert(name, collection); store.insert(name, collection);
for base in to_include {
if !load_collections(store, base) {
return false;
}
}
true true
} }
@ -179,7 +189,7 @@ async fn run_postwoman(args: &PostWomanArgs, namespace: String, collection: Post
println!(); println!();
}, },
PostWomanActions::Run { query, parallel, debug } => { PostWomanActions::Run { query, parallel, debug, dry_run } => {
// this is always safe to compile because we tested it beforehand // this is always safe to compile because we tested it beforehand
let pattern = regex::Regex::new(query).expect("tested it before and still failed here???"); let pattern = regex::Regex::new(query).expect("tested it before and still failed here???");
let client = std::sync::Arc::new(collection.client.unwrap_or_default()); let client = std::sync::Arc::new(collection.client.unwrap_or_default());
@ -190,16 +200,21 @@ async fn run_postwoman(args: &PostWomanArgs, namespace: String, collection: Post
let _client = client.clone(); let _client = client.clone();
let _env = env.clone(); let _env = env.clone();
let _endpoint = endpoint.clone(); let _endpoint = endpoint.clone();
let _dry_run = *dry_run;
let _name = name.clone(); let _name = name.clone();
let _namespace = namespace.clone(); let _namespace = namespace.clone();
let task = async move { let task = async move {
let before = chrono::Local::now(); let before = chrono::Local::now();
eprintln!(" : [{}] {_namespace}::{_name} \tsending request...", before.format(TIMESTAMP_FMT)); eprintln!(" : [{}] {_namespace}::{_name} \tsending request...", before.format(TIMESTAMP_FMT));
if _dry_run {
(Ok("".to_string()), _namespace, _name, before)
} else {
let res = _endpoint let res = _endpoint
.fill(&_env) .fill(&_env)
.execute(&_client) .execute(&_client)
.await; .await;
(res, _namespace, _name, before) (res, _namespace, _name, before)
}
}; };
if *parallel { if *parallel {
pool.spawn(task); pool.spawn(task);