wrkflw-ui 0.8.0

Terminal user interface for wrkflw workflow execution engine
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// Workflow handlers
use crate::cli_style;
use std::io;
use std::path::{Path, PathBuf};
use wrkflw_evaluator::evaluate_workflow_file;
use wrkflw_executor::{self, JobStatus, RuntimeType, StepStatus};
#[cfg(feature = "tui")]
use {
    crate::app::App,
    crate::models::{ExecutionResultMsg, WorkflowExecution, WorkflowStatus},
    chrono::Local,
    std::sync::mpsc,
    std::thread,
};

// Validate a workflow or directory containing workflows
pub fn validate_workflow(path: &Path, verbose: bool) -> io::Result<()> {
    let mut workflows = Vec::new();

    if path.is_dir() {
        let entries = std::fs::read_dir(path)?;

        for entry in entries {
            let entry = entry?;
            let entry_path = entry.path();

            if entry_path.is_file() && wrkflw_utils::is_workflow_file(&entry_path) {
                workflows.push(entry_path);
            }
        }
    } else if path.is_file() {
        workflows.push(PathBuf::from(path));
    } else {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            format!("Path does not exist: {}", path.display()),
        ));
    }

    let mut valid_count = 0;
    let mut invalid_count = 0;

    println!(
        "{}",
        cli_style::info(&format!(
            "Validating {} workflow file(s)...",
            workflows.len()
        ))
    );

    for workflow_path in workflows {
        match evaluate_workflow_file(&workflow_path, verbose) {
            Ok(result) => {
                if result.is_valid {
                    println!(
                        "{}",
                        cli_style::success(&format!(
                            "Valid: {}",
                            cli_style::dim(&workflow_path.display().to_string())
                        ))
                    );
                    valid_count += 1;
                } else {
                    println!(
                        "{}",
                        cli_style::error(&format!("Invalid: {}", workflow_path.display()))
                    );
                    for (i, issue) in result.issues.iter().enumerate() {
                        println!("{}", cli_style::indent(&format!("{}. {}", i + 1, issue)));
                    }
                    invalid_count += 1;
                }
            }
            Err(e) => {
                println!(
                    "{}",
                    cli_style::error(&format!(
                        "Error processing {}: {}",
                        workflow_path.display(),
                        e
                    ))
                );
                invalid_count += 1;
            }
        }
    }

    use colored::Colorize;
    println!(
        "\n{}",
        format!("Summary: {} valid, {} invalid", valid_count, invalid_count).bold()
    );

    Ok(())
}

