mecha10-cli 0.8.0

Mecha10 CLI tool
Documentation
//! Development mode service
//!
//! Service for managing development mode operations including Redis flushing
//! and environment variable setup.

use anyhow::{Context, Result};
use std::collections::HashMap;

pub struct DevService {
    #[allow(dead_code)]
    redis_url: String,
}

impl DevService {
    /// Create a new dev service from a Redis connection URL.
    pub fn new(redis_url: String) -> Self {
        Self { redis_url }
    }

    /// Load project information from context, returning (name, version, config).
    pub async fn load_project_info(
        ctx: &mut crate::context::CliContext,
    ) -> Result<(String, String, crate::types::ProjectConfig)> {
        let project = ctx.project()?;
        let name = project.name()?.to_string();
        let version = project.version()?.to_string();
        let config = ctx.load_project_config().await?;
        Ok((name, version, config))
    }

    /// Flush Redis database for a clean dev session.
    pub async fn flush_redis<C>(&self, conn: &mut C) -> Result<()>
    where
        C: redis::aio::ConnectionLike,
    {
        redis::cmd("FLUSHALL")
            .query_async::<()>(conn)
            .await
            .context("Failed to flush Redis")?;
        Ok(())
    }

    /// Get default environment variables for dev mode.
    #[allow(dead_code)]
    pub fn get_default_env_vars(&self) -> HashMap<String, String> {
        let mut env = HashMap::new();
        env.insert("REDIS_URL".to_string(), self.redis_url.clone());
        env.insert("RUST_LOG".to_string(), "info".to_string());
        env
    }
}

impl Default for DevService {
    fn default() -> Self {
        Self::new("redis://localhost:6379".to_string())
    }
}