use colored::*;
use core::error::Error;
use std::fs;
use std::path::Path;
use rumdl_lib::config as rumdl_config;
use rumdl_lib::exit_codes::exit;
use crate::CheckArgs;
pub fn apply_cli_overrides(sourced: &mut rumdl_config::SourcedConfig, args: &CheckArgs) {
if let Some(flavor) = args.flavor {
sourced.global.flavor = rumdl_config::SourcedValue::new(flavor.into(), rumdl_config::ConfigSource::Cli);
}
if let Some(respect_gitignore) = args.respect_gitignore {
sourced.global.respect_gitignore =
rumdl_config::SourcedValue::new(respect_gitignore, rumdl_config::ConfigSource::Cli);
}
if let Some(ref fixable) = args.fixable {
let rules: Vec<String> = fixable
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
sourced.global.fixable = rumdl_config::SourcedValue::new(rules, rumdl_config::ConfigSource::Cli);
}
if let Some(ref unfixable) = args.unfixable {
let rules: Vec<String> = unfixable
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
sourced.global.unfixable = rumdl_config::SourcedValue::new(rules, rumdl_config::ConfigSource::Cli);
}
}
pub fn read_file_efficiently(path: &Path) -> Result<String, Box<dyn Error>> {
fs::read_to_string(path).map_err(|e| format!("Failed to read file {}: {}", path.display(), e).into())
}
pub fn load_config_with_cli_error_handling(config_path: Option<&str>, isolated: bool) -> rumdl_config::SourcedConfig {
load_config_with_cli_error_handling_with_dir(config_path, isolated, None)
}
pub fn load_config_with_cli_error_handling_with_dir(
config_path: Option<&str>,
isolated: bool,
discovery_dir: Option<&Path>,
) -> rumdl_config::SourcedConfig {
let result = if let Some(dir) = discovery_dir {
let absolute_config_path = config_path.map(|p| {
let path = Path::new(p);
if path.is_absolute() {
p.to_string()
} else if let Ok(canonical) = std::fs::canonicalize(path) {
canonical.to_string_lossy().to_string()
} else {
std::env::current_dir()
.map(|cwd| cwd.join(p).to_string_lossy().to_string())
.unwrap_or_else(|_| p.to_string())
}
});
let original_dir = std::env::current_dir().ok();
if dir.is_dir() {
let _ = std::env::set_current_dir(dir);
} else if let Some(parent) = dir.parent() {
let _ = std::env::set_current_dir(parent);
}
let config_result =
rumdl_config::SourcedConfig::load_with_discovery(absolute_config_path.as_deref(), None, isolated);
if let Some(orig) = original_dir {
let _ = std::env::set_current_dir(orig);
}
config_result
} else {
rumdl_config::SourcedConfig::load_with_discovery(config_path, None, isolated)
};
match result {
Ok(config) => config,
Err(e) => {
eprintln!("{}: {}", "Config error".red().bold(), e);
exit::tool_error();
}
}
}