// Execute a workflow through the CLI
pub async fn execute_workflow_cli(
    path: &Path,
    runtime_type: RuntimeType,
    verbose: bool,
    show_action_messages: bool,
) -> io::Result<()> {
    if !path.exists() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            format!("Workflow file does not exist: {}", path.display()),
        ));
    }

    println!("{}", cli_style::info("Validating workflow..."));
    match evaluate_workflow_file(path, false) {
        Ok(result) => {
            if !result.is_valid {
                println!(
                    "{}",
                    cli_style::error(&format!(
                        "Cannot execute invalid workflow: {}",
                        path.display()
                    ))
                );
                for (i, issue) in result.issues.iter().enumerate() {
                    println!("{}", cli_style::indent(&format!("{}. {}", i + 1, issue)));
                }
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "Workflow validation failed",
                ));
            }
        }
        Err(e) => {
            return Err(io::Error::other(format!(
                "Error validating workflow: {}",
                e
            )));
        }
    }

    // Check container runtime availability if container runtime is selected
    let runtime_type = match runtime_type {
        RuntimeType::Docker => {
            if !wrkflw_executor::docker::is_available() {
                println!(
                    "{}",
                    cli_style::warning("Docker is not available. Using emulation mode instead.")
                );
                wrkflw_logging::warning("Docker is not available. Using emulation mode instead.");
                RuntimeType::Emulation
            } else {
                RuntimeType::Docker
            }
        }
        RuntimeType::Podman => {
            if !wrkflw_executor::podman::is_available() {
                println!(
                    "{}",
                    cli_style::warning("Podman is not available. Using emulation mode instead.")
                );
                wrkflw_logging::warning("Podman is not available. Using emulation mode instead.");
                RuntimeType::Emulation
            } else {
                RuntimeType::Podman
            }
        }
        RuntimeType::SecureEmulation => RuntimeType::SecureEmulation,
        RuntimeType::Emulation => RuntimeType::Emulation,
    };

    println!(
        "{}",
        cli_style::key_value("Workflow", &path.display().to_string())
    );
    println!(
        "{}",
        cli_style::key_value("Runtime", &format!("{:?}", runtime_type))
    );

    // Log the start of the execution in debug mode with more details
    wrkflw_logging::debug(&format!(
        "Starting workflow execution: path={}, runtime={:?}, verbose={}",
        path.display(),
        runtime_type,
        verbose
    ));

    let config = wrkflw_executor::ExecutionConfig {
        runtime_type,
        verbose,
        preserve_containers_on_failure: false, // Default for this path
        secrets_config: None,                  // Use default secrets configuration
        show_action_messages,
        target_job: None,
    };

    match wrkflw_executor::execute_workflow(path, config).await {
        Ok(result) => {
            println!("{}", cli_style::section("Workflow execution results"));

            let mut any_job_failed = false;

            for job in &result.jobs {
                match job.status {
                    JobStatus::Success => println!("\n{}", cli_style::job_success(&job.name)),
                    JobStatus::Failure => {
                        println!("\n{}", cli_style::job_failure(&job.name));
                        any_job_failed = true;
                    }
                    JobStatus::Skipped => println!("\n{}", cli_style::job_skipped(&job.name)),
                }

                println!("{}", cli_style::separator());

                wrkflw_logging::debug(&format!("Job: {}, Status: {:?}", job.name, job.status));

                for step in job.steps.iter() {
                    match step.status {
                        StepStatus::Success => {
                            println!("{}", cli_style::step_success(&step.name));

                            let should_hide = std::env::var("WRKFLW_HIDE_ACTION_MESSAGES")
                                .map(|val| val == "true")
                                .unwrap_or(false)
                                && step.output.contains("Would execute GitHub action:");

                            if !should_hide
                                && !step.output.trim().is_empty()
                                && step.output.lines().count() <= 3
                            {
                                println!("{}", cli_style::indent(step.output.trim()));
                            }
                        }
                        StepStatus::Failure => {
                            println!("{}", cli_style::step_failure(&step.name));

                            if let Some(exit_code) = step
                                .output
                                .lines()
                                .find(|line| line.trim().starts_with("Exit code:"))
                                .map(|line| line.trim().to_string())
                            {
                                println!("{}", cli_style::indent(&exit_code));
                            }

                            if wrkflw_logging::get_log_level() <= wrkflw_logging::LogLevel::Debug {
                                if let Some(cmd_output) = step
                                    .output
                                    .lines()
                                    .skip_while(|l| !l.trim().starts_with("$"))
                                    .take(1)
                                    .next()
                                {
                                    println!(
                                        "{}",
                                        cli_style::indent(&format!(
                                            "Command: {}",
                                            cmd_output.trim()
                                        ))
                                    );
                                }
                            }

                            let output_lines: Vec<&str> = step
                                .output
                                .lines()
                                .filter(|line| !line.trim().starts_with("Exit code:"))
                                .collect();

                            if !output_lines.is_empty() {
                                println!("{}", cli_style::indent("Error output:"));
                                for line in output_lines.iter().take(10) {
                                    println!("{}", cli_style::indent(line.trim()));
                                }

                                if output_lines.len() > 10 {
                                    println!(
                                        "{}",
                                        cli_style::indent(&format!(
                                            "... (and {} more lines)",
                                            output_lines.len() - 10
                                        ))
                                    );
                                    println!(
                                        "{}",
                                        cli_style::indent("Use --debug to see full output")
                                    );
                                }
                            }
                        }
                        StepStatus::Skipped => {
                            println!("{}", cli_style::step_skipped(&step.name));
                        }
                    }

                    wrkflw_logging::debug(&format!(
                        "Step: {}, Status: {:?}, Output length: {} lines",
                        step.name,
                        step.status,
                        step.output.lines().count()
                    ));

                    if wrkflw_logging::get_log_level() == wrkflw_logging::LogLevel::Debug
                        && !step.output.trim().is_empty()
                    {
                        wrkflw_logging::debug(&format!(
                            "Step output for '{}': \n{}",
                            step.name, step.output
                        ));
                    }
                }
            }

            if any_job_failed {
                println!("\n{}", cli_style::error("Workflow completed with failures"));
                if wrkflw_logging::get_log_level() > wrkflw_logging::LogLevel::Debug {
                    println!(
                        "{}",
                        cli_style::indent("Run with --debug for more detailed output")
                    );
                }
            } else {
                println!(
                    "\n{}",
                    cli_style::success("Workflow completed successfully!")
                );
            }

            Ok(())
        }
        Err(e) => {
            println!(
                "{}",
                cli_style::error(&format!("Failed to execute workflow: {}", e))
            );
            wrkflw_logging::error(&format!("Failed to execute workflow: {}", e));
            Err(io::Error::other(e))
        }
    }
}

