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
//! Task execution request types
//!
//! Defines structured types for task execution parameters, replacing
//! the 16-parameter function signature with a single request struct.

use std::fmt;
use std::path::PathBuf;

use crate::commands::CommandExecutor;
use cuenv_core::{DryRun, OutputCapture};

/// Request to execute a task or set of tasks.
///
/// This struct groups all parameters needed for task execution,
/// replacing the 16-parameter `execute_task` signature with a
/// single, structured request.
#[derive(Clone)]
pub struct TaskExecutionRequest<'a> {
    /// Path to the directory containing the CUE configuration
    pub path: String,

    /// CUE package name to evaluate
    pub package: String,

    /// How to select which task(s) to run
    pub selection: TaskSelection,

    /// Environment name to use (if any)
    pub environment: Option<String>,

    /// Output configuration
    pub output: OutputConfig,

    /// Execution mode (TUI vs simple)
    pub execution_mode: ExecutionMode,

    /// Backend to use for task execution (e.g., "dagger")
    pub backend: Option<String>,

    /// Skip executing task dependencies (for CI orchestrators that handle deps externally)
    pub skip_dependencies: bool,

    /// Dry run mode: export DAG as JSON without executing
    pub dry_run: DryRun,

    /// Executor for cached module evaluation (required - single CUE eval per process)
    pub executor: &'a CommandExecutor,
}

impl fmt::Debug for TaskExecutionRequest<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TaskExecutionRequest")
            .field("path", &self.path)
            .field("package", &self.package)
            .field("selection", &self.selection)
            .field("environment", &self.environment)
            .field("output", &self.output)
            .field("execution_mode", &self.execution_mode)
            .field("backend", &self.backend)
            .field("skip_dependencies", &self.skip_dependencies)
            .field("dry_run", &self.dry_run)
            .field("executor", &"<CommandExecutor>")
            .finish()
    }
}

/// How to select which task(s) to execute.
///
/// These variants are mutually exclusive - you can only use one
/// selection mode per execution request.
#[derive(Debug, Clone, Default)]
pub enum TaskSelection {
    /// Execute a specific named task with optional arguments.
    Named {
        /// The task name.
        name: String,
        /// Optional arguments to pass to the task.
        args: Vec<String>,
    },

    /// Execute all tasks matching the given labels (AND semantics)
    Labels(Vec<String>),

    /// List all available tasks (no execution)
    #[default]
    List,

    /// Interactively pick a task to run
    Interactive,
}

/// Configuration for output formatting and capture.
#[derive(Debug, Clone)]
pub struct OutputConfig {
    /// Output format: "simple", "json", "text", etc.
    pub format: String,

    /// Whether to capture stdout/stderr instead of streaming
    pub capture_output: OutputCapture,

    /// Whether to show cache paths in output
    pub show_cache_path: bool,

    /// Path to materialize task outputs to
    pub materialize_outputs: Option<PathBuf>,

    /// Show help for the task instead of executing
    pub help: bool,
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            format: "simple".to_string(),
            capture_output: OutputCapture::Capture,
            show_cache_path: false,
            materialize_outputs: None,
            help: false,
        }
    }
}

/// Execution mode for task running.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ExecutionMode {
    /// Standard execution with simple output
    #[default]
    Simple,

    /// Rich TUI mode with interactive display
    Tui,
}

impl<'a> TaskExecutionRequest<'a> {
    /// Create a new request for listing tasks.
    #[must_use]
    pub fn list(
        path: impl Into<String>,
        package: impl Into<String>,
        executor: &'a CommandExecutor,
    ) -> Self {
        Self {
            path: path.into(),
            package: package.into(),
            selection: TaskSelection::List,
            environment: None,
            output: OutputConfig::default(),
            execution_mode: ExecutionMode::default(),
            backend: None,
            skip_dependencies: false,
            dry_run: DryRun::No,
            executor,
        }
    }

    /// Create a new request for executing a named task.
    #[must_use]
    pub fn named(
        path: impl Into<String>,
        package: impl Into<String>,
        task_name: impl Into<String>,
        executor: &'a CommandExecutor,
    ) -> Self {
        Self {
            path: path.into(),
            package: package.into(),
            selection: TaskSelection::Named {
                name: task_name.into(),
                args: Vec::new(),
            },
            environment: None,
            output: OutputConfig::default(),
            execution_mode: ExecutionMode::default(),
            backend: None,
            skip_dependencies: false,
            dry_run: DryRun::No,
            executor,
        }
    }

