feat: implement unified settings system
Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
103
src/main.rs
103
src/main.rs
@@ -29,7 +29,7 @@ extern crate serde_yaml;
|
||||
extern crate serde;
|
||||
|
||||
use args::{Args, NumberOrString};
|
||||
use config::Config;
|
||||
use config::{Config, Settings};
|
||||
|
||||
/**
|
||||
* Main function to handle command-line arguments and execute the appropriate mode.
|
||||
@@ -67,6 +67,16 @@ fn main() -> Result<(), Error> {
|
||||
|
||||
debug!("MAIN: Loaded config: {:?}", config);
|
||||
|
||||
// Determine default data directory
|
||||
let default_dir = match proj_dirs {
|
||||
Some(ref proj_dirs) => proj_dirs.data_dir().to_path_buf(),
|
||||
None => return Err(anyhow!("Unable to determine data directory")),
|
||||
};
|
||||
|
||||
// Create unified settings
|
||||
let settings = Settings::from_config_and_args(&config, &args, default_dir)?;
|
||||
debug!("MAIN: Unified settings: {:?}", settings);
|
||||
|
||||
let ids = &mut Vec::new();
|
||||
let tags = &mut Vec::new();
|
||||
|
||||
@@ -114,23 +124,9 @@ fn main() -> Result<(), Error> {
|
||||
mode = KeepModes::Status;
|
||||
} else if args.mode.server.is_some() {
|
||||
mode = KeepModes::Server;
|
||||
} else if config.server.is_some() && args.mode.server.is_none() {
|
||||
} else if settings.get_server_address(&args, &config).is_some() && args.mode.server.is_none() {
|
||||
// If server is configured in config file but not specified via CLI
|
||||
if let Some(server_config) = &config.server {
|
||||
let mut server_addr = String::new();
|
||||
if let Some(address) = &server_config.address {
|
||||
server_addr.push_str(address);
|
||||
} else {
|
||||
server_addr.push_str("127.0.0.1");
|
||||
}
|
||||
if let Some(port) = server_config.port {
|
||||
server_addr.push_str(&format!(":{}", port));
|
||||
} else {
|
||||
server_addr.push_str(":8080");
|
||||
}
|
||||
args.mode.server = Some(server_addr);
|
||||
mode = KeepModes::Server;
|
||||
}
|
||||
mode = KeepModes::Server;
|
||||
}
|
||||
|
||||
if mode == KeepModes::Unknown {
|
||||
@@ -142,7 +138,7 @@ fn main() -> Result<(), Error> {
|
||||
}
|
||||
|
||||
// Validate output format usage
|
||||
if let Some(output_format_str) = &args.options.output_format {
|
||||
if let Some(output_format_str) = &settings.output_format {
|
||||
if output_format_str != "table" && mode != KeepModes::Info && mode != KeepModes::Status && mode != KeepModes::List {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
@@ -152,7 +148,7 @@ fn main() -> Result<(), Error> {
|
||||
}
|
||||
|
||||
// Validate human-readable usage
|
||||
if args.options.human_readable && mode != KeepModes::List && mode != KeepModes::Info {
|
||||
if settings.human_readable && mode != KeepModes::List && mode != KeepModes::Info {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
"--human-readable can only be used with --list and --info modes"
|
||||
@@ -160,7 +156,7 @@ fn main() -> Result<(), Error> {
|
||||
}
|
||||
|
||||
// Validate server password usage
|
||||
if args.options.server_password.is_some() && mode != KeepModes::Server {
|
||||
if settings.server_password.is_some() && mode != KeepModes::Server {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
"--server-password can only be used with --server mode"
|
||||
@@ -172,58 +168,11 @@ fn main() -> Result<(), Error> {
|
||||
debug!("MAIN: tags: {:?}", tags);
|
||||
debug!("MAIN: mode: {:?}", mode);
|
||||
|
||||
// Apply configuration priority: CLI args > env vars > config file > defaults
|
||||
if args.options.dir.is_none() {
|
||||
if let Some(config_dir) = &config.dir {
|
||||
args.options.dir = Some(config_dir.clone());
|
||||
} else {
|
||||
match proj_dirs {
|
||||
Some(proj_dirs) => args.options.dir = Some(proj_dirs.data_dir().to_path_buf()),
|
||||
None => return Err(anyhow!("Unable to determine data directory")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply list_format from config if not set via CLI/env
|
||||
if args.options.list_format == "id,time,size,tags,meta:hostname" {
|
||||
if let Some(config_list_format) = &config.list_format {
|
||||
args.options.list_format = config_list_format.clone();
|
||||
}
|
||||
}
|
||||
|
||||
// Apply human_readable from config if not set via CLI
|
||||
if !args.options.human_readable {
|
||||
if let Some(config_human_readable) = config.human_readable {
|
||||
args.options.human_readable = config_human_readable;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply server password from config file if not set via CLI/env
|
||||
if args.options.server_password.is_none() {
|
||||
if let Ok(Some(password)) = config.get_server_password() {
|
||||
args.options.server_password = Some(password);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply compression from config if not set via CLI/env
|
||||
if args.item.compression.is_none() {
|
||||
if let Some(compression_plugin) = &config.compression_plugin {
|
||||
args.item.compression = Some(compression_plugin.name.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Apply meta_plugins from config if not set via CLI/env
|
||||
if args.item.meta_plugins.is_empty() {
|
||||
if let Some(meta_plugins) = &config.meta_plugins {
|
||||
args.item.meta_plugins = meta_plugins.iter().map(|p| p.name.clone()).collect();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
libc::umask(0o077);
|
||||
}
|
||||
|
||||
let data_path = args.options.dir.clone().unwrap();
|
||||
let data_path = settings.dir.clone();
|
||||
let mut db_path = data_path.clone();
|
||||
db_path.push("keep-1.db");
|
||||
|
||||
@@ -238,31 +187,31 @@ fn main() -> Result<(), Error> {
|
||||
|
||||
match mode {
|
||||
KeepModes::Save => {
|
||||
crate::modes::save::mode_save(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::save::mode_save(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Get => {
|
||||
crate::modes::get::mode_get(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::get::mode_get(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Diff => {
|
||||
crate::modes::diff::mode_diff(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::diff::mode_diff(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::List => {
|
||||
crate::modes::list::mode_list(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::list::mode_list(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Update => {
|
||||
crate::modes::update::mode_update(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::update::mode_update(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Info => {
|
||||
crate::modes::info::mode_info(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::info::mode_info(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Delete => {
|
||||
crate::modes::delete::mode_delete(&mut cmd, &args, ids, tags, &mut conn, data_path)?
|
||||
crate::modes::delete::mode_delete(&mut cmd, &settings, &config, ids, tags, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Status => {
|
||||
crate::modes::status::mode_status(&mut cmd, &args, data_path, db_path)?
|
||||
crate::modes::status::mode_status(&mut cmd, &settings, &config, data_path, db_path)?
|
||||
}
|
||||
KeepModes::Server => {
|
||||
crate::modes::server::mode_server(&mut cmd, &args, &mut conn, data_path)?
|
||||
crate::modes::server::mode_server(&mut cmd, &settings, &config, &mut conn, data_path)?
|
||||
}
|
||||
KeepModes::Unknown => todo!(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user