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 rendered_path = super::render_string(&step.path, ctx)?;
16 let path = super::safe_join(project_root, &rendered_path, "create path")?;
17 let lines: Vec<String> = step.content.lines().map(str::to_string).collect();
18 let content = render_lines(&lines, ctx)?.join("\n");
19
20 let mut rollbacks = Vec::new();
21
22 if path.exists() {
23 match step.if_exists {
24 IfExists::Skip => return Ok(rollbacks),
25 IfExists::Ask => {
26 let overwrite = Confirm::new(&format!("{} already exists. Overwrite?", step.path))
27 .with_default(false)
28 .prompt()?;
29 if !overwrite {
30 return Ok(rollbacks);
31 }
32 rollbacks.push(Rollback::RestoreFile { path: path.clone(), original: std::fs::read(&path)? });
33 }
34 IfExists::Overwrite => {
35 rollbacks.push(Rollback::RestoreFile { path: path.clone(), original: std::fs::read(&path)? });
36 }
37 }
38 } else {
39 rollbacks.push(Rollback::DeleteCreatedFile { path: path.clone() });
40 }
41
42 if let Some(parent) = path.parent() {
43 std::fs::create_dir_all(parent)?;
44 }
45 std::fs::write(&path, content)?;
46
47 Ok(rollbacks)
48}