epoch_forge/bin_efo/src/commands/init.rs

104 lines
3.1 KiB
Rust
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! # `init` module
//!
//! Provides the implementation of the `init` sub-command for the
//! command-line interface. The sub-command creates an empty
//! repository, writes the default configuration (`.epoch`),
//! and sets up the first epoch (epoch 0).
//!
//! The main public API consists of:
//!
//! * `InitCmd` - a struct containing the command-line arguments.
//!
//! ## Guide
//!
//! The `init` sub‑command bootstraps a new epoch repository:
//!
//! * It creates the default `.epoch` configuration file in the current directory.
//! * It creates an empty **epoch 0** folder (with its metadata file).
//! * It refuses to run if the target directory already contains files unless the user supplies `--force`.
//!
//! ### How to use
//!
//! ```bash
//! epoch init # normal init – directory must be empty
//! epoch init --force # skip the empty‑folder check
//! ```
//!
//! ### Table Entry
//!
//! | Sub‑command | Usage | What it changes |
//! |-------------|-------|-----------------|
//! | `init` | `epoch init [--force]` | Initialize the current folder as an epoch folder |
#![warn(missing_docs, clippy::missing_docs_in_private_items)]
use clap::Args;
use lib_efo::{
epoch::{EpochConfig, EpochInfo, epoch_traits::ConfigWritable},
error::EpochError,
};
use std::fs::read_dir;
use crate::{
error::AppError,
success::{SuccessMessages, display_success},
};
#[derive(Args, Debug)]
/// Arguments for the `init` subcommand.
///
/// The `--force` flag forces initialisation even when the target
/// directory already contains files.
pub struct InitCmd {
/// Force initialisation even if the directory isn't empty
#[arg(long)]
force: bool,
}
/// Initialise an empty repository.
///
/// Creates the default `EpochConfig`, verifies that the target directory
/// is empty, and writes the initial configuration and epoch data.
//TODO(5): Upholster the logic behind force and empty detection
pub fn init(args: &InitCmd) -> Result<(), AppError> {
let epoch_config = EpochConfig::default();
let root = epoch_config.save_folder();
if read_dir(&root)?.count() != 0 {
Err(EpochError::FolderNotEmpty {
name: root,
show_label: None,
})?;
}
init_epoch(args)?;
epoch_config
.save_with_context(
"Creatig initial config",
&lib_efo::epoch::SaveMode::DoNotErase,
)
.map_err(|_| EpochError::AlreadyInitialized {
advice: Some(
"You must remove the .epoch configuration file manually or pick another folder"
.to_string(),
),
})?;
display_success(&SuccessMessages::FolderInitializedSuccessfully("."));
Ok(())
}
/// Helper that creates the initial epoch (epoch 0).
///
/// This function is not part of the public API; it is kept internal
/// to `init`. It creates an empty `EpochInfo`, crea
fn init_epoch(_args: &InitCmd) -> Result<(), AppError> {
let empty = EpochInfo::default();
_ = empty.create_subfolders();
empty.save_with_context(
"Creating empty epoch file 0",
&lib_efo::epoch::SaveMode::DoNotErase,
)?;
Ok(())
}