From 09a9d36b2c63f173afbabd32cebf255b90310961 Mon Sep 17 00:00:00 2001 From: rfitzger Date: Fri, 20 Dec 2024 13:40:04 -0700 Subject: [PATCH] COMPASS_PROGRESS env variable --- .../src/util/fs/read_utils.rs | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/rust/routee-compass-core/src/util/fs/read_utils.rs b/rust/routee-compass-core/src/util/fs/read_utils.rs index e30ccce1..edd96722 100644 --- a/rust/routee-compass-core/src/util/fs/read_utils.rs +++ b/rust/routee-compass-core/src/util/fs/read_utils.rs @@ -1,7 +1,7 @@ use super::fs_utils; use csv::ReaderBuilder; use flate2::read::GzDecoder; -use kdam::{BarBuilder, BarExt}; +use kdam::{Bar, BarBuilder, BarExt}; use std::{ fs::File, @@ -12,6 +12,10 @@ use std::{ type CsvCallback<'a, T> = Option>; type RawCallback<'a> = Option>; +/// environment variable used to denote if the progress bar should be used. +/// if COMPASS_PROGRESS=false, the bar is deactivated, otherwise it runs. +const COMPASS_PROGRESS: &str = "COMPASS_PROGRESS"; + /// reads from a CSV into an iterator of T records. /// building the iterator may fail with an io::Error. /// each row hasn't yet been decoded so it is provided in a Result @@ -58,12 +62,7 @@ pub fn from_csv<'a, T>( where T: serde::de::DeserializeOwned + 'a, { - // build the progress bar if the user provided it and the logging system is at least INFO - let bar_opt = if log::log_enabled!(log::Level::Info) { - progress.and_then(|b| b.build().ok()) - } else { - None - }; + let bar_opt = build_bar(progress); let row_callback: CsvCallback<'a, T> = match (callback, bar_opt) { (None, None) => None, @@ -100,12 +99,7 @@ pub fn read_raw_file( where F: AsRef, { - // build the progress bar if the user provided it and the logging system is at least INFO - let bar_opt = if log::log_enabled!(log::Level::Info) { - progress.and_then(|b| b.build().ok()) - } else { - None - }; + let bar_opt = build_bar(progress); let row_callback: RawCallback = match (callback, bar_opt) { (None, None) => None, @@ -173,6 +167,32 @@ where Ok(result.into_boxed_slice()) } +/// helper function for building a progress bar. +/// a progress bar is created only if: +/// - the `progress` argument is not None +/// - the logging system is set to DEBUG or INFO +/// - the COMPASS_PROGRESS environment variable is not set to "false" +/// +/// # Arguments +/// +/// * `progress` - progress bar configuration +/// +/// # Returns +/// +/// Some progress bar if it should be built, else None +fn build_bar(progress: Option) -> Option { + let progress_disabled = std::env::var(COMPASS_PROGRESS) + .ok() + .map(|v| v.to_lowercase() == "false") + .unwrap_or_default(); + let log_info_enabled = log::log_enabled!(log::Level::Info); + if !progress_disabled && log_info_enabled { + progress.and_then(|b| b.build().ok()) + } else { + None + } +} + #[cfg(test)] mod tests { use std::path::PathBuf;