oph 0.1.0

OpenHealth CLI - Universal development tool for the OpenHealth team
Documentation
use std::env;
use std::path::PathBuf;

/// Docker compose file paths
pub const DOCKER_DEV_DIR: &str = ".docker/development";
pub const DOCKER_PROD_DIR: &str = ".docker/selfhosted";

/// Database configuration
pub const DB_USER: &str = "openhealth";
pub const DB_NAME_DEV: &str = "openhealth_dev";
pub const DB_NAME_PROD: &str = "openhealth_prod";

/// Redis configuration
pub const REDIS_DEV_PASSWORD: &str = "redis_dev_secret";

/// Environment type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Environment {
    Dev,
    Prod,
}

impl Environment {
    pub fn docker_dir(&self) -> &'static str {
        match self {
            Environment::Dev => DOCKER_DEV_DIR,
            Environment::Prod => DOCKER_PROD_DIR,
        }
    }

    pub fn compose_file(&self) -> String {
        format!("{}/docker-compose.yml", self.docker_dir())
    }

    pub fn db_name(&self) -> &'static str {
        match self {
            Environment::Dev => DB_NAME_DEV,
            Environment::Prod => DB_NAME_PROD,
        }
    }

    pub fn label(&self) -> &'static str {
        match self {
            Environment::Dev => "development",
            Environment::Prod => "production",
        }
    }
}

impl std::fmt::Display for Environment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.label())
    }
}

/// Get the project root directory
pub fn project_root() -> anyhow::Result<PathBuf> {
    // Try to find the project root by looking for Makefile or .git
    let current_dir = env::current_dir()?;
    let mut dir = current_dir.as_path();

    loop {
        if dir.join("Makefile").exists() || dir.join(".git").exists() {
            return Ok(dir.to_path_buf());
        }

        match dir.parent() {
            Some(parent) => dir = parent,
            None => break,
        }
    }

    // Fallback to current directory
    Ok(current_dir)
}

/// Get the backups directory, creating it if it doesn't exist
pub fn backups_dir() -> anyhow::Result<PathBuf> {
    let root = project_root()?;
    let backups = root.join("backups");

    if !backups.exists() {
        std::fs::create_dir_all(&backups)?;
    }

    Ok(backups)
}