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