From a4d5acfa3d14e40a05c64f718fefd22102f6f2d1 Mon Sep 17 00:00:00 2001 From: Jeroen Rinzema Date: Sun, 15 Nov 2026 10:59:24 +0000 Subject: [PATCH] fix: describe reads the configured API endpoint --- README.md | 2 +- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b216f3b..b00bfa6 100644 --- a/README.md +++ b/README.md @@ -431,7 +431,7 @@ brainpod describe resource app --json brainpod describe resource postgres --json ``` -Resource schemas are fetched from `https://api.prod.brainpod.io/v1/openapi.json` on each request. If the production document cannot be reached or does not contain the expected resource schemas, the version embedded in the CLI is used instead. Set `BRAINPOD_OPENAPI_URL` to override the document URL, or pass `--endpoint` to derive the document URL from another API endpoint. +Resource schemas are fetched on each request from `/v1/openapi.json`, where the endpoint is resolved like every other command's: `--endpoint`, then `BRAINPOD_API_ENDPOINT`, then the configured endpoint, then `https://api.brainpod.io`. No API token is needed. If the document cannot be reached or does not contain the expected resource schemas, the version embedded in the CLI is used instead. Set `BRAINPOD_OPENAPI_URL` to override the document URL entirely. Each schema carries the variables that kind exports, so the references an App resource can use in `spec.env` are discoverable before the resource exists. Names in this catalog are placeholders: `${.uri}` becomes `${db.uri}` for a resource named `db`. Template-sourced variables also report the template, which contains references only and never values. diff --git a/src/main.rs b/src/main.rs index b285be6..86992a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,8 +105,12 @@ async fn run(opts: Opts) -> Result { if let Command::Describe(args) = &opts.command { if openapi::is_resource_path(&args.command) { - let resource_description = - openapi::describe(&args.command, opts.endpoint.as_deref()).await?; + let endpoint = api_endpoint( + opts.endpoint.clone(), + environment("BRAINPOD_API_ENDPOINT"), + || Ok(Config::load(&Config::path()?)?.endpoint), + )?; + let resource_description = openapi::describe(&args.command, Some(&endpoint)).await?; if args.command.len() == 1 { let mut description = describe::generate(Opts::command(), &args.command)?; description @@ -138,14 +142,9 @@ async fn run(opts: Opts) -> Result { let config_path = Config::path()?; let mut config = Config::load(&config_path)?; - let endpoint = match opts - .endpoint - .or_else(|| environment("BRAINPOD_API_ENDPOINT")) - .or_else(|| config.endpoint.clone()) - { - Some(endpoint) => endpoint, - None => DEFAULT_ENDPOINT.to_owned(), - }; + let endpoint = api_endpoint(opts.endpoint, environment("BRAINPOD_API_ENDPOINT"), || { + Ok(config.endpoint.clone()) + })?; let api_token = opts .api_token .or_else(|| environment("BRAINPOD_API_TOKEN")) @@ -236,6 +235,19 @@ pub(crate) fn http_client_builder() -> Result { Ok(reqwest::Client::builder()) } +/// Resolves the API endpoint: flag, then `BRAINPOD_API_ENDPOINT`, then the configured +/// endpoint (loaded only when needed), then the production default. +fn api_endpoint( + flag: Option, + environment: Option, + config: impl FnOnce() -> Result>, +) -> Result { + if let Some(endpoint) = flag.or(environment) { + return Ok(endpoint); + } + Ok(config()?.unwrap_or_else(|| DEFAULT_ENDPOINT.to_owned())) +} + fn environment(name: &str) -> Option { std::env::var(name) .ok() @@ -430,6 +442,30 @@ mod tests { assert!(result.is_err()); } + #[test] + fn resolves_api_endpoint_by_precedence() { + let config = || Ok(Some("https://config.example".to_owned())); + let flag = Some("https://flag.example".to_owned()); + let env = Some("https://env.example".to_owned()); + + assert_eq!( + super::api_endpoint(flag, env.clone(), config).unwrap(), + "https://flag.example" + ); + assert_eq!( + super::api_endpoint(None, env, || panic!("config must not be loaded")).unwrap(), + "https://env.example" + ); + assert_eq!( + super::api_endpoint(None, None, config).unwrap(), + "https://config.example" + ); + assert_eq!( + super::api_endpoint(None, None, || Ok(None)).unwrap(), + super::DEFAULT_ENDPOINT + ); + } + #[test] fn parses_cluster_list() { let opts = Opts::try_parse_from(["brainpod", "cluster", "list"]).unwrap();