patchy/commands/
init.rs

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
use std::{
    fs::{self, File},
    io::Write,
};

use colored::Colorize;

use crate::{
    confirm_prompt, git_commands::GIT_ROOT, success, types::CommandArgs, CONFIG_FILE, CONFIG_ROOT,
    INDENT,
};

pub fn init(_args: &CommandArgs) -> anyhow::Result<()> {
    let example_config = include_bytes!("../../example-config.toml");

    let config_path = GIT_ROOT.join(CONFIG_ROOT);

    let config_file_path = config_path.join(CONFIG_FILE);

    if config_file_path.exists()
        && !confirm_prompt!(
            "File {} already exists. Overwrite it?",
            config_file_path.to_string_lossy().bright_blue(),
        )
    {
        anyhow::bail!("Did not overwrite {config_file_path:?}");
    }

    let _ = fs::create_dir(config_path);

    let mut file = File::create(&config_file_path)?;

    file.write_all(example_config)?;

    success!("Created config file {config_file_path:?}");

    Ok(())
}