Skip to main content

mecha10_cli/services/dev/
mod.rs

1//! Development mode service
2//!
3//! Service for managing development mode operations including Redis flushing
4//! and environment variable setup.
5
6use anyhow::{Context, Result};
7use std::collections::HashMap;
8
9pub struct DevService {
10    #[allow(dead_code)]
11    redis_url: String,
12}
13
14impl DevService {
15    /// Create a new dev service from a Redis connection URL.
16    pub fn new(redis_url: String) -> Self {
17        Self { redis_url }
18    }
19
20    /// Load project information from context, returning (name, version, config).
21    pub async fn load_project_info(
22        ctx: &mut crate::context::CliContext,
23    ) -> Result<(String, String, crate::types::ProjectConfig)> {
24        let project = ctx.project()?;
25        let name = project.name()?.to_string();
26        let version = project.version()?.to_string();
27        let config = ctx.load_project_config().await?;
28        Ok((name, version, config))
29    }
30
31    /// Flush Redis database for a clean dev session.
32    pub async fn flush_redis<C>(&self, conn: &mut C) -> Result<()>
33    where
34        C: redis::aio::ConnectionLike,
35    {
36        redis::cmd("FLUSHALL")
37            .query_async::<()>(conn)
38            .await
39            .context("Failed to flush Redis")?;
40        Ok(())
41    }
42
43    /// Get default environment variables for dev mode.
44    #[allow(dead_code)]
45    pub fn get_default_env_vars(&self) -> HashMap<String, String> {
46        let mut env = HashMap::new();
47        env.insert("REDIS_URL".to_string(), self.redis_url.clone());
48        env.insert("RUST_LOG".to_string(), "info".to_string());
49        env
50    }
51}
52
53impl Default for DevService {
54    fn default() -> Self {
55        Self::new("redis://localhost:6379".to_string())
56    }
57}