use std::{env, path::PathBuf};

use anyhow::Context;
use bookyard_core::{BookyardConfig, CONFIG_FILE};

pub mod add;
pub mod build;
pub mod doctor;
pub mod init;
pub mod serve;

pub fn root_dir() -> anyhow::Result<PathBuf> {
    env::current_dir().context("failed to read current directory")
}

pub fn config_path() -> anyhow::Result<PathBuf> {
    Ok(root_dir()?.join(CONFIG_FILE))
}

pub fn load_config() -> anyhow::Result<BookyardConfig> {
    let path = config_path()?;
    BookyardConfig::load_from(&path).with_context(|| format!("failed to load {}", path.display()))
}
