From 0614b9dfbc03abc62933d1780a4cb06de26e5f08 Mon Sep 17 00:00:00 2001 From: codcod Date: Thu, 13 Nov 2025 12:18:26 +0100 Subject: [PATCH] fix: wrong indentation --- plugins/repos-validate/src/main.rs | 22 ++++++++-- src/config/loader.rs | 69 +++++++++--------------------- 2 files changed, 40 insertions(+), 51 deletions(-) diff --git a/plugins/repos-validate/src/main.rs b/plugins/repos-validate/src/main.rs index b9290be..933d970 100644 --- a/plugins/repos-validate/src/main.rs +++ b/plugins/repos-validate/src/main.rs @@ -269,10 +269,26 @@ fn apply_sync(config_path: &PathBuf, sync_map: &HashMap) -> R } // Write back to file - let updated_content = - serde_yaml::to_string(&config).context("Failed to serialize updated config")?; + let yaml = serde_yaml::to_string(&config).context("Failed to serialize updated config")?; + + // Minimal fix for yamllint: indent array items under 'repositories:' and 'recipes:' + let fixed_yaml = yaml + .lines() + .map(|line| { + // Indent array items and their properties + if line.starts_with("- ") || (line.starts_with(" ") && !line.starts_with(" ")) { + format!(" {}", line) + } else { + line.to_string() + } + }) + .collect::>() + .join("\n"); + + // Add document marker for yamllint compliance + let updated_content = format!("---\n{}\n", fixed_yaml); - fs::write(config_path, updated_content) + fs::write(config_path, &updated_content) .context(format!("Failed to write config file: {:?}", config_path))?; println!("{} Successfully updated config.yaml", "✅".green()); diff --git a/src/config/loader.rs b/src/config/loader.rs index 19fc816..51d8daf 100644 --- a/src/config/loader.rs +++ b/src/config/loader.rs @@ -44,13 +44,29 @@ impl Config { /// Save configuration to a file pub fn save(&self, path: &str) -> Result<()> { + // Use standard serde_yaml serialization let yaml = serde_yaml::to_string(self)?; - // Fix indentation for yamllint compliance - // yamllint expects array items to be indented relative to their parent - let fixed_yaml = Self::fix_yaml_indentation(&yaml); - - // Make yamllint compliant by adding document separator and ensuring newline at end + // Minimal fix for yamllint: indent array items under 'repositories:' and 'recipes:' + // This is the only formatting adjustment needed for yamllint compliance + let fixed_yaml = yaml + .lines() + .map(|line| { + // If line starts with "- " and previous non-empty line ends with ":" + // then it's an array item that needs indenting + if line.starts_with("- ") { + format!(" {}", line) + } else if line.starts_with(" ") && !line.starts_with(" ") { + // Lines with 1-2 spaces are properties of array items, need to be 4 spaces + format!(" {}", line) + } else { + line.to_string() + } + }) + .collect::>() + .join("\n"); + + // Add document marker for yamllint compliance let yaml_content = format!("---\n{}\n", fixed_yaml); std::fs::write(path, yaml_content)?; @@ -58,27 +74,6 @@ impl Config { Ok(()) } - /// Fix YAML indentation to be yamllint compliant - fn fix_yaml_indentation(yaml: &str) -> String { - let lines: Vec<&str> = yaml.lines().collect(); - let mut result = Vec::new(); - - for line in lines { - // If line starts with a dash (array item), indent it by 2 spaces - if line.starts_with("- ") { - result.push(format!(" {}", line)); - } else if line.starts_with(" ") && !line.starts_with(" ") { - // If line is already indented by 2 spaces but not 4, make it 4 spaces - // This handles the properties of array items - result.push(format!(" {}", line)); - } else { - result.push(line.to_string()); - } - } - - result.join("\n") - } - /// Filter repositories by specific names pub fn filter_by_names(&self, names: &[String]) -> Vec { filters::filter_by_names(&self.repositories, names) @@ -468,28 +463,6 @@ mod tests { assert_eq!(config.repositories.len(), 1); } - #[test] - fn test_fix_yaml_indentation() { - // Test basic array item indentation - let yaml = "repositories:\n- name: test\n url: test.git"; - let fixed = Config::fix_yaml_indentation(yaml); - assert!(fixed.contains(" - name: test")); - assert!(fixed.contains(" url: test.git")); - - // Test already correctly indented yaml - let yaml = "repositories:\n - name: test\n url: test.git"; - let fixed = Config::fix_yaml_indentation(yaml); - assert!(fixed.contains(" - name: test")); - assert!(fixed.contains(" url: test.git")); - - // Test lines with different indentation levels - let yaml = "key: value\narray:\n- item1\n subkey: subvalue"; - let fixed = Config::fix_yaml_indentation(yaml); - assert!(fixed.contains("key: value")); - assert!(fixed.contains(" - item1")); - assert!(fixed.contains(" subkey: subvalue")); - } - #[test] fn test_find_recipe() { let mut config = Config::new();