Skip to main content

hexz_cli/cmd/data/
init.rs

1//! Initialize a new Hexz workspace.
2
3use super::workspace::Workspace;
4use anyhow::{Context, Result};
5use colored::Colorize;
6use std::path::PathBuf;
7
8/// Initializes a new empty workspace.
9pub fn run(path: Option<PathBuf>) -> Result<()> {
10    let target_path =
11        path.unwrap_or(std::env::current_dir().context("Failed to get current directory")?);
12
13    if target_path.exists() && std::fs::read_dir(&target_path)?.next().is_some() {
14        let ws_config = target_path.join(".hexz");
15        if ws_config.exists() {
16            anyhow::bail!("Directory already contains a workspace.");
17        }
18        println!(
19            "{} Initializing workspace in existing directory {}",
20            "╭".dimmed(),
21            target_path.display().to_string().cyan()
22        );
23    } else {
24        std::fs::create_dir_all(&target_path)?;
25        println!(
26            "{} Initializing new workspace at {}",
27            "╭".dimmed(),
28            target_path.display().to_string().cyan()
29        );
30    }
31
32    let _ = Workspace::init(&target_path, None)?;
33    println!("{} Workspace initialized.", "╰".dimmed());
34    Ok(())
35}