cuenv 0.40.6

Event-driven CLI with inline TUI for cuenv
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Formatter execution for post-sync formatting.
//!
//! Runs configured formatters on generated files after sync operations.

use cuenv_core::DryRun;
use cuenv_core::Result;
use cuenv_core::manifest::{Formatters, NixFormatter, NixFormatterTool, RustFormatter};
use glob::Pattern;
use std::path::Path;
use std::process::Command;
use tracing::{debug, info, warn};

/// Format files using configured formatters.
///
/// Takes a list of file paths that were generated and the formatters config.
/// Matches files against glob patterns and runs the appropriate formatter.
///
/// # Arguments
/// * `files` - List of file paths that were generated (should be absolute paths)
/// * `formatters` - The formatters configuration from the project
/// * `project_root` - Root path of the project (used for pattern matching and running formatters)
/// * `dry_run` - If true, only report what would be formatted
/// * `check` - If true, check formatting without making changes
///
/// # Pattern Matching
/// Patterns are matched against the relative path from `project_root`, allowing
/// patterns like `src/**/*.rs` or `tests/*.go` to work correctly. Invalid glob
/// patterns are logged as warnings and skipped.
///
/// # Returns
/// A string describing what was formatted.
///
/// # Errors
/// Returns an error if:
/// - In check mode and files are not properly formatted
/// - A formatter command fails to execute
pub fn format_generated_files(
    files: &[&Path],
    formatters: &Formatters,
    project_root: &Path,
    dry_run: DryRun,
    check: bool,
) -> Result<String> {
    let mut output_lines = Vec::new();

    // Collect files by formatter type
    let mut rust_files = Vec::new();
    let mut nix_files = Vec::new();
    let mut go_files = Vec::new();
    let mut cue_files = Vec::new();

    for file in files {
        // Use relative path from project root for pattern matching
        // This allows patterns like "src/**/*.rs" to work correctly
        let relative_path = file.strip_prefix(project_root).unwrap_or(file);
        let path_str = relative_path.to_string_lossy();

        // Check Rust formatter
        if let Some(ref rust) = formatters.rust
            && rust.enabled
            && matches_any_pattern(&path_str, &rust.includes)
        {
            rust_files.push(*file);
        }

        // Check Nix formatter
        if let Some(ref nix) = formatters.nix
            && nix.enabled
            && matches_any_pattern(&path_str, &nix.includes)
        {
            nix_files.push(*file);
        }

        // Check Go formatter
        if let Some(ref go) = formatters.go
            && go.enabled
            && matches_any_pattern(&path_str, &go.includes)
        {
            go_files.push(*file);
        }

        // Check CUE formatter
        if let Some(ref cue) = formatters.cue
            && cue.enabled
            && matches_any_pattern(&path_str, &cue.includes)
        {
            cue_files.push(*file);
        }
    }

    // Run formatters and collect results
    // In check mode, we want to report all failures, not just the first
    let mut errors = Vec::new();

    if !rust_files.is_empty() {
        match run_rust_formatter(
            &rust_files,
            formatters.rust.as_ref(),
            project_root,
            dry_run,
            check,
        ) {
            Ok(result) => output_lines.push(result),
            Err(e) => errors.push(e),
        }
    }

    if !nix_files.is_empty() {
        match run_nix_formatter(
            &nix_files,
            formatters.nix.as_ref(),
            project_root,
            dry_run,
            check,
        ) {
            Ok(result) => output_lines.push(result),
            Err(e) => errors.push(e),
        }
    }

    if !go_files.is_empty() {
        match run_go_formatter(&go_files, project_root, dry_run, check) {
            Ok(result) => output_lines.push(result),
            Err(e) => errors.push(e),
        }
    }

    if !cue_files.is_empty() {
        match run_cue_formatter(&cue_files, project_root, dry_run, check) {
            Ok(result) => output_lines.push(result),
            Err(e) => errors.push(e),
        }
    }

    // Return the first error if any occurred
    if let Some(first_error) = errors.into_iter().next() {
        return Err(first_error);
    }

    if output_lines.is_empty() {
        Ok(String::new())
    } else {
        Ok(output_lines.join("\n"))
    }
}

