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
16    ///
17    /// # Arguments
18    ///
19    /// * `redis_url` - Redis connection URL
20    pub fn new(redis_url: String) -> Self {
21        Self { redis_url }
22    }
23
24    /// Load project information from context
25    ///
26    /// # Returns
27    ///
28    /// Tuple of (project_name, project_version, project_config)
29    pub async fn load_project_info(
30        ctx: &mut crate::context::CliContext,
31    ) -> Result<(String, String, crate::types::ProjectConfig)> {
32        let project = ctx.project()?;
33        let name = project.name()?.to_string();
34        let version = project.version()?.to_string();
35        let config = ctx.load_project_config().await?;
36        Ok((name, version, config))
37    }
38
39    /// Flush Redis database for clean dev session
40    ///
41    /// # Arguments
42    ///
43    /// * `conn` - Mutable reference to Redis connection (generic type)
44    ///
45    /// # Errors
46    ///
47    /// Returns error if Redis FLUSHALL command fails
48    pub async fn flush_redis<C>(&self, conn: &mut C) -> Result<()>
49    where
50        C: redis::aio::ConnectionLike,
51    {
52        redis::cmd("FLUSHALL")
53            .query_async::<()>(conn)
54            .await
55            .context("Failed to flush Redis")?;
56        Ok(())
57    }
58
59    /// Get default environment variables for dev mode
60    ///
61    /// # Returns
62    ///
63    /// HashMap of environment variable key-value pairs
64    ///
65    /// Note: This method is no longer used by CLI (replaced by node-runner in Phase 2).
66    /// Kept for testing purposes.
67    #[allow(dead_code)]
68    pub fn get_default_env_vars(&self) -> HashMap<String, String> {
69        let mut env = HashMap::new();
70        env.insert("REDIS_URL".to_string(), self.redis_url.clone());
71        env.insert("RUST_LOG".to_string(), "info".to_string());
72        env
73    }
74}
75
76impl Default for DevService {
77    fn default() -> Self {
78        Self::new("redis://localhost:6379".to_string())
79    }
80}