feat: allow specifying fallback env vars in [env]
This commit is contained in:
parent
553a5fc68e
commit
81661bb136
4 changed files with 29 additions and 3 deletions
|
@ -1,6 +1,9 @@
|
|||
[client]
|
||||
user_agent = "postwoman@sample/0.2.0"
|
||||
|
||||
[env]
|
||||
PW_TOKEN = "set-me-as-and-environment-variable!"
|
||||
|
||||
[route.healthcheck]
|
||||
url = "https://api.alemi.dev/"
|
||||
|
||||
|
|
|
@ -74,6 +74,7 @@ async fn main() -> Result<(), PostWomanError> {
|
|||
let pattern = regex::Regex::new(&query)?;
|
||||
let mut joinset = tokio::task::JoinSet::new();
|
||||
let client = Arc::new(config.client);
|
||||
let env = Arc::new(config.env);
|
||||
for (name, endpoint) in config.route {
|
||||
if pattern.find(&name).is_some() {
|
||||
for i in 0..repeat {
|
||||
|
@ -83,13 +84,14 @@ async fn main() -> Result<(), PostWomanError> {
|
|||
"".to_string()
|
||||
};
|
||||
let _client = client.clone();
|
||||
let _env = env.clone();
|
||||
let _endpoint = endpoint.clone();
|
||||
let _name = name.clone();
|
||||
let task = async move {
|
||||
let before = chrono::Local::now();
|
||||
eprintln!(" : [{}] sending {_name} {suffix}...", before.format(TIMESTAMP_FMT));
|
||||
let res = _endpoint
|
||||
.fill()
|
||||
.fill(&_env)
|
||||
.execute(&_client)
|
||||
.await;
|
||||
(res, _name, before, suffix)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::str::FromStr;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use http::{HeaderMap, HeaderName, HeaderValue};
|
||||
|
@ -49,9 +49,29 @@ fn replace_recursive(element: toml::Value, from: &str, to: &str) -> toml::Value
|
|||
}
|
||||
}
|
||||
|
||||
fn stringify(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(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
pub fn fill(mut self) -> Self {
|
||||
pub fn fill(mut self, env: &toml::Table) -> Self {
|
||||
let mut vars: HashMap<String, String> = env.into_iter()
|
||||
.map(|(k, v)| (k.clone(), stringify(v)))
|
||||
.collect();
|
||||
|
||||
for (k, v) in std::env::vars() {
|
||||
vars.insert(k, v);
|
||||
}
|
||||
|
||||
for (k, v) in vars {
|
||||
let k_var = format!("${{{k}}}");
|
||||
self.url = self.url.replace(&k_var, &v);
|
||||
if let Some(method) = self.method {
|
||||
|
|
|
@ -9,6 +9,7 @@ pub use extractor::Extractor;
|
|||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanConfig {
|
||||
pub client: PostWomanClient,
|
||||
pub env: toml::Table,
|
||||
// it's weird to name it singular but makes more sense in config
|
||||
pub route: indexmap::IndexMap<String, Endpoint>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue