sql_cli/config/
global.rs

1use super::config::{BehaviorConfig, Config};
2use once_cell::sync::OnceCell;
3use std::sync::RwLock;
4
5// Global config instance
6static CONFIG: OnceCell<RwLock<Config>> = OnceCell::new();
7
8/// Initialize the global config. Should be called early in application startup.
9pub fn init_config(config: Config) {
10    CONFIG.set(RwLock::new(config)).ok();
11}
12
13/// Get the date notation preference from the global config.
14/// Returns "us" if config is not initialized or on error.
15/// Can be overridden by SQL_CLI_DATE_NOTATION environment variable for testing.
16pub fn get_date_notation() -> String {
17    // Check for environment variable override first (useful for testing)
18    if let Ok(notation) = std::env::var("SQL_CLI_DATE_NOTATION") {
19        return notation;
20    }
21
22    CONFIG.get().and_then(|c| c.read().ok()).map_or_else(
23        || "us".to_string(),
24        |c| c.behavior.default_date_notation.clone(),
25    )
26}
27
28/// Get a copy of the behavior config
29pub fn get_behavior_config() -> BehaviorConfig {
30    CONFIG
31        .get()
32        .and_then(|c| c.read().ok())
33        .map(|c| c.behavior.clone())
34        .unwrap_or_default()
35}
36
37/// Update the global config
38pub fn update_config(config: Config) {
39    if let Some(global) = CONFIG.get() {
40        if let Ok(mut c) = global.write() {
41            *c = config;
42        }
43    } else {
44        init_config(config);
45    }
46}