    /// Create a new request for executing tasks matching labels.
    #[must_use]
    pub fn labels(
        path: impl Into<String>,
        package: impl Into<String>,
        labels: Vec<String>,
        executor: &'a CommandExecutor,
    ) -> Self {
        Self {
            path: path.into(),
            package: package.into(),
            selection: TaskSelection::Labels(labels),
            environment: None,
            output: OutputConfig::default(),
            execution_mode: ExecutionMode::default(),
            backend: None,
            skip_dependencies: false,
            dry_run: DryRun::No,
            executor,
        }
    }

    /// Create a new request for interactive task selection.
    #[must_use]
    pub fn interactive(
        path: impl Into<String>,
        package: impl Into<String>,
        executor: &'a CommandExecutor,
    ) -> Self {
        Self {
            path: path.into(),
            package: package.into(),
            selection: TaskSelection::Interactive,
            environment: None,
            output: OutputConfig::default(),
            execution_mode: ExecutionMode::default(),
            backend: None,
            skip_dependencies: false,
            dry_run: DryRun::No,
            executor,
        }
    }

    /// Set task arguments (for named task selection).
    #[must_use]
    pub fn with_args(mut self, args: Vec<String>) -> Self {
        if let TaskSelection::Named { name, .. } = self.selection {
            self.selection = TaskSelection::Named { name, args };
        }
        self
    }

    /// Set the environment name.
    #[must_use]
    pub fn with_environment(mut self, env: impl Into<String>) -> Self {
        self.environment = Some(env.into());
        self
    }

    /// Set the output format.
    #[must_use]
    pub fn with_format(mut self, format: impl Into<String>) -> Self {
        self.output.format = format.into();
        self
    }

    /// Enable output capture.
    #[must_use]
    pub const fn with_capture(mut self) -> Self {
        self.output.capture_output = OutputCapture::Capture;
        self
    }

    /// Enable TUI mode.
    #[must_use]
    pub const fn with_tui(mut self) -> Self {
        self.execution_mode = ExecutionMode::Tui;
        self
    }

    /// Set the backend.
    #[must_use]
    pub fn with_backend(mut self, backend: impl Into<String>) -> Self {
        self.backend = Some(backend.into());
        self
    }

    /// Enable help mode.
    #[must_use]
    pub const fn with_help(mut self) -> Self {
        self.output.help = true;
        self
    }

    /// Set materialize outputs path.
    #[must_use]
    pub fn with_materialize_outputs(mut self, path: impl Into<PathBuf>) -> Self {
        self.output.materialize_outputs = Some(path.into());
        self
    }

    /// Enable cache path display.
    #[must_use]
    pub const fn with_show_cache_path(mut self) -> Self {
        self.output.show_cache_path = true;
        self
    }

    /// Skip executing task dependencies.
    #[must_use]
    pub const fn with_skip_dependencies(mut self) -> Self {
        self.skip_dependencies = true;
        self
    }

