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
//! Format command implementation.
//!
//! Formats all files in a repository based on the formatters configuration
//! defined in `env.cue`. Uses the same formatter runners as the sync command
//! but discovers files by walking the directory tree.

use crate::commands::env_file::find_cue_module_root;
use crate::commands::sync::{
    matches_any_pattern, run_cue_formatter, run_go_formatter, run_nix_formatter, run_rust_formatter,
};
use crate::commands::{convert_engine_error, relative_path_from_root};
use cuengine::ModuleEvalOptions;
use cuenv_core::manifest::{Base, Formatters};
use cuenv_core::{ModuleEvaluation, Result};
use ignore::WalkBuilder;
use std::path::{Path, PathBuf};
use tracing::{debug, info};

/// Files discovered for each formatter type.
#[allow(clippy::struct_field_names)]
struct DiscoveredFiles {
    rust_files: Vec<PathBuf>,
    nix_files: Vec<PathBuf>,
    go_files: Vec<PathBuf>,
    cue_files: Vec<PathBuf>,
}

impl DiscoveredFiles {
    fn is_empty(&self) -> bool {
        self.rust_files.is_empty()
            && self.nix_files.is_empty()
            && self.go_files.is_empty()
            && self.cue_files.is_empty()
    }

    fn total_count(&self) -> usize {
        self.rust_files.len() + self.nix_files.len() + self.go_files.len() + self.cue_files.len()
    }
}

/// Check if a formatter should be included based on --only filter.
fn should_include(formatter_name: &str, only: Option<&[String]>) -> bool {
    match only {
        None => true,
        Some(list) => list.iter().any(|s| s.eq_ignore_ascii_case(formatter_name)),
    }
}

/// Discover all files matching formatter patterns.
///
/// Walks the project directory respecting .gitignore and matches files
/// against the configured formatter patterns.
fn discover_files(
    project_root: &Path,
    formatters: &Formatters,
    only: Option<&[String]>,
) -> DiscoveredFiles {
    let walker = WalkBuilder::new(project_root)
        .follow_links(true)
        .standard_filters(true) // Respects .gitignore
        .build();

    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 entry in walker.flatten() {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }

        let relative = path.strip_prefix(project_root).unwrap_or(path);
        let path_str = relative.to_string_lossy();

        // Check Rust formatter
        if should_include("rust", only)
            && let Some(ref rust) = formatters.rust
            && rust.enabled
            && matches_any_pattern(&path_str, &rust.includes)
        {
            rust_files.push(path.to_path_buf());
        }

        // Check Nix formatter
        if should_include("nix", only)
            && let Some(ref nix) = formatters.nix
            && nix.enabled
            && matches_any_pattern(&path_str, &nix.includes)
        {
            nix_files.push(path.to_path_buf());
        }

        // Check Go formatter
        if should_include("go", only)
            && let Some(ref go) = formatters.go
            && go.enabled
            && matches_any_pattern(&path_str, &go.includes)
        {
            go_files.push(path.to_path_buf());
        }

        // Check CUE formatter
        if should_include("cue", only)
            && let Some(ref cue) = formatters.cue
            && cue.enabled
            && matches_any_pattern(&path_str, &cue.includes)
        {
            cue_files.push(path.to_path_buf());
        }
    }

    debug!(
        rust = rust_files.len(),
        nix = nix_files.len(),
        go = go_files.len(),
        cue = cue_files.len(),
        "Discovered files for formatting"
    );

    DiscoveredFiles {
        rust_files,
        nix_files,
        go_files,
        cue_files,
    }
}

/// Load the Base configuration from the CUE module.
fn load_base_config(path: &str, package: &str) -> Result<Base> {
    let target_path = Path::new(path)
        .canonicalize()
        .map_err(|e| cuenv_core::Error::Io {
            source: e,
            path: Some(Path::new(path).to_path_buf().into_boxed_path()),
            operation: "canonicalize path".to_string(),
        })?;

    let module_root = find_cue_module_root(&target_path).ok_or_else(|| {
        cuenv_core::Error::configuration(format!(
            "No CUE module found (looking for cue.mod/) starting from: {}",
            target_path.display()
        ))
    })?;

    let options = ModuleEvalOptions {
        recursive: false,
        target_dir: Some(target_path.to_string_lossy().to_string()),
        ..Default::default()
    };
    let raw_result = cuengine::evaluate_module(&module_root, package, Some(&options))
        .map_err(convert_engine_error)?;

    let module = ModuleEvaluation::from_raw(
        module_root.clone(),
        raw_result.instances,
        raw_result.projects,
        None,
    );

    let relative_path = relative_path_from_root(&module_root, &target_path);
    let instance = module.get(&relative_path).ok_or_else(|| {
        cuenv_core::Error::configuration(format!(
            "No CUE instance found at path: {} (relative: {})",
            target_path.display(),
            relative_path.display()
        ))
    })?;

    instance.deserialize()
}

