From 42e456ed8aa022b2f2828184eea01b8263ed0b10 Mon Sep 17 00:00:00 2001 From: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:02:27 +0200 Subject: [PATCH] fix: Only warn about mismatched URLs when they are different (#2143) Previously, we warned about a mismatch between the auth token URL and the manually configured URL even when they were the same. We also sometimes issued the warning when no URL was manually configured (i.e. when the default https://sentry.io/ was used). Now, we only warn when the two are actually different and we also only warn if a URL was actually manually configured. Fixes #2137 --- src/config.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/config.rs b/src/config.rs index c70c5e21cc..54bf8a5625 100644 --- a/src/config.rs +++ b/src/config.rs @@ -65,21 +65,16 @@ impl Config { _ => None, // get_default_auth never returns Auth::Token variant }; - let default_url = get_default_url(&ini); + let manually_configured_url = configured_url(&ini); let token_url = token_embedded_data .as_ref() .map(|td| td.url.as_str()) .unwrap_or_default(); let url = if token_url.is_empty() { - default_url + manually_configured_url.unwrap_or_else(|| DEFAULT_URL.to_string()) } else { - if !default_url.is_empty() { - log::warn!( - "Using {token_url} (embedded in token) rather than manually-configured URL {default_url}. \ - To use {default_url}, please provide an auth token for this URL." - ); - } + warn_about_conflicting_urls(token_url, manually_configured_url.as_deref()); token_url.into() }; @@ -535,6 +530,18 @@ impl Config { } } +fn warn_about_conflicting_urls(token_url: &str, manually_configured_url: Option<&str>) { + if let Some(manually_configured_url) = manually_configured_url { + if manually_configured_url != token_url { + warn!( + "Using {token_url} (embedded in token) rather than manually-configured URL \ + {manually_configured_url}. To use {manually_configured_url}, please provide an \ + auth token for {manually_configured_url}." + ); + } + } +} + fn find_global_config_file() -> Result { let home_dir_file = dirs::home_dir().map(|p| p.join(CONFIG_RC_FILE_NAME)); let config_dir_file = dirs::config_dir().map(|p| p.join(CONFIG_INI_FILE_PATH)); @@ -694,14 +701,13 @@ fn get_default_auth(ini: &Ini) -> Option { } } -fn get_default_url(ini: &Ini) -> String { - if let Ok(val) = env::var("SENTRY_URL") { - val - } else if let Some(val) = ini.get_from(Some("defaults"), "url") { - val.to_owned() - } else { - DEFAULT_URL.to_owned() - } +/// Returns the URL configured in the SENTRY_URL environment variable or provided ini (in that +/// order of precedence), or returns None if neither is set. +fn configured_url(ini: &Ini) -> Option { + env::var("SENTRY_URL").ok().or_else(|| { + ini.get_from(Some("defaults"), "url") + .map(|url| url.to_owned()) + }) } fn get_default_headers(ini: &Ini) -> Option> {