syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Background Process Manager
//!
//! Manages long-running background processes like `kubectl port-forward`.
//! Processes run asynchronously and can be started, stopped, and listed.
//!
//! # Example
//!
//! ```rust,ignore
//! use syncable_cli::agent::tools::background::BackgroundProcessManager;
//!
//! let manager = BackgroundProcessManager::new();
//!
//! // Start a port-forward in the background
//! let port = manager.start_port_forward(
//!     "prometheus",
//!     "svc/prometheus-server",
//!     "monitoring",
//!     9090
//! ).await?;
//!
//! println!("Port-forward running on localhost:{}", port);
//!
//! // Later, stop it
//! manager.stop("prometheus").await?;
//! ```

use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::Instant;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::sync::Mutex;

/// Error type for background process operations.
#[derive(Debug, thiserror::Error)]
pub enum BackgroundProcessError {
    #[error("Failed to spawn process: {0}")]
    SpawnFailed(String),

    #[error("Process not found: {0}")]
    NotFound(String),

    #[error("Failed to parse port from output: {0}")]
    PortParseFailed(String),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Process exited unexpectedly: {0}")]
    ProcessExited(String),
}

/// Information about a running background process.
#[derive(Debug, Clone)]
pub struct ProcessInfo {
    /// Unique identifier for this process
    pub id: String,
    /// The command that was executed
    pub command: String,
    /// When the process was started
    pub started_at: Instant,
    /// Local port (for port-forwards)
    pub local_port: Option<u16>,
    /// Whether the process is still running
    pub is_running: bool,
}

/// Internal state for a background process.
struct BackgroundProcess {
    id: String,
    command: String,
    started_at: Instant,
    local_port: Option<u16>,
    child: Child,
}

/// Manages background processes like port-forwards.
///
/// Thread-safe and designed to be shared across the agent session.
pub struct BackgroundProcessManager {
    processes: Arc<Mutex<HashMap<String, BackgroundProcess>>>,
}

impl Default for BackgroundProcessManager {
    fn default() -> Self {
        Self::new()
    }
}

