diff --git a/README.md b/README.md index 2aca194e..b1169462 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,7 @@ You can place a file called `yek.yaml` at your project root or pass a custom pat ### Configurable Options -Most CLI options can be configured in the config file. The following options can be set: +Most CLI options can be configured in the config file. Keys can be written in snake_case (`ignore_patterns`) or kebab-case (`ignore-patterns`). If a file sets both, the kebab-case key is used. The following options can be set: **File Processing:** - `max_size` - Size limit (same as `--max-size`) diff --git a/src/config.rs b/src/config.rs index 1287b813..d3f13dd7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -207,6 +207,7 @@ impl YekConfig { let (mut cfg, config_path, _config_format) = YekConfig::parse_info(); cfg.apply_config_bool_overrides(config_path.as_deref()); + cfg.apply_snake_case_config_keys(config_path.as_deref()); // Handle version flag if cfg.version { @@ -326,6 +327,55 @@ impl YekConfig { self.tree_only |= config_bool(&settings, "tree_only", "tree-only"); } + fn apply_snake_case_config_keys(&mut self, config_path: Option<&Path>) { + let Some(config_path) = config_path else { + return; + }; + + let Ok(settings) = ::config::Config::builder() + .add_source(::config::File::from(config_path).required(false)) + .build() + else { + return; + }; + + if let Some(patterns) = snake_case_value::>(&settings, "ignore_patterns") { + self.ignore_patterns.extend(patterns); + } + if let Some(patterns) = snake_case_value::>(&settings, "unignore_patterns") { + self.unignore_patterns.extend(patterns); + } + if let Some(rules) = snake_case_value(&settings, "priority_rules") { + self.priority_rules = rules; + } + if let Some(extensions) = snake_case_value(&settings, "binary_extensions") { + self.binary_extensions = extensions; + } + if let Some(boost) = snake_case_value(&settings, "git_boost_max") { + self.git_boost_max = Some(boost); + } + if let Some(depth) = snake_case_value(&settings, "max_git_depth") { + self.max_git_depth = depth; + } + if let Some(weights) = snake_case_value(&settings, "category_weights") { + self.category_weights = Some(weights); + } + if self.max_size == DEFAULT_MAX_SIZE { + if let Some(size) = snake_case_value(&settings, "max_size") { + self.max_size = size; + } + } + if self.output_dir.is_none() { + self.output_dir = snake_case_value(&settings, "output_dir"); + } + if self.output_name.is_none() { + self.output_name = snake_case_value(&settings, "output_name"); + } + if self.output_template.is_none() { + self.output_template = snake_case_value(&settings, "output_template"); + } + } + /// Compute a quick checksum for the input paths (files and directories). /// For directories, it uses the top-level listing. For files, it uses the file metadata. pub fn get_checksum(input_paths: &[String]) -> String { @@ -706,6 +756,19 @@ fn config_bool(settings: &::config::Config, snake_case_key: &str, kebab_case_key .unwrap_or(false) } +const DEFAULT_MAX_SIZE: &str = "10MB"; + +fn snake_case_value( + settings: &::config::Config, + snake_case_key: &str, +) -> Option { + let kebab_case_key = snake_case_key.replace('_', "-"); + if settings.get::<::config::Value>(&kebab_case_key).is_ok() { + return None; + } + settings.get::(snake_case_key).ok() +} + #[cfg(test)] mod bool_config_tests { use super::*; diff --git a/tests/main_test.rs b/tests/main_test.rs index 2d7ed8f4..be8055c7 100644 --- a/tests/main_test.rs +++ b/tests/main_test.rs @@ -556,3 +556,134 @@ fn test_main_help_includes_update_flag() { .stdout(predicate::str::contains("--update")) .stdout(predicate::str::contains("Update yek to the latest version")); } + +fn run_yek_in(dir: &std::path::Path, args: &[&str]) -> String { + let output = Command::cargo_bin("yek") + .expect("Binary 'yek' not found") + .current_dir(dir) + .arg(".") + .args(args) + .output() + .unwrap(); + assert!(output.status.success(), "yek failed: {:?}", output); + String::from_utf8(output.stdout).unwrap() +} + +fn write_sample_repo(dir: &std::path::Path) { + use std::fs; + + fs::create_dir_all(dir.join("docs")).unwrap(); + fs::create_dir_all(dir.join("src")).unwrap(); + fs::create_dir_all(dir.join("zzz")).unwrap(); + fs::write(dir.join("docs/guide.md"), "guide").unwrap(); + fs::write(dir.join("src/main.rs"), "fn main() {}").unwrap(); + fs::write(dir.join("zzz/notes.txt"), "notes").unwrap(); +} + +#[test] +fn test_main_with_snake_case_yaml_config() { + use std::fs; + use tempfile::tempdir; + + let temp_dir = tempdir().unwrap(); + write_sample_repo(temp_dir.path()); + fs::write( + temp_dir.path().join("yek.yaml"), + "ignore_patterns:\n - \"docs/**\"\npriority_rules:\n - score: 500\n pattern: \"^zzz/\"\noutput_template: \"@@ FILE_PATH\\nFILE_CONTENT\"\n", + ) + .unwrap(); + + let output = run_yek_in(temp_dir.path(), &[]); + + assert!( + !output.contains("docs/guide.md"), + "ignore_patterns not applied:\n{}", + output + ); + assert!( + output.contains("@@ src/main.rs"), + "output_template not applied:\n{}", + output + ); + let source = output.find("@@ src/main.rs").unwrap(); + let boosted = output.find("@@ zzz/notes.txt").unwrap(); + assert!(source < boosted, "priority_rules not applied:\n{}", output); +} + +#[test] +fn test_main_with_snake_case_toml_config() { + use std::fs; + use tempfile::tempdir; + + let temp_dir = tempdir().unwrap(); + write_sample_repo(temp_dir.path()); + fs::write( + temp_dir.path().join("yek.toml"), + "ignore_patterns = [\"docs/**\"]\n", + ) + .unwrap(); + + let output = run_yek_in(temp_dir.path(), &[]); + + assert!( + !output.contains("docs/guide.md"), + "ignore_patterns not applied:\n{}", + output + ); + assert!(output.contains("src/main.rs")); +} + +#[test] +fn test_main_kebab_case_config_key_wins_over_snake_case() { + use std::fs; + use tempfile::tempdir; + + let temp_dir = tempdir().unwrap(); + write_sample_repo(temp_dir.path()); + fs::write( + temp_dir.path().join("yek.yaml"), + "ignore_patterns:\n - \"docs/**\"\nignore-patterns:\n - \"zzz/**\"\n", + ) + .unwrap(); + + let output = run_yek_in(temp_dir.path(), &[]); + + assert!( + output.contains("docs/guide.md"), + "snake_case key should be ignored when kebab-case is set:\n{}", + output + ); + assert!( + !output.contains("zzz/notes.txt"), + "kebab-case key not applied:\n{}", + output + ); +} + +#[test] +fn test_main_cli_ignore_patterns_extend_snake_case_config() { + use std::fs; + use tempfile::tempdir; + + let temp_dir = tempdir().unwrap(); + write_sample_repo(temp_dir.path()); + fs::write( + temp_dir.path().join("yek.yaml"), + "ignore_patterns:\n - \"docs/**\"\n", + ) + .unwrap(); + + let output = run_yek_in(temp_dir.path(), &["--ignore-patterns", "zzz/**"]); + + assert!( + !output.contains("docs/guide.md"), + "config ignore_patterns not applied:\n{}", + output + ); + assert!( + !output.contains("zzz/notes.txt"), + "CLI ignore patterns not applied:\n{}", + output + ); + assert!(output.contains("src/main.rs")); +}