espforge_lib/examples/
fs.rs1use crate::cli::interactive::Prompter;
2use crate::cli::model::ExampleConfig;
3use anyhow::{Context, Result, bail};
4use std::fs;
5use std::path::{Path, PathBuf};
6
7pub struct OutputDirectory {
8 path: PathBuf,
9}
10
11impl OutputDirectory {
12 pub fn prepare(config: &ExampleConfig, prompter: &dyn Prompter) -> Result<Self> {
13 let path = Self::resolve_path(&config.project_name)?;
14
15 if path.exists() {
16 Self::handle_existing_directory(&path, &config.project_name, prompter)?;
17 } else {
18 Self::create_directory(&path)?;
19 }
20
21 Ok(Self { path })
22 }
23
24 fn resolve_path(project_name: &str) -> Result<PathBuf> {
25 let current_dir = std::env::current_dir().context("Failed to get current directory")?;
26 Ok(current_dir.join(project_name))
27 }
28
29 fn handle_existing_directory(
30 path: &Path,
31 project_name: &str,
32 prompter: &dyn Prompter,
33 ) -> Result<()> {
34 let overwrite = prompter.confirm_overwrite(project_name)?;
35 if !overwrite {
36 bail!("Operation cancelled by user");
37 }
38 fs::remove_dir_all(path).context("Failed to remove existing directory")?;
39 Ok(())
40 }
41
42 fn create_directory(path: &Path) -> Result<()> {
43 fs::create_dir_all(path).context("Failed to create output directory")
44 }
45
46 pub fn path(&self) -> &Path {
47 &self.path
48 }
49}