use anyhow::{Context, Result};
use code2prompt_core::configuration::{OutputDestination, TomlConfig};
use colored::*;
use log::{debug, info};
use std::path::Path;
#[derive(Debug, Clone)]
pub struct ConfigSource {
pub config: TomlConfig,
}
pub fn load_config(quiet: bool) -> Result<ConfigSource> {
let local_config_path = std::env::current_dir()?.join(".c2pconfig");
if local_config_path.exists() {
match load_config_from_file(&local_config_path) {
Ok(config) => {
if !quiet {
eprintln!(
"{}{}{} Using config from: {}",
"[".bold().white(),
"i".bold().blue(),
"]".bold().white(),
local_config_path.display()
);
}
info!("Loaded local config from: {}", local_config_path.display());
return Ok(ConfigSource { config });
}
Err(e) => {
debug!("Failed to load local config: {}", e);
}
}
}
if let Some(config_dir) = dirs::config_dir() {
let global_config_path = config_dir.join("code2prompt").join(".c2pconfig");
if global_config_path.exists() {
match load_config_from_file(&global_config_path) {
Ok(config) => {
if !quiet {
eprintln!(
"{}{}{} Using config from: {}",
"[".bold().white(),
"i".bold().blue(),
"]".bold().white(),
global_config_path.display()
);
}
info!(
"Loaded global config from: {}",
global_config_path.display()
);
return Ok(ConfigSource { config });
}
Err(e) => {
debug!("Failed to load global config: {}", e);
}
}
}
}
if !quiet {
eprintln!(
"{}{}{} Using default configuration",
"[".bold().white(),
"i".bold().blue(),
"]".bold().white(),
);
}
info!("Using default configuration");
Ok(ConfigSource {
config: TomlConfig::default(),
})
}
fn load_config_from_file(path: &Path) -> Result<TomlConfig> {
let content = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read config file: {}", path.display()))?;
TomlConfig::from_toml_str(&content)
.with_context(|| format!("Failed to parse TOML config file: {}", path.display()))
}
pub fn get_default_output_destination(config_source: &ConfigSource) -> OutputDestination {
config_source.config.default_output.clone()
}