Skip to main content

oxide_cli/addons/steps/
append.rs

1use std::path::Path;
2
3use anyhow::Result;
4
5use crate::addons::manifest::AppendStep;
6
7use super::{Rollback, render_lines, resolve_target};
8
9pub fn execute_append(
10  step: &AppendStep,
11  project_root: &Path,
12  ctx: &tera::Context,
13) -> Result<Vec<Rollback>> {
14  let paths = resolve_target(&step.target, project_root)?;
15  let lines: Vec<String> = step.content.lines().map(str::to_string).collect();
16  let rendered = render_lines(&lines, ctx)?.join("\n");
17
18  let mut rollbacks = Vec::new();
19
20  for path in paths {
21    let original = std::fs::read(&path)?;
22    let mut new_content = String::from_utf8_lossy(&original).to_string();
23
24    if !new_content.is_empty() && !new_content.ends_with('\n') {
25      new_content.push('\n');
26    }
27    new_content.push_str(&rendered);
28
29    rollbacks.push(Rollback::RestoreFile {
30      path: path.clone(),
31      original,
32    });
33    std::fs::write(&path, new_content)?;
34  }
35
36  Ok(rollbacks)
37}