feat: add debug logging and make dir field optional in settings
Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
@@ -2,7 +2,7 @@ use std::path::PathBuf;
|
||||
use std::fs;
|
||||
use anyhow::{Result, Context};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use log::debug;
|
||||
use log::{debug, error};
|
||||
use crate::args::{Args};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
@@ -33,11 +33,15 @@ pub struct MetaPluginConfig {
|
||||
/// Unified settings that merges config file and CLI arguments
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Settings {
|
||||
#[serde(default)]
|
||||
pub dir: PathBuf,
|
||||
pub list_format: Vec<ColumnConfig>,
|
||||
#[serde(default)]
|
||||
pub human_readable: bool,
|
||||
pub output_format: Option<String>,
|
||||
#[serde(default)]
|
||||
pub quiet: bool,
|
||||
#[serde(default)]
|
||||
pub force: bool,
|
||||
pub server: Option<ServerConfig>,
|
||||
pub compression_plugin: Option<CompressionPluginConfig>,
|
||||
@@ -48,26 +52,41 @@ pub struct Settings {
|
||||
impl Settings {
|
||||
/// Create unified settings from config and args with proper priority
|
||||
pub fn new(args: &Args, default_dir: PathBuf) -> Result<Self> {
|
||||
debug!("CONFIG: Creating settings with default dir: {:?}", default_dir);
|
||||
|
||||
let config_path = if let Some(config_path) = &args.options.config {
|
||||
config_path.clone()
|
||||
} else if let Ok(env_config) = std::env::var("KEEP_CONFIG") {
|
||||
PathBuf::from(env_config)
|
||||
} else {
|
||||
Self::default_config_path().unwrap_or_else(|_| PathBuf::from("~/.config/keep/config.yml"))
|
||||
match Self::default_config_path() {
|
||||
Ok(path) => path,
|
||||
Err(e) => {
|
||||
debug!("CONFIG: Failed to get default config path: {}", e);
|
||||
PathBuf::from("~/.config/keep/config.yml")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
debug!("CONFIG: Using config path: {:?}", config_path);
|
||||
|
||||
let mut config_builder = config::Config::builder();
|
||||
|
||||
// Load config file if it exists
|
||||
if config_path.exists() {
|
||||
debug!("CONFIG: Loading config file: {:?}", config_path);
|
||||
config_builder = config_builder.add_source(config::File::from(config_path.clone()).required(false));
|
||||
} else {
|
||||
debug!("CONFIG: Config file does not exist: {:?}", config_path);
|
||||
}
|
||||
|
||||
// Add environment variables
|
||||
debug!("CONFIG: Adding environment variables");
|
||||
config_builder = config_builder.add_source(config::Environment::with_prefix("KEEP").separator("__"));
|
||||
|
||||
// Override with CLI args
|
||||
if let Some(dir) = &args.options.dir {
|
||||
debug!("CONFIG: Overriding dir with CLI arg: {:?}", dir);
|
||||
config_builder = config_builder.set_override("dir", dir.to_str().unwrap())?;
|
||||
}
|
||||
|
||||
@@ -148,10 +167,15 @@ impl Settings {
|
||||
}
|
||||
|
||||
let config = config_builder.build()?;
|
||||
let mut settings: Settings = config.try_deserialize()?;
|
||||
debug!("CONFIG: Built config, attempting to deserialize");
|
||||
|
||||
match config.try_deserialize::<Settings>() {
|
||||
Ok(mut settings) => {
|
||||
debug!("CONFIG: Successfully deserialized settings: {:?}", settings);
|
||||
|
||||
// Set defaults for list_format if not provided
|
||||
if settings.list_format.is_empty() {
|
||||
debug!("CONFIG: Setting default list_format");
|
||||
settings.list_format = vec![
|
||||
ColumnConfig { name: "id".to_string(), label: "Item".to_string() },
|
||||
ColumnConfig { name: "time".to_string(), label: "Time".to_string() },
|
||||
@@ -161,13 +185,21 @@ impl Settings {
|
||||
];
|
||||
}
|
||||
|
||||
// Set dir to default if not provided
|
||||
// Set dir to default if not provided or is empty
|
||||
if settings.dir == PathBuf::new() {
|
||||
debug!("CONFIG: Setting default dir: {:?}", default_dir);
|
||||
settings.dir = default_dir;
|
||||
}
|
||||
|
||||
debug!("CONFIG: Final settings: {:?}", settings);
|
||||
Ok(settings)
|
||||
}
|
||||
Err(e) => {
|
||||
error!("CONFIG: Failed to deserialize settings: {}", e);
|
||||
Err(e.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the default config file path
|
||||
pub fn default_config_path() -> Result<PathBuf> {
|
||||
|
||||
Reference in New Issue
Block a user