impl BackgroundProcessManager {
    /// Create a new background process manager.
    pub fn new() -> Self {
        Self {
            processes: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Start a kubectl port-forward in the background.
    ///
    /// Returns the local port that was allocated.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this port-forward
    /// * `resource` - Kubernetes resource (e.g., "svc/prometheus-server" or "pod/prometheus-0")
    /// * `namespace` - Kubernetes namespace
    /// * `target_port` - The port on the remote resource
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let port = manager.start_port_forward(
    ///     "prometheus",
    ///     "svc/prometheus-server",
    ///     "monitoring",
    ///     9090
    /// ).await?;
    /// ```
    pub async fn start_port_forward(
        &self,
        id: &str,
        resource: &str,
        namespace: &str,
        target_port: u16,
    ) -> Result<u16, BackgroundProcessError> {
        // Check if already running
        {
            let processes = self.processes.lock().await;
            if processes.contains_key(id)
                && let Some(proc) = processes.get(id)
                && let Some(port) = proc.local_port
            {
                return Ok(port);
            }
        }

        // Build the port-forward command
        // Using :0 to let kubectl pick a random available port
        let port_spec = format!(":{}", target_port);
        let command = format!(
            "kubectl port-forward {} {} -n {}",
            resource, port_spec, namespace
        );

        // Spawn kubectl directly (not through sh) to avoid process hierarchy issues
        let mut child = Command::new("kubectl")
            .arg("port-forward")
            .arg(resource)
            .arg(&port_spec)
            .arg("-n")
            .arg(namespace)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| BackgroundProcessError::SpawnFailed(e.to_string()))?;

        // Take stderr for error capturing
        let stderr = child.stderr.take();

        // Read stdout to get the port
        // kubectl outputs: "Forwarding from 127.0.0.1:XXXXX -> 9090" to stdout
        let local_port = if let Some(stdout) = child.stdout.take() {
            let mut reader = BufReader::new(stdout).lines();
            let mut port = None;

            // Read lines with timeout
            let timeout = tokio::time::Duration::from_secs(10);
            let deadline = tokio::time::Instant::now() + timeout;

            while tokio::time::Instant::now() < deadline {
                match tokio::time::timeout(
                    deadline - tokio::time::Instant::now(),
                    reader.next_line(),
                )
                .await
                {
                    Ok(Ok(Some(line))) => {
                        // Parse port from "Forwarding from 127.0.0.1:XXXXX -> 9090"
                        if line.contains("Forwarding from")
                            && let Some(port_str) = line
                                .split(':')
                                .nth(1)
                                .and_then(|s| s.split_whitespace().next())
                        {
                            port = port_str.parse().ok();
                            // Keep draining stdout in background to prevent SIGPIPE
                            tokio::spawn(async move {
                                while let Ok(Some(_)) = reader.next_line().await {}
                            });
                            break;
                        }
                    }
                    Ok(Ok(None)) => break, // EOF
                    Ok(Err(e)) => {
                        return Err(BackgroundProcessError::IoError(e));
                    }
                    Err(_) => {
                        // Timeout - process may still be starting
                        break;
                    }
                }
            }

            port
        } else {
            None
        };

        // If we couldn't get the port, try to capture stderr for better error messages
        let local_port = match local_port {
            Some(p) => p,
            None => {
                // Try to read stderr for error messages
                let error_msg = if let Some(stderr) = stderr {
                    let mut reader = BufReader::new(stderr).lines();
                    let mut errors = Vec::new();
                    while let Ok(Ok(Some(line))) = tokio::time::timeout(
                        tokio::time::Duration::from_millis(100),
                        reader.next_line(),
                    )
                    .await
                    {
                        if !line.is_empty() {
                            errors.push(line);
                        }
                    }
                    if errors.is_empty() {
                        "Could not determine local port (no output from kubectl)".to_string()
                    } else {
                        errors.join("; ")
                    }
                } else {
                    "Could not determine local port".to_string()
                };
                return Err(BackgroundProcessError::PortParseFailed(error_msg));
            }
        };

        // Store the process
        let mut processes = self.processes.lock().await;
        processes.insert(
            id.to_string(),
            BackgroundProcess {
                id: id.to_string(),
                command,
                started_at: Instant::now(),
                local_port: Some(local_port),
                child,
            },
        );

        Ok(local_port)
    }

    /// Start a generic background command.
    ///
    /// # Arguments
    ///
    /// * `id` - Unique identifier for this process
    /// * `command` - The command to execute
    /// * `working_dir` - Working directory for the command
    pub async fn start(
        &self,
        id: &str,
        command: &str,
        working_dir: &Path,
    ) -> Result<(), BackgroundProcessError> {
        // Check if already running
        {
            let processes = self.processes.lock().await;
            if processes.contains_key(id) {
                return Ok(()); // Already running
            }
        }

        let child = Command::new("sh")
            .arg("-c")
            .arg(command)
            .current_dir(working_dir)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| BackgroundProcessError::SpawnFailed(e.to_string()))?;

        let mut processes = self.processes.lock().await;
        processes.insert(
            id.to_string(),
            BackgroundProcess {
                id: id.to_string(),
                command: command.to_string(),
                started_at: Instant::now(),
                local_port: None,
                child,
            },
        );

        Ok(())
    }

    /// Stop a background process by ID.
    pub async fn stop(&self, id: &str) -> Result<(), BackgroundProcessError> {
        let mut processes = self.processes.lock().await;
        if let Some(mut proc) = processes.remove(id) {
            // Try graceful shutdown first
            let _ = proc.child.kill().await;
        }
        Ok(())
    }

    /// Check if a process is running.
    pub async fn is_running(&self, id: &str) -> bool {
        let mut processes = self.processes.lock().await;
        if let Some(proc) = processes.get_mut(id) {
            // Check if process is still alive
            match proc.child.try_wait() {
                Ok(None) => true, // Still running
                Ok(Some(_)) => {
                    // Process exited, clean up
                    processes.remove(id);
                    false
                }
                Err(_) => false,
            }
        } else {
            false
        }
    }

    /// Get information about a specific process.
    pub async fn get(&self, id: &str) -> Option<ProcessInfo> {
        let mut processes = self.processes.lock().await;
        if let Some(proc) = processes.get_mut(id) {
            let is_running = proc
                .child
                .try_wait()
                .ok()
                .map(|s| s.is_none())
                .unwrap_or(false);
            Some(ProcessInfo {
                id: proc.id.clone(),
                command: proc.command.clone(),
                started_at: proc.started_at,
                local_port: proc.local_port,
                is_running,
            })
        } else {
            None
        }
    }

    /// List all background processes.
    pub async fn list(&self) -> Vec<ProcessInfo> {
        let mut processes = self.processes.lock().await;
        let mut infos = Vec::new();
        let mut to_remove = Vec::new();

        for (id, proc) in processes.iter_mut() {
            let is_running = proc
                .child
                .try_wait()
                .ok()
                .map(|s| s.is_none())
                .unwrap_or(false);
            if !is_running {
                to_remove.push(id.clone());
            }
            infos.push(ProcessInfo {
                id: proc.id.clone(),
                command: proc.command.clone(),
                started_at: proc.started_at,
                local_port: proc.local_port,
                is_running,
            });
        }

        // Clean up exited processes
        for id in to_remove {
            processes.remove(&id);
        }

        infos
    }

    /// Stop all background processes.
    pub async fn stop_all(&self) {
        let mut processes = self.processes.lock().await;
        for (_, mut proc) in processes.drain() {
            let _ = proc.child.kill().await;
        }
    }
}

impl Drop for BackgroundProcessManager {
    fn drop(&mut self) {
        // Note: We can't await here, so we use blocking
        // In practice, the manager should be stopped explicitly
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_manager() {
        let manager = BackgroundProcessManager::new();
        assert!(manager.processes.try_lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn test_list_empty() {
        let manager = BackgroundProcessManager::new();
        let list = manager.list().await;
        assert!(list.is_empty());
    }

    #[tokio::test]
    async fn test_is_running_not_found() {
        let manager = BackgroundProcessManager::new();
        assert!(!manager.is_running("nonexistent").await);
    }
}