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 std::fs;
|
||||||
use anyhow::{Result, Context};
|
use anyhow::{Result, Context};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use log::debug;
|
use log::{debug, error};
|
||||||
use crate::args::{Args};
|
use crate::args::{Args};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
@@ -33,11 +33,15 @@ pub struct MetaPluginConfig {
|
|||||||
/// Unified settings that merges config file and CLI arguments
|
/// Unified settings that merges config file and CLI arguments
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
pub struct Settings {
|
pub struct Settings {
|
||||||
|
#[serde(default)]
|
||||||
pub dir: PathBuf,
|
pub dir: PathBuf,
|
||||||
pub list_format: Vec<ColumnConfig>,
|
pub list_format: Vec<ColumnConfig>,
|
||||||
|
#[serde(default)]
|
||||||
pub human_readable: bool,
|
pub human_readable: bool,
|
||||||
pub output_format: Option<String>,
|
pub output_format: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
pub quiet: bool,
|
pub quiet: bool,
|
||||||
|
#[serde(default)]
|
||||||
pub force: bool,
|
pub force: bool,
|
||||||
pub server: Option<ServerConfig>,
|
pub server: Option<ServerConfig>,
|
||||||
pub compression_plugin: Option<CompressionPluginConfig>,
|
pub compression_plugin: Option<CompressionPluginConfig>,
|
||||||
@@ -48,26 +52,41 @@ pub struct Settings {
|
|||||||
impl Settings {
|
impl Settings {
|
||||||
/// Create unified settings from config and args with proper priority
|
/// Create unified settings from config and args with proper priority
|
||||||
pub fn new(args: &Args, default_dir: PathBuf) -> Result<Self> {
|
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 {
|
let config_path = if let Some(config_path) = &args.options.config {
|
||||||
config_path.clone()
|
config_path.clone()
|
||||||
} else if let Ok(env_config) = std::env::var("KEEP_CONFIG") {
|
} else if let Ok(env_config) = std::env::var("KEEP_CONFIG") {
|
||||||
PathBuf::from(env_config)
|
PathBuf::from(env_config)
|
||||||
} else {
|
} 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();
|
let mut config_builder = config::Config::builder();
|
||||||
|
|
||||||
// Load config file if it exists
|
// Load config file if it exists
|
||||||
if config_path.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));
|
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
|
// Add environment variables
|
||||||
|
debug!("CONFIG: Adding environment variables");
|
||||||
config_builder = config_builder.add_source(config::Environment::with_prefix("KEEP").separator("__"));
|
config_builder = config_builder.add_source(config::Environment::with_prefix("KEEP").separator("__"));
|
||||||
|
|
||||||
// Override with CLI args
|
// Override with CLI args
|
||||||
if let Some(dir) = &args.options.dir {
|
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())?;
|
config_builder = config_builder.set_override("dir", dir.to_str().unwrap())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,25 +167,38 @@ impl Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let config = config_builder.build()?;
|
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
|
match config.try_deserialize::<Settings>() {
|
||||||
if settings.list_format.is_empty() {
|
Ok(mut settings) => {
|
||||||
settings.list_format = vec![
|
debug!("CONFIG: Successfully deserialized settings: {:?}", settings);
|
||||||
ColumnConfig { name: "id".to_string(), label: "Item".to_string() },
|
|
||||||
ColumnConfig { name: "time".to_string(), label: "Time".to_string() },
|
// Set defaults for list_format if not provided
|
||||||
ColumnConfig { name: "size".to_string(), label: "Size".to_string() },
|
if settings.list_format.is_empty() {
|
||||||
ColumnConfig { name: "meta:full_hostname".to_string(), label: "Host".to_string() },
|
debug!("CONFIG: Setting default list_format");
|
||||||
ColumnConfig { name: "meta:command".to_string(), label: "Command".to_string() },
|
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
|
/// Get the default config file path
|
||||||
|
|||||||
Reference in New Issue
Block a user