// Helper function to execute workflow trigger using curl
pub async fn execute_curl_trigger(
    workflow_name: &str,
    branch: Option<&str>,
) -> Result<(Vec<wrkflw_executor::JobResult>, ()), String> {
    // Get GitHub token
    let token = std::env::var("GITHUB_TOKEN").map_err(|_| {
        "GitHub token not found. Please set GITHUB_TOKEN environment variable".to_string()
    })?;

    // Debug log to check if GITHUB_TOKEN is set
    match std::env::var("GITHUB_TOKEN") {
        Ok(token) => wrkflw_logging::info(&format!("GITHUB_TOKEN is set: {}", &token[..5])), // Log first 5 characters for security
        Err(_) => wrkflw_logging::error("GITHUB_TOKEN is not set"),
    }

    // Get repository information
    let repo_info = wrkflw_github::get_repo_info()
        .map_err(|e| format!("Failed to get repository info: {}", e))?;

    // Determine branch to use
    let branch_ref = branch.unwrap_or(&repo_info.default_branch);

    // Extract just the workflow name from the path if it's a full path
    let workflow_name = if workflow_name.contains('/') {
        Path::new(workflow_name)
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| "Invalid workflow name".to_string())?
    } else {
        workflow_name
    };

    wrkflw_logging::info(&format!("Using workflow name: {}", workflow_name));

    // Construct JSON payload
    let payload = serde_json::json!({
        "ref": branch_ref
    });

    // Construct API URL
    let url = format!(
        "https://api.github.com/repos/{}/{}/actions/workflows/{}.yml/dispatches",
        repo_info.owner, repo_info.repo, workflow_name
    );

    wrkflw_logging::info(&format!("Triggering workflow at URL: {}", url));

    // Create a reqwest client
    let client = reqwest::Client::new();

    // Send the request using reqwest
    let response = client
        .post(&url)
        .header("Authorization", format!("Bearer {}", token.trim()))
        .header("Accept", "application/vnd.github.v3+json")
        .header("Content-Type", "application/json")
        .header("User-Agent", "wrkflw-cli")
        .json(&payload)
        .send()
        .await
        .map_err(|e| format!("Failed to send request: {}", e))?;

    if !response.status().is_success() {
        let status = response.status().as_u16();
        let error_message = response
            .text()
            .await
            .unwrap_or_else(|_| format!("Unknown error (HTTP {})", status));

        return Err(format!("API error: {} - {}", status, error_message));
    }

    // Success message with URL to view the workflow
    let success_msg = format!(
        "Workflow triggered successfully. View it at: https://github.com/{}/{}/actions/workflows/{}.yml",
        repo_info.owner, repo_info.repo, workflow_name
    );

    // Create a job result structure
    let job_result = wrkflw_executor::JobResult {
        name: "GitHub Trigger".to_string(),
        canonical_name: "GitHub Trigger".to_string(),
        status: wrkflw_executor::JobStatus::Success,
        steps: vec![wrkflw_executor::StepResult {
            name: "Remote Trigger".to_string(),
            status: wrkflw_executor::StepStatus::Success,
            output: success_msg,
            outcome: wrkflw_executor::StepStatus::Success,
            conclusion: wrkflw_executor::StepStatus::Success,
        }],
        logs: "Workflow triggered remotely on GitHub".to_string(),
        outputs: std::collections::HashMap::new(),
    };

    Ok((vec![job_result], ()))
}

