moadim 0.4.0

Moadim.io MCP/REST server for managing cron jobs
//! Path builders for the moadim jobs and handlers directory layout.

use std::path::PathBuf;

/// Returns the path to `~/.config/moadim/jobs/`.
pub fn jobs_dir() -> PathBuf {
    jobs_dir_from_home(dirs::home_dir())
}

/// Returns the jobs directory under `home`, or `.` if `home` is `None`.
pub(crate) fn jobs_dir_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("moadim")
        .join("jobs")
}

/// Returns the path to `~/.config/moadim/handlers/`.
pub fn handlers_dir() -> PathBuf {
    handlers_dir_from_home(dirs::home_dir())
}

/// Returns the handlers directory under `home`, or `.` if `home` is `None`.
pub(crate) fn handlers_dir_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("moadim")
        .join("handlers")
}

/// Returns the path to `{jobs_dir}/{id}/`.
pub fn job_dir(id: &str) -> PathBuf {
    jobs_dir().join(id)
}

/// Returns the path to `{jobs_dir}/{id}/job.toml`.
pub fn job_toml_path(id: &str) -> PathBuf {
    job_dir(id).join("job.toml")
}

/// Returns the path to `{jobs_dir}/{id}/job.local.toml`.
pub fn job_local_toml_path(id: &str) -> PathBuf {
    job_dir(id).join("job.local.toml")
}

/// Returns the path to `{jobs_dir}/{id}/.gitignore`.
pub fn job_gitignore_path(id: &str) -> PathBuf {
    job_dir(id).join(".gitignore")
}

/// Returns the path to `{jobs_dir}/{id}/job.local.log`.
pub fn job_log_path(id: &str) -> PathBuf {
    job_dir(id).join("job.local.log")
}

// ─── Routines ────────────────────────────────────────────────────────────────

/// Returns the path to `~/.config/moadim/routines/`.
pub fn routines_dir() -> PathBuf {
    routines_dir_from_home(dirs::home_dir())
}

/// Returns the routines directory under `home`, or `.` if `home` is `None`.
pub(crate) fn routines_dir_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("moadim")
        .join("routines")
}

/// Returns the path to `{routines_dir}/{id}/`.
pub fn routine_dir(id: &str) -> PathBuf {
    routines_dir().join(id)
}

/// Returns the path to `{routines_dir}/{id}/routine.toml`.
pub fn routine_toml_path(id: &str) -> PathBuf {
    routine_dir(id).join("routine.toml")
}

/// Returns the path to `{routines_dir}/{id}/prompt.txt`.
pub fn routine_prompt_path(id: &str) -> PathBuf {
    routine_dir(id).join("prompt.txt")
}

/// Returns the path to `{routines_dir}/{id}/.gitignore`.
pub fn routine_gitignore_path(id: &str) -> PathBuf {
    routine_dir(id).join(".gitignore")
}

/// Returns the path to `{routines_dir}/{id}/run.sh`, the generated launch script invoked by cron.
pub fn routine_script_path(id: &str) -> PathBuf {
    routine_dir(id).join("run.sh")
}

// ─── Agent registry ──────────────────────────────────────────────────────────

/// Returns the path to `~/.config/moadim/agents/`.
pub fn agents_dir() -> PathBuf {
    agents_dir_from_home(dirs::home_dir())
}

/// Returns the agents directory under `home`, or `.` if `home` is `None`.
pub(crate) fn agents_dir_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("moadim")
        .join("agents")
}

/// Returns the path to `~/.config/moadim/agents/{name}.toml`.
pub fn agent_toml_path(name: &str) -> PathBuf {
    agents_dir().join(format!("{name}.toml"))
}

// ─── Daemon runtime files ────────────────────────────────────────────────────

/// Returns the path to `~/.config/moadim/`.
pub fn config_dir() -> PathBuf {
    config_dir_from_home(dirs::home_dir())
}

/// Returns the moadim config directory under `home`, or `.` if `home` is `None`.
pub(crate) fn config_dir_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("moadim")
}

/// Returns the path to `~/.config/moadim/moadim.pid`, where the running server records its PID.
pub fn pid_file() -> PathBuf {
    config_dir().join("moadim.pid")
}

/// Returns the path to `~/.config/moadim/daemon.log`, where a backgrounded server writes its output.
pub fn daemon_log_file() -> PathBuf {
    config_dir().join("daemon.log")
}

// ─── Workbenches ─────────────────────────────────────────────────────────────

/// Returns the path to `~/.moadim/`.
pub fn moadim_home() -> PathBuf {
    moadim_home_from_home(dirs::home_dir())
}

/// Returns the moadim home directory under `home`, or `.` if `home` is `None`.
pub(crate) fn moadim_home_from_home(home: Option<PathBuf>) -> PathBuf {
    home.unwrap_or_else(|| PathBuf::from(".")).join(".moadim")
}

/// Returns the path to `~/.moadim/workbenches/`.
pub fn workbenches_dir() -> PathBuf {
    moadim_home().join("workbenches")
}

#[cfg(test)]
mod mod_tests;