Compare commits

...

2 commits

Author SHA1 Message Date
68a73284c7
feat: add list action (as default) 2024-10-19 21:53:47 +02:00
58900630e1
chore: moved formatters into ext mod 2024-10-19 21:53:40 +02:00
3 changed files with 48 additions and 28 deletions

24
src/ext.rs Normal file
View file

@ -0,0 +1,24 @@
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(),
}
}

View file

@ -1,5 +1,6 @@
mod model; mod model;
mod errors; mod errors;
mod ext;
use std::sync::Arc; use std::sync::Arc;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -19,10 +20,10 @@ struct PostWomanArgs {
/// action to run /// action to run
#[clap(subcommand)] #[clap(subcommand)]
action: PostWomanActions, action: Option<PostWomanActions>,
} }
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug, Default)]
pub enum PostWomanActions { pub enum PostWomanActions {
/// execute specific endpoint requests /// execute specific endpoint requests
Run { Run {
@ -38,6 +39,10 @@ pub enum PostWomanActions {
repeat: u32, repeat: u32,
}, },
/// show all registered routes in current collection
#[default]
List,
// Save { // Save {
// /// name for new endpoint // /// name for new endpoint
// name: String, // name: String,
@ -69,7 +74,21 @@ async fn main() -> Result<(), PostWomanError> {
let collection = std::fs::read_to_string(args.collection)?; let collection = std::fs::read_to_string(args.collection)?;
let config: PostWomanConfig = toml::from_str(&collection)?; let config: PostWomanConfig = toml::from_str(&collection)?;
match args.action { 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);
}
},
PostWomanActions::Run { query, parallel, repeat } => { PostWomanActions::Run { query, parallel, repeat } => {
let pattern = regex::Regex::new(&query)?; let pattern = regex::Regex::new(&query)?;
let mut joinset = tokio::task::JoinSet::new(); let mut joinset = tokio::task::JoinSet::new();

View file

@ -50,29 +50,6 @@ 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 { impl Endpoint {
pub fn fill(mut self, env: &toml::Table) -> Self { pub fn fill(mut self, env: &toml::Table) -> Self {
let mut vars: HashMap<String, String> = HashMap::default(); let mut vars: HashMap<String, String> = HashMap::default();
@ -80,7 +57,7 @@ impl Endpoint {
vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string()); vars.insert("POSTWOMAN_TIMESTAMP".to_string(), chrono::Local::now().timestamp().to_string());
for (k, v) in env { for (k, v) in env {
vars.insert(k.to_string(), stringify_toml(v)); vars.insert(k.to_string(), crate::ext::stringify_toml(v));
} }
for (k, v) in std::env::vars() { for (k, v) in std::env::vars() {
@ -190,7 +167,7 @@ impl Endpoint {
let json: serde_json::Value = res.json().await?; let json: serde_json::Value = res.json().await?;
let selection = jq(&query, json)?; let selection = jq(&query, json)?;
if selection.len() == 1 { if selection.len() == 1 {
stringify_json(&selection[0]) + "\n" crate::ext::stringify_json(&selection[0]) + "\n"
} else { } else {
serde_json::to_string_pretty(&selection)? + "\n" serde_json::to_string_pretty(&selection)? + "\n"
} }