cargo_setupx/utils/
write_file.rs

1//! File writing utilities
2
3use crate::error::{Result, SetupError};
4use std::fs;
5use std::path::Path;
6
7/// Write a file, checking for existence first
8pub fn write_file(path: &Path, content: &str, force: bool) -> Result<()> {
9    if path.exists() && !force {
10        return Err(SetupError::FileExists {
11            path: path.to_path_buf(),
12        });
13    }
14
15    // Create parent directories if they don't exist
16    if let Some(parent) = path.parent() {
17        fs::create_dir_all(parent)?;
18    }
19
20    fs::write(path, content)?;
21
22    let status = if path.exists() && force {
23        "✏️  Overwrote"
24    } else {
25        "✅ Created"
26    };
27
28    println!("{} {}", status, path.display());
29    Ok(())
30}