/// Execute the format command.
///
/// # Arguments
/// * `path` - Path to the CUE module directory
/// * `package` - CUE package name to evaluate
/// * `fix` - If true, apply formatting changes; if false, check mode (validate only)
/// * `only` - Optional list of formatters to run (e.g., `["rust", "go"]`)
///
/// # Returns
/// A string describing the formatting results.
///
/// # Errors
/// Returns an error if:
/// - No formatters are configured in env.cue
/// - In check mode and files need formatting
/// - A formatter command fails to execute
pub fn execute_fmt(
    path: &str,
    package: &str,
    fix: bool,
    only: Option<&[String]>,
) -> Result<String> {
    // Load config
    let config = load_base_config(path, package)?;

    // Check formatters exist
    let formatters = config.formatters.ok_or_else(|| {
        cuenv_core::Error::configuration(
            "No formatters configured in env.cue\n\n\
             Add a formatters block to your configuration:\n\n\
               formatters: {\n\
                   rust: {}\n\
                   nix: { tool: \"nixfmt\" }\n\
               }",
        )
    })?;

    // Discover files
    let project_root = Path::new(path)
        .canonicalize()
        .map_err(|e| cuenv_core::Error::Io {
            source: e,
            path: Some(Path::new(path).to_path_buf().into_boxed_path()),
            operation: "canonicalize path".to_string(),
        })?;

    let files = discover_files(&project_root, &formatters, only);

    if files.is_empty() {
        return Ok("No files found matching formatter patterns".to_string());
    }

    info!(
        total = files.total_count(),
        "Found files to {}",
        if fix { "format" } else { "check" }
    );

    // Run formatters (check = !fix)
    let check = !fix;
    let dry_run = cuenv_core::DryRun::No;
    let mut results = Vec::new();
    let mut errors = Vec::new();

    // Run Rust formatter
    if !files.rust_files.is_empty() {
        let file_refs: Vec<&Path> = files.rust_files.iter().map(AsRef::as_ref).collect();
        match run_rust_formatter(
            &file_refs,
            formatters.rust.as_ref(),
            &project_root,
            dry_run,
            check,
        ) {
            Ok(result) => results.push(result),
            Err(e) => errors.push(e),
        }
    }

    // Run Nix formatter
    if !files.nix_files.is_empty() {
        let file_refs: Vec<&Path> = files.nix_files.iter().map(AsRef::as_ref).collect();
        match run_nix_formatter(
            &file_refs,
            formatters.nix.as_ref(),
            &project_root,
            dry_run,
            check,
        ) {
            Ok(result) => results.push(result),
            Err(e) => errors.push(e),
        }
    }

    // Run Go formatter
    if !files.go_files.is_empty() {
        let file_refs: Vec<&Path> = files.go_files.iter().map(AsRef::as_ref).collect();
        match run_go_formatter(&file_refs, &project_root, dry_run, check) {
            Ok(result) => results.push(result),
            Err(e) => errors.push(e),
        }
    }

    // Run CUE formatter
    if !files.cue_files.is_empty() {
        let file_refs: Vec<&Path> = files.cue_files.iter().map(AsRef::as_ref).collect();
        match run_cue_formatter(&file_refs, &project_root, dry_run, check) {
            Ok(result) => results.push(result),
            Err(e) => errors.push(e),
        }
    }

    // Return first error if any occurred
    if let Some(first_error) = errors.into_iter().next() {
        // In check mode, add helpful message about --fix
        if check {
            let error_msg = format!("{first_error}");
            return Err(cuenv_core::Error::configuration(format!(
                "{error_msg}\n\nRun `cuenv fmt --fix` to apply changes."
            )));
        }
        return Err(first_error);
    }

    if results.is_empty() {
        Ok("No files needed formatting".to_string())
    } else {
        Ok(results.join("\n"))
    }
}

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

    #[test]
    fn test_should_include_no_filter() {
        assert!(should_include("rust", None));
        assert!(should_include("nix", None));
        assert!(should_include("go", None));
        assert!(should_include("cue", None));
    }

    #[test]
    fn test_should_include_with_filter() {
        let only = vec!["rust".to_string(), "go".to_string()];
        assert!(should_include("rust", Some(&only)));
        assert!(should_include("go", Some(&only)));
        assert!(!should_include("nix", Some(&only)));
        assert!(!should_include("cue", Some(&only)));
    }

    #[test]
    fn test_should_include_case_insensitive() {
        let only = vec!["RUST".to_string(), "Go".to_string()];
        assert!(should_include("rust", Some(&only)));
        assert!(should_include("Rust", Some(&only)));
        assert!(should_include("go", Some(&only)));
        assert!(should_include("GO", Some(&only)));
    }

    #[test]
    fn test_discovered_files_is_empty() {
        let empty = DiscoveredFiles {
            rust_files: vec![],
            nix_files: vec![],
            go_files: vec![],
            cue_files: vec![],
        };
        assert!(empty.is_empty());
        assert_eq!(empty.total_count(), 0);

        let non_empty = DiscoveredFiles {
            rust_files: vec![PathBuf::from("test.rs")],
            nix_files: vec![],
            go_files: vec![],
            cue_files: vec![],
        };
        assert!(!non_empty.is_empty());
        assert_eq!(non_empty.total_count(), 1);
    }

    #[test]
    fn test_discovered_files_total_count_all_types() {
        let files = DiscoveredFiles {
            rust_files: vec![PathBuf::from("a.rs"), PathBuf::from("b.rs")],
            nix_files: vec![PathBuf::from("flake.nix")],
            go_files: vec![PathBuf::from("main.go"), PathBuf::from("util.go")],
            cue_files: vec![PathBuf::from("env.cue")],
        };
        assert_eq!(files.total_count(), 6);
        assert!(!files.is_empty());
    }

    #[test]
    fn test_discovered_files_single_type_not_empty() {
        // Test each type individually
        let rust_only = DiscoveredFiles {
            rust_files: vec![PathBuf::from("lib.rs")],
            nix_files: vec![],
            go_files: vec![],
            cue_files: vec![],
        };
        assert!(!rust_only.is_empty());

        let nix_only = DiscoveredFiles {
            rust_files: vec![],
            nix_files: vec![PathBuf::from("shell.nix")],
            go_files: vec![],
            cue_files: vec![],
        };
        assert!(!nix_only.is_empty());

        let go_only = DiscoveredFiles {
            rust_files: vec![],
            nix_files: vec![],
            go_files: vec![PathBuf::from("main.go")],
            cue_files: vec![],
        };
        assert!(!go_only.is_empty());

        let cue_only = DiscoveredFiles {
            rust_files: vec![],
            nix_files: vec![],
            go_files: vec![],
            cue_files: vec![PathBuf::from("env.cue")],
        };
        assert!(!cue_only.is_empty());
    }

    #[test]
    fn test_should_include_empty_filter_list() {
        let empty: Vec<String> = vec![];
        // Empty list means nothing matches
        assert!(!should_include("rust", Some(&empty)));
        assert!(!should_include("nix", Some(&empty)));
    }

    #[test]
    fn test_should_include_single_formatter() {
        let only = vec!["cue".to_string()];
        assert!(!should_include("rust", Some(&only)));
        assert!(!should_include("nix", Some(&only)));
        assert!(!should_include("go", Some(&only)));
        assert!(should_include("cue", Some(&only)));
    }

    #[test]
    fn test_execute_fmt_invalid_path() {
        let result = execute_fmt("/nonexistent/path", "cuenv", false, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_execute_fmt_no_cue_module() {
        // Use temp dir without cue.mod
        let temp = std::env::temp_dir();
        let result = execute_fmt(temp.to_str().unwrap(), "cuenv", false, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_load_base_config_invalid_path() {
        let result = load_base_config("/nonexistent/path", "cuenv");
        assert!(result.is_err());
    }
}