Compare commits
No commits in common. "68a73284c7aceff0a5551671467d820c12286619" and "6211a1630e732a4503e723cdbd229c28c6523b24" have entirely different histories.
68a73284c7
...
6211a1630e
3 changed files with 28 additions and 48 deletions
24
src/ext.rs
24
src/ext.rs
|
@ -1,24 +0,0 @@
|
|||
|
||||
pub fn stringify_toml(v: &toml::Value) -> String {
|
||||
match v {
|
||||
toml::Value::Boolean(x) => x.to_string(),
|
||||
toml::Value::Integer(x) => x.to_string(),
|
||||
toml::Value::Float(x) => x.to_string(),
|
||||
toml::Value::String(x) => x.clone(),
|
||||
toml::Value::Datetime(x) => x.to_string(),
|
||||
toml::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
toml::Value::Table(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stringify_json(v: &serde_json::Value) -> String {
|
||||
match v {
|
||||
serde_json::Value::Null => "null".to_string(),
|
||||
serde_json::Value::Bool(x) => x.to_string(),
|
||||
serde_json::Value::Number(x) => x.to_string(),
|
||||
serde_json::Value::String(x) => x.clone(),
|
||||
serde_json::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
serde_json::Value::Object(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
25
src/main.rs
25
src/main.rs
|
@ -1,6 +1,5 @@
|
|||
mod model;
|
||||
mod errors;
|
||||
mod ext;
|
||||
|
||||
use std::sync::Arc;
|
||||
use clap::{Parser, Subcommand};
|
||||
|
@ -20,10 +19,10 @@ struct PostWomanArgs {
|
|||
|
||||
/// action to run
|
||||
#[clap(subcommand)]
|
||||
action: Option<PostWomanActions>,
|
||||
action: PostWomanActions,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug, Default)]
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub enum PostWomanActions {
|
||||
/// execute specific endpoint requests
|
||||
Run {
|
||||
|
@ -39,10 +38,6 @@ pub enum PostWomanActions {
|
|||
repeat: u32,
|
||||
},
|
||||
|
||||
/// show all registered routes in current collection
|
||||
#[default]
|
||||
List,
|
||||
|
||||
// Save {
|
||||
// /// name for new endpoint
|
||||
// name: String,
|
||||
|
@ -74,21 +69,7 @@ async fn main() -> Result<(), PostWomanError> {
|
|||
let collection = std::fs::read_to_string(args.collection)?;
|
||||
let config: PostWomanConfig = toml::from_str(&collection)?;
|
||||
|
||||
match args.action.unwrap_or_default() {
|
||||
PostWomanActions::List => {
|
||||
let ua = config.client.user_agent.unwrap_or(APP_USER_AGENT.to_string());
|
||||
println!("> {ua}");
|
||||
|
||||
for (key, value) in config.env {
|
||||
println!("+ {key}: {}", ext::stringify_toml(&value));
|
||||
}
|
||||
|
||||
println!();
|
||||
|
||||
for (name, endpoint) in config.route {
|
||||
println!("- {name}: \t{} \t{}", endpoint.method.unwrap_or("GET".into()), endpoint.url);
|
||||
}
|
||||
},
|
||||
match args.action {
|
||||
PostWomanActions::Run { query, parallel, repeat } => {
|
||||
let pattern = regex::Regex::new(&query)?;
|
||||
let mut joinset = tokio::task::JoinSet::new();
|
||||
|
|
|
@ -50,6 +50,29 @@ fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value
|
|||
}
|
||||
}
|
||||
|
||||
fn stringify_toml(v: &toml::Value) -> String {
|
||||
match v {
|
||||
toml::Value::Boolean(x) => x.to_string(),
|
||||
toml::Value::Integer(x) => x.to_string(),
|
||||
toml::Value::Float(x) => x.to_string(),
|
||||
toml::Value::String(x) => x.clone(),
|
||||
toml::Value::Datetime(x) => x.to_string(),
|
||||
toml::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
toml::Value::Table(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn stringify_json(v: &serde_json::Value) -> String {
|
||||
match v {
|
||||
serde_json::Value::Null => "null".to_string(),
|
||||
serde_json::Value::Bool(x) => x.to_string(),
|
||||
serde_json::Value::Number(x) => x.to_string(),
|
||||
serde_json::Value::String(x) => x.clone(),
|
||||
serde_json::Value::Array(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
serde_json::Value::Object(x) => serde_json::to_string(&x).unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
pub fn fill(mut self, env: &toml::Table) -> Self {
|
||||
let mut vars: HashMap<String, String> = HashMap::default();
|
||||
|
@ -57,7 +80,7 @@ impl Endpoint {
|
|||
vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string());
|
||||
|
||||
for (k, v) in env {
|
||||
vars.insert(k.to_string(), crate::ext::stringify_toml(v));
|
||||
vars.insert(k.to_string(), stringify_toml(v));
|
||||
}
|
||||
|
||||
for (k, v) in std::env::vars() {
|
||||
|
@ -167,7 +190,7 @@ impl Endpoint {
|
|||
let json: serde_json::Value = res.json().await?;
|
||||
let selection = jq(&query, json)?;
|
||||
if selection.len() == 1 {
|
||||
crate::ext::stringify_json(&selection[0]) + "\n"
|
||||
stringify_json(&selection[0]) + "\n"
|
||||
} else {
|
||||
serde_json::to_string_pretty(&selection)? + "\n"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue