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 {
33 path: path.clone(),
34 original: std::fs::read(&path)?,
35 });
36 }
37 IfExists::Overwrite => {
38 rollbacks.push(Rollback::RestoreFile {
39 path: path.clone(),
40 original: std::fs::read(&path)?,
41 });
42 }
43 }
44 } else {
45 rollbacks.push(Rollback::DeleteCreatedFile { path: path.clone() });
46 }
47
48 if let Some(parent) = path.parent() {
49 std::fs::create_dir_all(parent)?;
50 }
51 std::fs::write(&path, content)?;
52
53 Ok(rollbacks)
54}