Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions plugins/repos-validate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,26 @@ fn apply_sync(config_path: &PathBuf, sync_map: &HashMap<String, TopicSync>) -> 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::<Vec<_>>()
.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());
Expand Down
69 changes: 21 additions & 48 deletions src/config/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,36 @@ 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::<Vec<_>>()
.join("\n");

// Add document marker for yamllint compliance
let yaml_content = format!("---\n{}\n", fixed_yaml);

std::fs::write(path, yaml_content)?;

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<Repository> {
filters::filter_by_names(&self.repositories, names)
Expand Down Expand Up @@ -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();
Expand Down