patchy/commands/
init.rs

1use std::{
2    fs::{self, File},
3    io::Write as _,
4};
5
6use colored::Colorize as _;
7
8use crate::{
9    confirm_prompt, git_commands::GIT_ROOT, success, types::CommandArgs, CONFIG_FILE, CONFIG_ROOT,
10    INDENT,
11};
12
13pub fn init(_args: &CommandArgs) -> anyhow::Result<()> {
14    let example_config = include_bytes!("../../example-config.toml");
15
16    let config_path = GIT_ROOT.join(CONFIG_ROOT);
17
18    let config_file_path = config_path.join(CONFIG_FILE);
19
20    if config_file_path.exists()
21        && !confirm_prompt!(
22            "File {} already exists. Overwrite it?",
23            config_file_path.to_string_lossy().bright_blue(),
24        )
25    {
26        anyhow::bail!("Did not overwrite {config_file_path:?}");
27    }
28
29    let _ = fs::create_dir_all(config_path);
30
31    let mut file = File::create(&config_file_path)?;
32
33    file.write_all(example_config)?;
34
35    success!("Created config file {config_file_path:?}");
36
37    Ok(())
38}