/// Check if a path matches any of the glob patterns.
///
/// Patterns are matched against the relative path from project root,
/// allowing patterns like `src/**/*.rs` or `tests/*.go` to work correctly.
pub fn matches_any_pattern(path: &str, patterns: &[String]) -> bool {
    for pattern_str in patterns {
        match Pattern::new(pattern_str) {
            Ok(pattern) => {
                if pattern.matches(path) {
                    return true;
                }
            }
            Err(e) => {
                warn!(
                    pattern = %pattern_str,
                    error = %e,
                    "Invalid glob pattern in formatter configuration; skipping"
                );
            }
        }
    }
    false
}

/// Run rustfmt on files.
///
/// # Errors
///
/// Returns an error if:
/// - In check mode and files are not properly formatted
/// - The formatter command fails to execute
pub fn run_rust_formatter(
    files: &[&Path],
    config: Option<&RustFormatter>,
    project_root: &Path,
    dry_run: DryRun,
    check: bool,
) -> Result<String> {
    if dry_run.is_dry_run() {
        return Ok(format!(
            "Would format {} Rust file(s) with rustfmt",
            files.len()
        ));
    }

    let mut cmd = Command::new("rustfmt");

    if check {
        cmd.arg("--check");
    }

    // Add edition if configured
    if let Some(cfg) = config
        && let Some(ref edition) = cfg.edition
    {
        cmd.arg("--edition").arg(edition);
    }

    // Add file paths
    for file in files {
        cmd.arg(file);
    }

    cmd.current_dir(project_root);

    debug!(?cmd, "Running rustfmt");

    match cmd.output() {
        Ok(output) => {
            if output.status.success() {
                info!(count = files.len(), "Formatted Rust files");
                Ok(format!("Formatted {} Rust file(s)", files.len()))
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                warn!(%stderr, "rustfmt reported issues");
                if check {
                    Err(cuenv_core::Error::configuration(format!(
                        "Rust formatting check failed: {stderr}"
                    )))
                } else {
                    Ok(format!(
                        "Formatted {} Rust file(s) (with warnings)",
                        files.len()
                    ))
                }
            }
        }
        Err(e) => {
            warn!(%e, "Failed to run rustfmt");
            Err(cuenv_core::Error::configuration(format!(
                "Failed to run rustfmt: {e}"
            )))
        }
    }
}

/// Run Nix formatter (nixfmt or alejandra) on files.
///
/// # Errors
///
/// Returns an error if:
/// - In check mode and files are not properly formatted
/// - The formatter command fails to execute
pub fn run_nix_formatter(
    files: &[&Path],
    config: Option<&NixFormatter>,
    project_root: &Path,
    dry_run: DryRun,
    check: bool,
) -> Result<String> {
    let tool = config.map_or(NixFormatterTool::default(), |c| c.tool);
    let tool_name = tool.command();

    if dry_run.is_dry_run() {
        return Ok(format!(
            "Would format {} Nix file(s) with {tool_name}",
            files.len()
        ));
    }

    let mut cmd = Command::new(tool_name);

    if check {
        cmd.arg(tool.check_flag());
    }

    // Add file paths
    for file in files {
        cmd.arg(file);
    }

    cmd.current_dir(project_root);

    debug!(?cmd, "Running Nix formatter");

    match cmd.output() {
        Ok(output) => {
            if output.status.success() {
                info!(count = files.len(), tool_name, "Formatted Nix files");
                Ok(format!(
                    "Formatted {} Nix file(s) with {tool_name}",
                    files.len()
                ))
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                warn!(%stderr, tool_name, "Nix formatter reported issues");
                if check {
                    Err(cuenv_core::Error::configuration(format!(
                        "Nix formatting check failed: {stderr}"
                    )))
                } else {
                    Ok(format!(
                        "Formatted {} Nix file(s) with {tool_name} (with warnings)",
                        files.len()
                    ))
                }
            }
        }
        Err(e) => {
            warn!(%e, tool_name, "Failed to run Nix formatter");
            Err(cuenv_core::Error::configuration(format!(
                "Failed to run {tool_name}: {e}"
            )))
        }
    }
}

