feat: inherit env from parent collections
This commit is contained in:
parent
afc554f497
commit
ac92c53799
5 changed files with 35 additions and 28 deletions
|
@ -1,13 +1,14 @@
|
||||||
|
include = ["other.toml", "api/auth.toml"] # include other postwoman collections relative to this one
|
||||||
|
|
||||||
|
[env] # these will be replaced in fields and inherited by includes. environment vars overrule these
|
||||||
|
PW_TOKEN = "set-me-as-and-environment-variable!"
|
||||||
|
|
||||||
[client] # HTTP client configuration
|
[client] # HTTP client configuration
|
||||||
user_agent = "postwoman@sample/0.4.1"
|
user_agent = "postwoman@sample/0.4.1"
|
||||||
timeout = 60 # max time for each request to complete, in seconds
|
timeout = 60 # max time for each request to complete, in seconds
|
||||||
redirects = 5 # allow up to five redirects, defaults to none
|
redirects = 5 # allow up to five redirects, defaults to none
|
||||||
base = "https://api.alemi.dev" # all route urls will be appended to this base
|
base = "https://api.alemi.dev" # all route urls will be appended to this base
|
||||||
|
|
||||||
[env] # these will be replaced in routes options. environment vars overrule these
|
|
||||||
PW_TOKEN = "set-me-as-and-environment-variable!"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[route.healthcheck] # the simplest possible route: just name and path
|
[route.healthcheck] # the simplest possible route: just name and path
|
||||||
path = "/"
|
path = "/"
|
||||||
|
|
|
@ -59,12 +59,12 @@ impl PrintableResult for ListResult {
|
||||||
for (namespace, collection) in collections {
|
for (namespace, collection) in collections {
|
||||||
println!("-> {namespace}");
|
println!("-> {namespace}");
|
||||||
|
|
||||||
for (key, value) in collection.env.unwrap_or_default() {
|
for (key, value) in collection.env {
|
||||||
println!(" + {key}={}", crate::ext::stringify_toml(&value));
|
println!(" + {key}={}", crate::ext::stringify_toml(&value));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, endpoint) in collection.route.unwrap_or_default() {
|
for (name, endpoint) in collection.route {
|
||||||
let url = endpoint.url(collection.client.as_ref().and_then(|x| x.base.as_deref()))
|
let url = endpoint.url(collection.client.base.as_deref())
|
||||||
.split('?')
|
.split('?')
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -7,6 +7,7 @@ use std::str::FromStr;
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
|
use ext::FillableFromEnvironment;
|
||||||
use fmt::{PrintableResult, ReportableResult};
|
use fmt::{PrintableResult, ReportableResult};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
pub use model::PostWomanCollection;
|
pub use model::PostWomanCollection;
|
||||||
|
@ -79,7 +80,7 @@ fn main() {
|
||||||
|
|
||||||
let mut collections = IndexMap::new();
|
let mut collections = IndexMap::new();
|
||||||
|
|
||||||
if !load_collections(&mut collections, args.collection.clone()) {
|
if !load_collections(&mut collections, args.collection.clone(), &toml::Table::default()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,10 +148,10 @@ async fn run_collection_endpoints(
|
||||||
) {
|
) {
|
||||||
// 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 env = std::sync::Arc::new(collection.env);
|
||||||
let env = std::sync::Arc::new(collection.env.unwrap_or_default());
|
let client = std::sync::Arc::new(collection.client.fill(&env));
|
||||||
|
|
||||||
for (name, mut endpoint) in collection.route.unwrap_or_default() {
|
for (name, mut endpoint) in collection.route {
|
||||||
let full_name = ext::full_name(&namespace, &name);
|
let full_name = ext::full_name(&namespace, &name);
|
||||||
if pattern.find(&full_name).is_none() { continue };
|
if pattern.find(&full_name).is_none() { continue };
|
||||||
|
|
||||||
|
@ -195,7 +196,7 @@ async fn run_collection_endpoints(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path: std::path::PathBuf) -> bool {
|
fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path: std::path::PathBuf, parent_env: &toml::Table) -> 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) => {
|
||||||
|
@ -204,7 +205,7 @@ fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path:
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let collection: PostWomanCollection = match toml::from_str(&collection_raw) {
|
let mut collection: PostWomanCollection = match toml::from_str(&collection_raw) {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("! error parsing collection {path:?}: {e}");
|
eprintln!("! error parsing collection {path:?}: {e}");
|
||||||
|
@ -212,23 +213,24 @@ fn load_collections(store: &mut IndexMap<String, PostWomanCollection>, mut path:
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
collection.env.extend(parent_env.iter().map(|(k, v)| (k.clone(), v.clone())));
|
||||||
|
|
||||||
let name = path.to_string_lossy().replace(".toml", "");
|
let name = path.to_string_lossy().replace(".toml", "");
|
||||||
let mut to_include = Vec::new();
|
let mut to_include = Vec::new();
|
||||||
|
|
||||||
if let Some(ref includes) = collection.include {
|
path.pop();
|
||||||
path.pop();
|
for include in &collection.include {
|
||||||
for include in includes {
|
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);
|
to_include.push(base);
|
||||||
to_include.push(base);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let parent_env = collection.env.clone();
|
||||||
store.insert(name, collection);
|
store.insert(name, collection);
|
||||||
|
|
||||||
for base in to_include {
|
for base in to_include {
|
||||||
if !load_collections(store, base) {
|
if !load_collections(store, base, &parent_env) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{collections::HashMap, str::FromStr};
|
use std::str::FromStr;
|
||||||
|
|
||||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||||
use http::method::InvalidMethod;
|
use http::method::InvalidMethod;
|
||||||
|
@ -8,7 +8,7 @@ use jaq_interpret::FilterT;
|
||||||
use crate::errors::InvalidHeaderError;
|
use crate::errors::InvalidHeaderError;
|
||||||
use crate::{PostWomanError, APP_USER_AGENT};
|
use crate::{PostWomanError, APP_USER_AGENT};
|
||||||
|
|
||||||
use crate::ext::{stringify_json, stringify_toml, FillableFromEnvironment, StringOr};
|
use crate::ext::{stringify_json, FillableFromEnvironment, StringOr};
|
||||||
use super::{ExtractorConfig, ClientConfig};
|
use super::{ExtractorConfig, ClientConfig};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,13 @@ pub use extractor::ExtractorConfig;
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct PostWomanCollection {
|
pub struct PostWomanCollection {
|
||||||
pub client: Option<ClientConfig>,
|
#[serde(default)]
|
||||||
pub env: Option<toml::Table>,
|
pub client: ClientConfig,
|
||||||
pub include: Option<Vec<String>>,
|
#[serde(default)]
|
||||||
|
pub include: Vec<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub env: toml::Table,
|
||||||
|
#[serde(default)]
|
||||||
|
pub route: indexmap::IndexMap<String, EndpointConfig>,
|
||||||
// it's weird to name it singular but makes more sense in config
|
// it's weird to name it singular but makes more sense in config
|
||||||
pub route: Option<indexmap::IndexMap<String, EndpointConfig>>,
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue