Skip to content

Commit f28e32f

Browse files
authored
Merge pull request #37 from codcod/23-make-configyaml-compliant-with-yamllint
fix: make config.yaml yamllint compliant
2 parents d67d350 + be96a9c commit f28e32f

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/config/loader.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,39 @@ impl Config {
3535
pub fn save(&self, path: &str) -> Result<()> {
3636
let yaml = serde_yaml::to_string(self)?;
3737

38-
std::fs::write(path, yaml)?;
38+
// Fix indentation for yamllint compliance
39+
// yamllint expects array items to be indented relative to their parent
40+
let fixed_yaml = Self::fix_yaml_indentation(&yaml);
41+
42+
// Make yamllint compliant by adding document separator and ensuring newline at end
43+
let yaml_content = format!("---\n{}\n", fixed_yaml);
44+
45+
std::fs::write(path, yaml_content)?;
3946

4047
Ok(())
4148
}
4249

50+
/// Fix YAML indentation to be yamllint compliant
51+
fn fix_yaml_indentation(yaml: &str) -> String {
52+
let lines: Vec<&str> = yaml.lines().collect();
53+
let mut result = Vec::new();
54+
55+
for line in lines {
56+
// If line starts with a dash (array item), indent it by 2 spaces
57+
if line.starts_with("- ") {
58+
result.push(format!(" {}", line));
59+
} else if line.starts_with(" ") && !line.starts_with(" ") {
60+
// If line is already indented by 2 spaces but not 4, make it 4 spaces
61+
// This handles the properties of array items
62+
result.push(format!(" {}", line));
63+
} else {
64+
result.push(line.to_string());
65+
}
66+
}
67+
68+
result.join("\n")
69+
}
70+
4371
/// Filter repositories by specific names
4472
pub fn filter_by_names(&self, names: &[String]) -> Vec<Repository> {
4573
if names.is_empty() {

0 commit comments

Comments
 (0)