epoch_forge/bin_efo/src/commands/init.rs

52 lines
1.4 KiB
Rust

use clap::Args;
use lib_efo::epoch_struct::{ConfigWritable, EpochInfo};
use lib_efo::{epoch_struct::EpochConfig, error::EpochError};
use std::fs::read_dir;
use crate::error::AppError;
#[derive(Args, Debug)]
pub struct InitCmd {
/// Force initialisation even if the directory isn't empty
#[arg(long)]
force: bool,
}
/// Init an empty repository
pub fn init(args: &InitCmd) -> Result<(), AppError> {
let epoch_config = EpochConfig::default();
let root = epoch_config.save_folder();
//TODO: This can be really deceiving when only .epoch is present
if read_dir(root)?.count() != 0 {
Err(EpochError::FolderNotEmpty {
name: None,
show_label: None,
})?;
}
init_epoch(args)?;
epoch_config
.save(
"Creatig initial config".into(),
lib_efo::epoch_struct::SaveMode::DoNotErase,
)
.map_err(|_| EpochError::AlreadyInitialized {
advice: Some(
"You must remove the .epoch configuration file manually or pick another folder"
.to_string(),
),
})?;
Ok(())
}
/// Tries to init the epoch 0
fn init_epoch(_args: &InitCmd) -> Result<(), AppError> {
let empty = EpochInfo::default();
_ = empty.create_subfolders();
empty.save(
"Creating empty epoch file 0".into(),
lib_efo::epoch_struct::SaveMode::DoNotErase,
)?;
Ok(())
}