/// Run gofmt on files.
///
/// # Errors
///
/// Returns an error if:
/// - In check mode and files are not properly formatted
/// - The formatter command fails to execute
pub fn run_go_formatter(
    files: &[&Path],
    project_root: &Path,
    dry_run: DryRun,
    check: bool,
) -> Result<String> {
    if dry_run.is_dry_run() {
        return Ok(format!(
            "Would format {} Go file(s) with gofmt",
            files.len()
        ));
    }

    let mut cmd = Command::new("gofmt");

    if check {
        cmd.arg("-l"); // List files that need formatting
    } else {
        cmd.arg("-w"); // Write result to (source) file
    }

    // Add file paths
    for file in files {
        cmd.arg(file);
    }

    cmd.current_dir(project_root);

    debug!(?cmd, "Running gofmt");

    match cmd.output() {
        Ok(output) => {
            if output.status.success() {
                if check {
                    let stdout = String::from_utf8_lossy(&output.stdout);
                    if stdout.trim().is_empty() {
                        Ok(format!(
                            "Go formatting check passed for {} file(s)",
                            files.len()
                        ))
                    } else {
                        Err(cuenv_core::Error::configuration(format!(
                            "Go formatting check failed - files need formatting:\n{stdout}"
                        )))
                    }
                } else {
                    info!(count = files.len(), "Formatted Go files");
                    Ok(format!("Formatted {} Go file(s)", files.len()))
                }
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                warn!(%stderr, "gofmt reported issues");
                Err(cuenv_core::Error::configuration(format!(
                    "gofmt failed: {stderr}"
                )))
            }
        }
        Err(e) => {
            warn!(%e, "Failed to run gofmt");
            Err(cuenv_core::Error::configuration(format!(
                "Failed to run gofmt: {e}"
            )))
        }
    }
}

