use std::env;
use std::{borrow::Cow, sync::Arc};
use crate::transports::DefaultTransportFactory;
use crate::types::Dsn;
use crate::{ClientOptions, Integration};
pub fn apply_defaults(mut opts: ClientOptions) -> ClientOptions {
if opts.transport.is_none() {
opts.transport = Some(Arc::new(DefaultTransportFactory));
}
if opts.default_integrations {
let mut integrations: Vec<Arc<dyn Integration>> = vec![];
#[cfg(feature = "backtrace")]
{
integrations.push(Arc::new(sentry_backtrace::AttachStacktraceIntegration));
}
#[cfg(feature = "debug-images")]
{
integrations.push(Arc::new(
sentry_debug_images::DebugImagesIntegration::default(),
))
}
#[cfg(feature = "contexts")]
{
integrations.push(Arc::new(sentry_contexts::ContextIntegration::default()));
}
#[cfg(feature = "panic")]
{
integrations.push(Arc::new(sentry_panic::PanicIntegration::default()));
}
#[cfg(feature = "backtrace")]
{
integrations.push(Arc::new(sentry_backtrace::ProcessStacktraceIntegration));
}
integrations.extend(opts.integrations);
opts.integrations = integrations;
}
if opts.dsn.is_none() {
opts.dsn = env::var("SENTRY_DSN")
.ok()
.and_then(|dsn| dsn.parse::<Dsn>().ok());
}
if opts.release.is_none() {
opts.release = env::var("SENTRY_RELEASE").ok().map(Cow::Owned);
}
if opts.environment.is_none() {
opts.environment =
env::var("SENTRY_ENVIRONMENT")
.ok()
.map(Cow::Owned)
.or(Some(Cow::Borrowed(if cfg!(debug_assertions) {
"development"
} else {
"production"
})));
}
if opts.http_proxy.is_none() {
opts.http_proxy = std::env::var("HTTP_PROXY")
.ok()
.map(Cow::Owned)
.or_else(|| std::env::var("http_proxy").ok().map(Cow::Owned));
}
if opts.https_proxy.is_none() {
opts.https_proxy = std::env::var("HTTPS_PROXY")
.ok()
.map(Cow::Owned)
.or_else(|| std::env::var("https_proxy").ok().map(Cow::Owned))
.or_else(|| opts.http_proxy.clone());
}
if let Ok(accept_invalid_certs) = std::env::var("SSL_VERIFY") {
opts.accept_invalid_certs = !accept_invalid_certs.parse().unwrap_or(true);
}
opts
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_environment() {
let opts = ClientOptions {
environment: Some("explicit-env".into()),
..Default::default()
};
let opts = apply_defaults(opts);
assert_eq!(opts.environment.unwrap(), "explicit-env");
let opts = apply_defaults(Default::default());
assert_eq!(opts.environment.unwrap(), "development");
env::set_var("SENTRY_ENVIRONMENT", "env-from-env");
let opts = apply_defaults(Default::default());
assert_eq!(opts.environment.unwrap(), "env-from-env");
}
}