// Extract common workflow execution logic to avoid duplication
#[cfg(feature = "tui")]
pub fn start_next_workflow_execution(
    app: &mut App,
    tx_clone: &mpsc::Sender<ExecutionResultMsg>,
    verbose: bool,
) {
    if let Some((next_idx, target_job)) = app.get_next_workflow_to_execute() {
        app.current_execution = Some(next_idx);
        let tx_clone_inner = tx_clone.clone();
        let workflow_path = app.workflows[next_idx].path.clone();

        // Log whether verbose mode is enabled
        if verbose {
            app.logs
                .push("Verbose mode: Step outputs will be displayed in full".to_string());
            wrkflw_logging::info("Verbose mode: Step outputs will be displayed in full");
        } else {
            app.logs.push(
                "Standard mode: Only step status will be shown (use --verbose for full output)"
                    .to_string(),
            );
            wrkflw_logging::info(
                "Standard mode: Only step status will be shown (use --verbose for full output)",
            );
        }

        // Check container runtime availability again if container runtime is selected
        let runtime_type = match app.runtime_type {
            RuntimeType::Docker => {
                // Use safe FD redirection to check Docker availability
                let is_docker_available = match wrkflw_utils::fd::with_stderr_to_null(
                    wrkflw_executor::docker::is_available,
                ) {
                    Ok(result) => result,
                    Err(_) => {
                        wrkflw_logging::debug(
                            "Failed to redirect stderr when checking Docker availability.",
                        );
                        false
                    }
                };

                if !is_docker_available {
                    app.logs
                        .push("Docker is not available. Using emulation mode instead.".to_string());
                    wrkflw_logging::warning(
                        "Docker is not available. Using emulation mode instead.",
                    );
                    RuntimeType::Emulation
                } else {
                    RuntimeType::Docker
                }
            }
            RuntimeType::Podman => {
                // Use safe FD redirection to check Podman availability
                let is_podman_available = match wrkflw_utils::fd::with_stderr_to_null(
                    wrkflw_executor::podman::is_available,
                ) {
                    Ok(result) => result,
                    Err(_) => {
                        wrkflw_logging::debug(
                            "Failed to redirect stderr when checking Podman availability.",
                        );
                        false
                    }
                };

                if !is_podman_available {
                    app.logs
                        .push("Podman is not available. Using emulation mode instead.".to_string());
                    wrkflw_logging::warning(
                        "Podman is not available. Using emulation mode instead.",
                    );
                    RuntimeType::Emulation
                } else {
                    RuntimeType::Podman
                }
            }
            RuntimeType::SecureEmulation => RuntimeType::SecureEmulation,
            RuntimeType::Emulation => RuntimeType::Emulation,
        };

        let validation_mode = app.validation_mode;
        let preserve_containers_on_failure = app.preserve_containers_on_failure;
        let show_action_messages = app.show_action_messages;

        // Update workflow status and add execution details
        app.workflows[next_idx].status = WorkflowStatus::Running;

        // Initialize execution details if not already done
        if app.workflows[next_idx].execution_details.is_none() {
            app.workflows[next_idx].execution_details = Some(WorkflowExecution {
                jobs: Vec::new(),
                start_time: Local::now(),
                end_time: None,
                logs: Vec::new(),
                progress: 0.0,
            });
        }

        thread::spawn(move || {
            let rt = match tokio::runtime::Runtime::new() {
                Ok(runtime) => runtime,
                Err(e) => {
                    let _ = tx_clone_inner.send((
                        next_idx,
                        Err(format!("Failed to create Tokio runtime: {}", e)),
                    ));
                    return;
                }
            };

            let result = rt.block_on(async {
                if validation_mode {
                    // Perform validation instead of execution
                    match evaluate_workflow_file(&workflow_path, verbose) {
                        Ok(validation_result) => {
                            // Create execution result based on validation
                            let status = if validation_result.is_valid {
                                wrkflw_executor::JobStatus::Success
                            } else {
                                wrkflw_executor::JobStatus::Failure
                            };

                            // Create a synthetic job result for validation
                            let jobs = vec![wrkflw_executor::JobResult {
                                name: "Validation".to_string(),
                                canonical_name: "Validation".to_string(),
                                status,
                                steps: vec![{
                                    let step_status = if validation_result.is_valid {
                                        wrkflw_executor::StepStatus::Success
                                    } else {
                                        wrkflw_executor::StepStatus::Failure
                                    };
                                    wrkflw_executor::StepResult {
                                        name: "Validator".to_string(),
                                        outcome: step_status,
                                        conclusion: step_status,
                                        status: step_status,
                                        output: validation_result.issues.join("\n"),
                                    }
                                }],
                                logs: format!(
                                    "Validation result: {}",
                                    if validation_result.is_valid {
                                        "PASSED"
                                    } else {
                                        "FAILED"
                                    }
                                ),
                                outputs: std::collections::HashMap::new(),
                            }];

                            Ok((jobs, ()))
                        }
                        Err(e) => Err(e.to_string()),
                    }
                } else {
                    // Use safe FD redirection for execution
                    let config = wrkflw_executor::ExecutionConfig {
                        runtime_type,
                        verbose,
                        preserve_containers_on_failure,
                        secrets_config: None, // Use default secrets configuration
                        show_action_messages,
                        target_job,
                    };

                    let execution_result = wrkflw_utils::fd::with_stderr_to_null(|| {
                        futures::executor::block_on(async {
                            wrkflw_executor::execute_workflow(&workflow_path, config).await
                        })
                    })
                    .map_err(|e| format!("Failed to redirect stderr during execution: {}", e))?;

                    match execution_result {
                        Ok(execution_result) => {
                            // Send back the job results in a wrapped result
                            Ok((execution_result.jobs, ()))
                        }
                        Err(e) => Err(e.to_string()),
                    }
                }
            });

            // Only send if we get a valid result
            if let Err(e) = tx_clone_inner.send((next_idx, result)) {
                wrkflw_logging::error(&format!("Error sending execution result: {}", e));
            }
        });
    } else {
        app.running = false;
        let timestamp = Local::now().format("%H:%M:%S").to_string();
        app.logs
            .push(format!("[{}] All workflows completed execution", timestamp));
        wrkflw_logging::info("All workflows completed execution");
    }
}