Skip to main content

hexz_cli/cmd/data/
init.rs

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