1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::tui::confirmation;
use simplelog::*;
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;

pub struct FileUtils {}

impl FileUtils {
    pub fn create_dir(dir_name: String, dir_path: String) -> Result<(), anyhow::Error> {
        if Path::new(&dir_path).exists() {
            info!("Tembo {} path exists", dir_name);
            return Ok(());
        }

        match fs::create_dir_all(dir_path) {
            Err(why) => panic!("Couldn't create {}: {}", dir_name, why),
            Ok(_) => info!("Tembo {} created", dir_name),
        };

        Ok(())
    }

    pub fn create_file(
        file_name: String,
        file_path: String,
        file_content: String,
        recreate: bool,
    ) -> Result<(), anyhow::Error> {
        let path = Path::new(&file_path);
        if !recreate && path.exists() {
            info!("Tembo {} file exists", file_name);
            return Ok(());
        }

        // Create all missing directories in the path
        let parent = path
            .parent()
            .ok_or_else(|| anyhow::anyhow!("Failed to get parent directory"))?;
        fs::create_dir_all(parent)?;

        let display = path.display();
        let mut file: File = match File::create(path) {
            Err(why) => panic!("Couldn't create {}: {}", display, why),
            Ok(file) => file,
        };
        info!("Tembo {} file created", file_name);

        if let Err(e) = file.write_all(file_content.as_bytes()) {
            panic!("Couldn't write to context file: {}", e);
        }
        Ok(())
    }

    pub fn save_tembo_toml(default_text: &str) -> Result<(), Box<dyn std::error::Error>> {
        let parent_dir = FileUtils::get_current_working_dir();
        let tembo_toml_path = Path::new(&parent_dir).join("tembo.toml");

        if tembo_toml_path.exists() {
            print!("Tembo.toml file already exists in this path");
            Ok(())
        } else {
            let mut file = File::create(&tembo_toml_path)?;
            file.write_all(default_text.as_bytes())?;
            confirmation("Tembo.toml initialized successfully!");

            Ok(())
        }
    }

    pub fn get_current_working_dir() -> String {
        env::current_dir().unwrap().to_str().unwrap().to_string()
    }
}