From c470e63bacc84ec60c4910c5fef0e88911091c7f Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Sat, 16 Aug 2025 12:42:31 -0300 Subject: [PATCH] feat: add debug logging and make dir field optional in settings Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/config.rs | 70 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/src/config.rs b/src/config.rs index b56cdb6..302b772 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, + #[serde(default)] pub human_readable: bool, pub output_format: Option, + #[serde(default)] pub quiet: bool, + #[serde(default)] pub force: bool, pub server: Option, pub compression_plugin: Option, @@ -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 { + 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,25 +167,38 @@ impl Settings { } let config = config_builder.build()?; - let mut settings: Settings = config.try_deserialize()?; + debug!("CONFIG: Built config, attempting to deserialize"); - // Set defaults for list_format if not provided - if settings.list_format.is_empty() { - settings.list_format = vec![ - ColumnConfig { name: "id".to_string(), label: "Item".to_string() }, - ColumnConfig { name: "time".to_string(), label: "Time".to_string() }, - ColumnConfig { name: "size".to_string(), label: "Size".to_string() }, - ColumnConfig { name: "meta:full_hostname".to_string(), label: "Host".to_string() }, - ColumnConfig { name: "meta:command".to_string(), label: "Command".to_string() }, - ]; + match config.try_deserialize::() { + 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() }, + ColumnConfig { name: "size".to_string(), label: "Size".to_string() }, + ColumnConfig { name: "meta:full_hostname".to_string(), label: "Host".to_string() }, + ColumnConfig { name: "meta:command".to_string(), label: "Command".to_string() }, + ]; + } + + // 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()) + } } - - // Set dir to default if not provided - if settings.dir == PathBuf::new() { - settings.dir = default_dir; - } - - Ok(settings) } /// Get the default config file path