    /// Enable dry run mode (export DAG as JSON without executing).
    #[must_use]
    pub const fn with_dry_run(mut self) -> Self {
        self.dry_run = DryRun::Yes;
        self
    }
}

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

    /// Create a test executor for unit tests.
    fn create_test_executor() -> CommandExecutor {
        let (sender, _receiver) = mpsc::unbounded_channel();
        CommandExecutor::new(sender, "cuenv".to_string())
    }

    #[test]
    fn test_request_list() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::list("./", "cuenv", &executor);
        assert_eq!(req.path, "./");
        assert_eq!(req.package, "cuenv");
        assert!(matches!(req.selection, TaskSelection::List));
    }

    #[test]
    fn test_request_named() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor);
        assert!(matches!(
            req.selection,
            TaskSelection::Named { ref name, .. } if name == "build"
        ));
    }

    #[test]
    fn test_request_builder_methods() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "test", &executor)
            .with_args(vec!["--verbose".to_string()])
            .with_environment("dev")
            .with_format("json")
            .with_capture()
            .with_tui();

        assert_eq!(req.environment, Some("dev".to_string()));
        assert_eq!(req.output.format, "json");
        assert!(req.output.capture_output.should_capture());
        assert_eq!(req.execution_mode, ExecutionMode::Tui);

        if let TaskSelection::Named { args, .. } = &req.selection {
            assert_eq!(args, &vec!["--verbose".to_string()]);
        } else {
            panic!("Expected Named selection");
        }
    }

    #[test]
    fn test_named_task_with_args() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor)
            .with_args(vec!["--release".to_string()])
            .with_environment("prod")
            .with_format("json")
            .with_capture();

        assert!(matches!(
            req.selection,
            TaskSelection::Named { ref name, ref args }
                if name == "build" && args == &vec!["--release".to_string()]
        ));
        assert_eq!(req.environment, Some("prod".to_string()));
        assert!(req.output.capture_output.should_capture());
    }

    #[test]
    fn test_tui_mode() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor).with_tui();

        assert_eq!(req.execution_mode, ExecutionMode::Tui);
    }

    #[test]
    fn test_skip_dependencies() {
        let executor = create_test_executor();
        let req =
            TaskExecutionRequest::named("./", "cuenv", "build", &executor).with_skip_dependencies();

        assert!(req.skip_dependencies);
    }

    #[test]
    fn test_request_labels() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::labels(
            "./",
            "cuenv",
            vec!["ci".to_string(), "fast".to_string()],
            &executor,
        );
        assert!(matches!(
            req.selection,
            TaskSelection::Labels(ref labels) if labels == &vec!["ci".to_string(), "fast".to_string()]
        ));
    }

    #[test]
    fn test_request_interactive() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::interactive("./", "cuenv", &executor);
        assert!(matches!(req.selection, TaskSelection::Interactive));
    }

    #[test]
    fn test_with_backend() {
        let executor = create_test_executor();
        let req =
            TaskExecutionRequest::named("./", "cuenv", "build", &executor).with_backend("dagger");
        assert_eq!(req.backend, Some("dagger".to_string()));
    }

    #[test]
    fn test_with_help() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor).with_help();
        assert!(req.output.help);
    }

    #[test]
    fn test_with_materialize_outputs() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor)
            .with_materialize_outputs("/tmp/outputs");
        assert_eq!(
            req.output.materialize_outputs,
            Some(PathBuf::from("/tmp/outputs"))
        );
    }

    #[test]
    fn test_with_show_cache_path() {
        let executor = create_test_executor();
        let req =
            TaskExecutionRequest::named("./", "cuenv", "build", &executor).with_show_cache_path();
        assert!(req.output.show_cache_path);
    }

    #[test]
    fn test_output_config_default() {
        let config = OutputConfig::default();
        assert_eq!(config.format, "simple");
        assert!(config.capture_output.should_capture());
        assert!(!config.show_cache_path);
        assert!(config.materialize_outputs.is_none());
        assert!(!config.help);
    }

    #[test]
    fn test_execution_mode_default() {
        let mode = ExecutionMode::default();
        assert_eq!(mode, ExecutionMode::Simple);
    }

    #[test]
    fn test_task_selection_default() {
        let selection = TaskSelection::default();
        assert!(matches!(selection, TaskSelection::List));
    }

    #[test]
    fn test_request_debug() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::list("./", "cuenv", &executor);
        let debug = format!("{req:?}");
        assert!(debug.contains("TaskExecutionRequest"));
        assert!(debug.contains("path"));
        assert!(debug.contains("package"));
    }

    #[test]
    fn test_request_clone() {
        let executor = create_test_executor();
        let req = TaskExecutionRequest::named("./", "cuenv", "build", &executor)
            .with_environment("dev")
            .with_format("json");
        let cloned = req.clone();
        assert_eq!(cloned.path, "./");
        assert_eq!(cloned.package, "cuenv");
        assert_eq!(cloned.environment, Some("dev".to_string()));
        assert_eq!(cloned.output.format, "json");
    }

    #[test]
    fn test_output_config_clone() {
        let config = OutputConfig {
            format: "json".to_string(),
            capture_output: OutputCapture::Capture,
            show_cache_path: true,
            materialize_outputs: Some(PathBuf::from("/tmp")),
            help: true,
        };
        let cloned = config.clone();
        assert_eq!(cloned.format, "json");
        assert!(cloned.capture_output.should_capture());
        assert!(cloned.show_cache_path);
    }

    #[test]
    fn test_execution_mode_clone() {
        let mode = ExecutionMode::Tui;
        let cloned = mode.clone();
        assert_eq!(cloned, ExecutionMode::Tui);
    }

    #[test]
    fn test_task_selection_clone() {
        let selection = TaskSelection::Named {
            name: "test".to_string(),
            args: vec!["arg1".to_string()],
        };
        let cloned = selection.clone();
        assert!(matches!(
            cloned,
            TaskSelection::Named { ref name, ref args }
                if name == "test" && args == &vec!["arg1".to_string()]
        ));
    }

    #[test]
    fn test_with_args_on_non_named_selection() {
        // with_args should be a no-op for non-Named selections
        let executor = create_test_executor();
        let req =
            TaskExecutionRequest::list("./", "cuenv", &executor).with_args(vec!["arg".to_string()]);
        assert!(matches!(req.selection, TaskSelection::List));
    }
}