tldr-cli 0.1.3

CLI binary for TLDR code analysis tool
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Daemon notify command implementation
//!
//! CLI command: `tldr daemon notify FILE [--project PATH]`
//!
//! This module provides file change notifications to the daemon for:
//! - Cache invalidation
//! - Dirty file tracking
//! - Automatic re-indexing when threshold is reached
//!
//! # Security Mitigations
//!
//! - TIGER-P3-03: Validates file path is within project root
//! - TIGER-P3-05: Rate limiting handled in daemon (client just sends)
//!
//! # Use Case
//!
//! Editor hooks call this on file save to keep daemon cache fresh.

use std::path::PathBuf;

use clap::Args;
use serde::Serialize;

use crate::output::OutputFormat;

use super::error::{DaemonError, DaemonResult};
use super::ipc::send_command;
use super::types::{DaemonCommand, DaemonResponse};

// =============================================================================
// CLI Arguments
// =============================================================================

/// Arguments for the `daemon notify` command.
#[derive(Debug, Clone, Args)]
pub struct DaemonNotifyArgs {
    /// Path to the changed file
    pub file: PathBuf,

    /// Project root directory (default: current directory)
    #[arg(long, short = 'p', default_value = ".")]
    pub project: PathBuf,
}

// =============================================================================
// Output Types
// =============================================================================

/// Output structure for successful notify response.
#[derive(Debug, Clone, Serialize)]
pub struct DaemonNotifyOutput {
    /// Status (always "ok")
    pub status: String,
    /// Number of dirty files tracked
    pub dirty_count: usize,
    /// Threshold for triggering re-index
    pub threshold: usize,
    /// Whether re-index was triggered
    pub reindex_triggered: bool,
    /// Optional message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// Output structure for notify errors.
#[derive(Debug, Clone, Serialize)]
pub struct DaemonNotifyErrorOutput {
    /// Status (always "error")
    pub status: String,
    /// Error message
    pub error: String,
}

// =============================================================================
// Command Implementation
// =============================================================================

impl DaemonNotifyArgs {
    /// Run the daemon notify command.
    pub fn run(&self, format: OutputFormat, quiet: bool) -> anyhow::Result<()> {
        // Create a new tokio runtime for the async operations
        let runtime = tokio::runtime::Runtime::new()?;
        runtime.block_on(self.run_async(format, quiet))
    }

    /// Async implementation of the daemon notify command.
    async fn run_async(&self, format: OutputFormat, quiet: bool) -> anyhow::Result<()> {
        // Resolve project path to absolute
        let project = self.project.canonicalize().unwrap_or_else(|_| {
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(&self.project)
        });

        // Resolve file path to absolute
        let file = self.file.canonicalize().unwrap_or_else(|_| {
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(&self.file)
        });

        // TIGER-P3-03: Validate file path is within project root
        if !file.starts_with(&project) {
            let output = DaemonNotifyErrorOutput {
                status: "error".to_string(),
                error: format!(
                    "File '{}' is outside project root '{}'",
                    file.display(),
                    project.display()
                ),
            };

            if !quiet {
                match format {
                    OutputFormat::Json | OutputFormat::Compact => {
                        println!("{}", serde_json::to_string_pretty(&output)?);
                    }
                    OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                        eprintln!("Error: File '{}' is outside project root", file.display());
                    }
                }
            }

            return Err(anyhow::anyhow!("File is outside project root"));
        }

        // Build notify command
        let cmd = DaemonCommand::Notify { file: file.clone() };

        // Send to daemon
        match send_command(&project, &cmd).await {
            Ok(response) => self.handle_response(response, format, quiet),
            Err(DaemonError::NotRunning) | Err(DaemonError::ConnectionRefused) => {
                // Daemon not running - silently succeed
                // File edits should never fail due to daemon status
                if !quiet {
                    match format {
                        OutputFormat::Json | OutputFormat::Compact => {
                            let output = DaemonNotifyOutput {
                                status: "ok".to_string(),
                                dirty_count: 0,
                                threshold: 20,
                                reindex_triggered: false,
                                message: Some(
                                    "Daemon not running (notification ignored)".to_string(),
                                ),
                            };
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        }
                        OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                            // Silent - don't interrupt editor workflow
                        }
                    }
                }
                Ok(())
            }
            Err(e) => {
                // Other errors - also silently succeed
                // File edits should never fail due to daemon issues
                if !quiet {
                    match format {
                        OutputFormat::Json | OutputFormat::Compact => {
                            let output = DaemonNotifyOutput {
                                status: "ok".to_string(),
                                dirty_count: 0,
                                threshold: 20,
                                reindex_triggered: false,
                                message: Some(format!("Notification failed: {} (ignored)", e)),
                            };
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        }
                        OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                            // Silent - don't interrupt editor workflow
                        }
                    }
                }
                Ok(())
            }
        }
    }

    /// Handle the daemon response.
    fn handle_response(
        &self,
        response: DaemonResponse,
        format: OutputFormat,
        quiet: bool,
    ) -> anyhow::Result<()> {
        match response {
            DaemonResponse::NotifyResponse {
                status,
                dirty_count,
                threshold,
                reindex_triggered,
            } => {
                let output = DaemonNotifyOutput {
                    status,
                    dirty_count,
                    threshold,
                    reindex_triggered,
                    message: None,
                };

                if !quiet {
                    match format {
                        OutputFormat::Json | OutputFormat::Compact => {
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        }
                        OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                            if reindex_triggered {
                                println!("Reindex triggered ({}/{} files)", dirty_count, threshold);
                            } else {
                                println!("Tracked: {}/{} files", dirty_count, threshold);
                            }
                        }
                    }
                }

                Ok(())
            }
            DaemonResponse::Status { status, message } => {
                // Simple status response (probably "ok")
                let output = DaemonNotifyOutput {
                    status: status.clone(),
                    dirty_count: 0,
                    threshold: 20,
                    reindex_triggered: false,
                    message,
                };

                if !quiet {
                    match format {
                        OutputFormat::Json | OutputFormat::Compact => {
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        }
                        OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                            println!("Status: {}", status);
                        }
                    }
                }

                Ok(())
            }
            DaemonResponse::Error { error, .. } => {
                let output = DaemonNotifyErrorOutput {
                    status: "error".to_string(),
                    error: error.clone(),
                };

                if !quiet {
                    match format {
                        OutputFormat::Json | OutputFormat::Compact => {
                            println!("{}", serde_json::to_string_pretty(&output)?);
                        }
                        OutputFormat::Text | OutputFormat::Sarif | OutputFormat::Dot => {
                            eprintln!("Error: {}", error);
                        }
                    }
                }

                // Don't fail - file edits should work even with daemon errors
                Ok(())
            }
            _ => {
                // Unexpected response - treat as success
                Ok(())
            }
        }
    }
}

