oxide_cli/addons/steps/
create.rs1use std::path::Path;
2
3use anyhow::Result;
4use inquire::Confirm;
5
6use crate::addons::manifest::{CreateStep, IfExists};
7
8use super::{Rollback, render_lines};
9
10pub fn execute_create(
11 step: &CreateStep,
12 project_root: &Path,
13 ctx: &tera::Context,
14) -> Result<Vec<Rollback>> {
15 let path = super::safe_join(project_root, &step.path, "create path")?;
16 let lines: Vec<String> = step.content.lines().map(str::to_string).collect();
17 let content = render_lines(&lines, ctx)?.join("\n");
18
19 let mut rollbacks = Vec::new();
20
21 if path.exists() {
22 match step.if_exists {
23 IfExists::Skip => return Ok(rollbacks),
24 IfExists::Ask => {
25 let overwrite = Confirm::new(&format!("{} already exists. Overwrite?", step.path))
26 .with_default(false)
27 .prompt()?;
28 if !overwrite {
29 return Ok(rollbacks);
30 }
31 rollbacks.push(Rollback::RestoreFile { path: path.clone(), original: std::fs::read(&path)? });
32 }
33 IfExists::Overwrite => {
34 rollbacks.push(Rollback::RestoreFile { path: path.clone(), original: std::fs::read(&path)? });
35 }
36 }
37 } else {
38 rollbacks.push(Rollback::DeleteCreatedFile { path: path.clone() });
39 }
40
41 if let Some(parent) = path.parent() {
42 std::fs::create_dir_all(parent)?;
43 }
44 std::fs::write(&path, content)?;
45
46 Ok(rollbacks)
47}