/// Run cue fmt on files.
///
/// # Errors
///
/// Returns an error if:
/// - In check mode and files are not properly formatted
/// - The formatter command fails to execute
///
/// # Check Mode
///
/// Uses `cue fmt -d` to display diffs. If stdout is empty, formatting passes.
pub fn run_cue_formatter(
    files: &[&Path],
    project_root: &Path,
    dry_run: DryRun,
    check: bool,
) -> Result<String> {
    if dry_run.is_dry_run() {
        return Ok(format!(
            "Would format {} CUE file(s) with cue fmt",
            files.len()
        ));
    }

    let mut cmd = Command::new("cue");
    cmd.arg("fmt");

    // Use -d flag to show diffs (check mode) or format in-place
    if check {
        cmd.arg("-d");
    }

    // Add file paths
    for file in files {
        cmd.arg(file);
    }

    cmd.current_dir(project_root);

    debug!(?cmd, "Running cue fmt");

    match cmd.output() {
        Ok(output) => {
            if output.status.success() {
                if check {
                    // In check mode with -d, if stdout is empty, files are properly formatted
                    let stdout = String::from_utf8_lossy(&output.stdout);
                    if stdout.trim().is_empty() {
                        Ok(format!(
                            "CUE formatting check passed for {} file(s)",
                            files.len()
                        ))
                    } else {
                        Err(cuenv_core::Error::configuration(format!(
                            "CUE formatting check failed - files need formatting:\n{stdout}"
                        )))
                    }
                } else {
                    info!(count = files.len(), "Formatted CUE files");
                    Ok(format!("Formatted {} CUE file(s)", files.len()))
                }
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                warn!(%stderr, "cue fmt reported issues");
                Err(cuenv_core::Error::configuration(format!(
                    "cue fmt failed: {stderr}"
                )))
            }
        }
        Err(e) => {
            warn!(%e, "Failed to run cue fmt");
            Err(cuenv_core::Error::configuration(format!(
                "Failed to run cue fmt: {e}"
            )))
        }
    }
}

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

    // ============================================================================
    // Pattern Matching Tests
    // ============================================================================

    #[test]
    fn test_matches_any_pattern_simple() {
        assert!(matches_any_pattern("foo.rs", &["*.rs".to_string()]));
        assert!(matches_any_pattern("bar.nix", &["*.nix".to_string()]));
        assert!(!matches_any_pattern("foo.rs", &["*.go".to_string()]));
        assert!(matches_any_pattern(
            "foo.rs",
            &["*.go".to_string(), "*.rs".to_string()]
        ));
    }

    #[test]
    fn test_matches_any_pattern_with_directory() {
        // Relative paths should match directory patterns
        assert!(matches_any_pattern("src/lib.rs", &["src/*.rs".to_string()]));
        assert!(matches_any_pattern(
            "src/utils/helpers.rs",
            &["src/**/*.rs".to_string()]
        ));
        assert!(!matches_any_pattern(
            "tests/test.rs",
            &["src/**/*.rs".to_string()]
        ));
    }

    #[test]
    fn test_matches_any_pattern_nested_directories() {
        // Deep nested paths
        assert!(matches_any_pattern(
            "crates/core/src/lib.rs",
            &["**/*.rs".to_string()]
        ));
        assert!(matches_any_pattern(
            "packages/ui/components/Button.tsx",
            &["packages/**/*.tsx".to_string()]
        ));
    }

    #[test]
    fn test_matches_any_pattern_empty_patterns() {
        assert!(!matches_any_pattern("foo.rs", &[]));
    }

    #[test]
    fn test_matches_any_pattern_invalid_pattern_skipped() {
        // Invalid pattern should be skipped, valid one should still match
        assert!(matches_any_pattern(
            "foo.rs",
            &["[invalid".to_string(), "*.rs".to_string()]
        ));
        // Only invalid pattern - should not match
        assert!(!matches_any_pattern("foo.rs", &["[invalid".to_string()]));
    }

    #[test]
    fn test_matches_any_pattern_case_sensitive() {
        // Glob patterns are case-sensitive by default
        assert!(matches_any_pattern("Makefile", &["Makefile".to_string()]));
        assert!(!matches_any_pattern("makefile", &["Makefile".to_string()]));
    }

    // ============================================================================
    // NixFormatterTool Tests
    // ============================================================================

    #[test]
    fn test_nix_formatter_tool_command() {
        assert_eq!(NixFormatterTool::Nixfmt.command(), "nixfmt");
        assert_eq!(NixFormatterTool::Alejandra.command(), "alejandra");
    }

    #[test]
    fn test_nix_formatter_tool_check_flag() {
        assert_eq!(NixFormatterTool::Nixfmt.check_flag(), "--check");
        assert_eq!(NixFormatterTool::Alejandra.check_flag(), "-c");
    }

    #[test]
    fn test_nix_formatter_tool_default() {
        assert_eq!(NixFormatterTool::default(), NixFormatterTool::Nixfmt);
    }

    // ============================================================================
    // File Classification Tests
    // ============================================================================

    #[test]
    fn test_file_classification_rust() {
        let formatters = Formatters {
            rust: Some(RustFormatter {
                enabled: true,
                includes: vec!["*.rs".to_string()],
                edition: None,
            }),
            ..Default::default()
        };

        // Would need to test with actual file classification
        // This validates the patterns are set up correctly
        assert!(formatters.rust.as_ref().unwrap().enabled);
        assert_eq!(formatters.rust.as_ref().unwrap().includes, vec!["*.rs"]);
    }

    #[test]
    fn test_file_classification_disabled_formatter() {
        let formatters = Formatters {
            rust: Some(RustFormatter {
                enabled: false,
                includes: vec!["*.rs".to_string()],
                edition: None,
            }),
            ..Default::default()
        };

        assert!(!formatters.rust.as_ref().unwrap().enabled);
    }

    #[test]
    fn test_file_classification_with_directory_patterns() {
        let formatters = Formatters {
            rust: Some(RustFormatter {
                enabled: true,
                includes: vec!["src/**/*.rs".to_string(), "tests/**/*.rs".to_string()],
                edition: Some("2024".to_string()),
            }),
            ..Default::default()
        };

        let patterns = &formatters.rust.as_ref().unwrap().includes;

        // Files that should match
        assert!(matches_any_pattern("src/lib.rs", patterns));
        assert!(matches_any_pattern("src/utils/mod.rs", patterns));
        assert!(matches_any_pattern("tests/integration.rs", patterns));

        // Files that should not match
        assert!(!matches_any_pattern("build/output.rs", patterns));
        assert!(!matches_any_pattern("lib.rs", patterns)); // Not in src or tests
    }
}