/// Send a notify command to the daemon (async version).
///
/// Convenience function that validates and sends the notification.
///
/// # Security
///
/// - TIGER-P3-03: Validates file path is within project root
pub async fn cmd_notify(args: DaemonNotifyArgs) -> DaemonResult<()> {
    // Resolve project path to absolute
    let project = args.project.canonicalize().unwrap_or_else(|_| {
        std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(&args.project)
    });

    // Resolve file path to absolute
    let file = args.file.canonicalize().unwrap_or_else(|_| {
        std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(&args.file)
    });

    // TIGER-P3-03: Validate file path is within project root
    if !file.starts_with(&project) {
        return Err(DaemonError::PermissionDenied { path: file });
    }

    // Build notify command
    let cmd = DaemonCommand::Notify { file };

    // Send to daemon
    let response = send_command(&project, &cmd).await?;

    // Print response
    match response {
        DaemonResponse::NotifyResponse {
            dirty_count,
            threshold,
            reindex_triggered,
            ..
        } => {
            if reindex_triggered {
                println!("Reindex triggered ({}/{} files)", dirty_count, threshold);
            } else {
                println!("Tracked: {}/{} files", dirty_count, threshold);
            }
            Ok(())
        }
        DaemonResponse::Error { error, .. } => {
            eprintln!("Error: {}", error);
            Ok(()) // Don't fail - file edits should work
        }
        _ => Ok(()),
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_daemon_notify_args_default() {
        let args = DaemonNotifyArgs {
            file: PathBuf::from("test.rs"),
            project: PathBuf::from("."),
        };

        assert_eq!(args.file, PathBuf::from("test.rs"));
        assert_eq!(args.project, PathBuf::from("."));
    }

    #[test]
    fn test_daemon_notify_args_with_project() {
        let args = DaemonNotifyArgs {
            file: PathBuf::from("/test/project/src/main.rs"),
            project: PathBuf::from("/test/project"),
        };

        assert_eq!(args.file, PathBuf::from("/test/project/src/main.rs"));
        assert_eq!(args.project, PathBuf::from("/test/project"));
    }

    #[test]
    fn test_daemon_notify_output_serialization() {
        let output = DaemonNotifyOutput {
            status: "ok".to_string(),
            dirty_count: 5,
            threshold: 20,
            reindex_triggered: false,
            message: None,
        };

        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("ok"));
        assert!(json.contains("5"));
        assert!(json.contains("20"));
        assert!(json.contains("false"));
    }

    #[test]
    fn test_daemon_notify_output_reindex_triggered() {
        let output = DaemonNotifyOutput {
            status: "ok".to_string(),
            dirty_count: 20,
            threshold: 20,
            reindex_triggered: true,
            message: None,
        };

        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("true"));
    }

    #[test]
    fn test_daemon_notify_error_output_serialization() {
        let output = DaemonNotifyErrorOutput {
            status: "error".to_string(),
            error: "File outside project root".to_string(),
        };

        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("error"));
        assert!(json.contains("File outside project root"));
    }

    #[tokio::test]
    async fn test_daemon_notify_file_outside_project() {
        let temp = TempDir::new().unwrap();
        let outside_file = TempDir::new().unwrap();
        let test_file = outside_file.path().join("outside.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let args = DaemonNotifyArgs {
            file: test_file.clone(),
            project: temp.path().to_path_buf(),
        };

        // Should fail because file is outside project
        let result = cmd_notify(args).await;
        assert!(result.is_err());
        assert!(matches!(result, Err(DaemonError::PermissionDenied { .. })));
    }

    #[tokio::test]
    async fn test_daemon_notify_file_inside_project() {
        let temp = TempDir::new().unwrap();
        let test_file = temp.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let args = DaemonNotifyArgs {
            file: test_file.clone(),
            project: temp.path().to_path_buf(),
        };

        // Should fail because daemon is not running (but path validation passed)
        let result = cmd_notify(args).await;
        // NotRunning error means path validation passed
        assert!(result.is_err());
        assert!(matches!(result, Err(DaemonError::NotRunning)));
    }

    #[tokio::test]
    async fn test_daemon_notify_silent_when_not_running() {
        let temp = TempDir::new().unwrap();
        let test_file = temp.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let args = DaemonNotifyArgs {
            file: test_file.clone(),
            project: temp.path().to_path_buf(),
        };

        // The run method should succeed even when daemon is not running
        let result = args.run_async(OutputFormat::Json, true).await;
        assert!(result.is_ok());
    }
}