waterui-cli 0.4.0

Cross-platform tooling for WaterUI applications
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
//! `water doctor` command implementation.

use std::future::Future;

use clap::Args as ClapArgs;
use dialoguer::{Confirm, theme::ColorfulTheme};
use eyre::Result;

use crate::shell::Shell;
use crate::{error, header, line, note, success, warn};
use waterui_cli::toolchain::{
    Host,
    doctor::{CheckStatus, DoctorItem, DoctorItemRecord, doctor},
};

/// Arguments for the doctor command.
#[derive(ClapArgs, Debug)]
pub struct Args {
    /// Attempt to fix issues automatically.
    #[arg(long)]
    fix: bool,
}

const MAX_AUTO_FIX_PASSES: usize = 3;

struct DoctorSummary {
    all_ok: bool,
    fixable_items: Vec<DoctorItem>,
}

fn emit_item(shell: &Shell, item: &DoctorItem) {
    if shell.is_json() {
        if let Ok(json) = serde_json::to_string(&DoctorItemRecord::from(item)) {
            let _ = shell.json_raw(&json);
        }
        return;
    }

    match item.status {
        CheckStatus::Ok => success!(shell, "{}", item.name),
        CheckStatus::Missing => print_missing_item(shell, item),
        CheckStatus::Skipped => print_skipped_item(shell, item),
    }
}

fn print_missing_item(shell: &Shell, item: &DoctorItem) {
    let is_fixable = item.is_fixable();
    if let Some(message) = &item.message {
        if is_fixable {
            warn!(shell, "{} ({message}) [fixable]", item.name);
        } else {
            warn!(shell, "{} ({message}) [manual]", item.name);
        }
    } else if is_fixable {
        warn!(shell, "{} [fixable]", item.name);
    } else {
        warn!(shell, "{} [manual]", item.name);
    }
}

/// Install every fixable item; returns how many installations failed.
///
/// A skipped or failed install leaves the item missing, which the
/// re-diagnosis pass observes — the count exists only so the loop can stop
/// retrying when fixes themselves are failing.
async fn install_fixable_items(shell: &Shell, items: Vec<DoctorItem>) -> usize {
    let mut failures = 0usize;
    for item in items {
        let name = item.name;
        if let Some(install_fn) = item.install_fn {
            let should_install = if shell.is_interactive() {
                Confirm::with_theme(&ColorfulTheme::default())
                    .with_prompt(format!("Install {name}?"))
                    .default(true)
                    .interact()
                    .unwrap_or(false)
            } else {
                true
            };

            if !should_install {
                note!(shell, "Skipped installation for {name}");
                continue;
            }

            let spinner = shell.spinner(format!("Installing {name}..."));
            let result = install_fn().await;
            if let Some(pb) = spinner {
                pb.finish_and_clear();
            }

            match result {
                Ok(()) => success!(shell, "Installed {name}"),
                Err(e) => {
                    failures += 1;
                    error!(shell, "Failed to install {name}: {e}");
                }
            }
        }
    }
    failures
}

fn collect_remaining_missing(
    shell: &Shell,
    items: Vec<DoctorItem>,
) -> (usize, usize, Vec<DoctorItem>) {
    let mut remaining_missing = 0usize;
    let mut remaining_manual = 0usize;
    let mut remaining_fixable = Vec::new();

    for item in items {
        if item.status != CheckStatus::Missing {
            continue;
        }
        remaining_missing += 1;
        let fixable = item.is_fixable();
        if !fixable {
            remaining_manual += 1;
        }
        emit_item(shell, &item);
        if fixable {
            remaining_fixable.push(item);
        }
    }

    (remaining_missing, remaining_manual, remaining_fixable)
}

/// Run the doctor command against the real machine.
pub async fn run(shell: &Shell, args: Args) -> Result<()> {
    let host = Host::current();
    let diagnose = || {
        let host = host.clone();
        async move { doctor(&host).await }
    };
    run_with_diagnose(shell, args.fix, diagnose).await
}

/// The doctor orchestration with an injectable diagnosis step.
///
/// `diagnose` produces a fresh report each call — the `--fix` loop re-runs it
/// between passes so a fix that unblocks another item is observed, and a test
/// can script the sequence of reports.
async fn run_with_diagnose<F, Fut>(shell: &Shell, fix: bool, diagnose: F) -> Result<()>
where
    F: Fn() -> Fut + Sync,
    Fut: Future<Output = Vec<DoctorItem>> + Send,
{
    header!(shell, "Checking development environment...");

    let items = run_diagnostics(shell, &diagnose, "Running diagnostics...").await;
    let summary = print_diagnostics(shell, items);
    handle_doctor_result(shell, fix, &diagnose, summary).await;
    Ok(())
}

async fn run_diagnostics<F, Fut>(shell: &Shell, diagnose: &F, message: &str) -> Vec<DoctorItem>
where
    F: Fn() -> Fut + Sync,
    Fut: Future<Output = Vec<DoctorItem>> + Send,
{
    let spinner = shell.spinner(message);
    let items = diagnose().await;
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }
    items
}

fn print_diagnostics(shell: &Shell, items: Vec<DoctorItem>) -> DoctorSummary {
    let mut summary = DoctorSummary {
        all_ok: true,
        fixable_items: Vec::new(),
    };

    for item in items {
        if item.status == CheckStatus::Missing {
            summary.all_ok = false;
            if item.is_fixable() {
                emit_item(shell, &item);
                summary.fixable_items.push(item);
                continue;
            }
        }
        emit_item(shell, &item);
    }
    summary
}

fn print_skipped_item(shell: &Shell, item: &DoctorItem) {
    if let Some(msg) = &item.message {
        line!(shell, "  - {} (skipped: {})", item.name, msg);
    } else {
        line!(shell, "  - {} (skipped)", item.name);
    }
}

async fn handle_doctor_result<F, Fut>(
    shell: &Shell,
    fix: bool,
    diagnose: &F,
    summary: DoctorSummary,
) where
    F: Fn() -> Fut + Sync,
    Fut: Future<Output = Vec<DoctorItem>> + Send,
{
    line!(shell);
    if summary.all_ok {
        success!(shell, "All checks passed!");
    } else if fix {
        if summary.fixable_items.is_empty() {
            note!(
                shell,
                "Nothing to fix automatically. Please fix issues manually."
            );
        } else {
            attempt_auto_fix_loop(shell, diagnose, summary.fixable_items).await;
        }
    } else if !summary.fixable_items.is_empty() {
        warn!(
            shell,
            "Some checks failed. Run `water doctor --fix` to attempt automatic fixes for {} issue(s).",
            summary.fixable_items.len()
        );
    } else {
        warn!(shell, "Some checks failed. See above for details.");
    }
}

async fn attempt_auto_fix_loop<F, Fut>(
    shell: &Shell,
    diagnose: &F,
    mut pending_fixable: Vec<DoctorItem>,
) where
    F: Fn() -> Fut + Sync,
    Fut: Future<Output = Vec<DoctorItem>> + Send,
{
    let mut pass = 1usize;

    loop {
        print_auto_fix_header(shell, pass, pending_fixable.len());
        let failures = install_fixable_items(shell, pending_fixable).await;
        line!(shell);

        let verification_items =
            run_diagnostics(shell, diagnose, "Re-running diagnostics...").await;
        let (remaining_missing, remaining_manual, next_fixable) =
            collect_remaining_missing(shell, verification_items);

        if remaining_missing == 0 {
            success!(shell, "All detected issues were fixed.");
            break;
        }

        if failures > 0 {
            warn!(
                shell,
                "Stopping auto-fix: {failures} installation(s) failed this pass. Inspect the errors above, then re-run `water doctor --fix`."
            );
            break;
        }

        if should_stop_auto_fix(
            shell,
            pass,
            remaining_missing,
            remaining_manual,
            &next_fixable,
        ) {
            break;
        }

        pending_fixable = next_fixable;
        pass += 1;
        line!(shell);
    }
}

fn print_auto_fix_header(shell: &Shell, pass: usize, pending_count: usize) {
    if pass == 1 {
        header!(shell, "Attempting to fix {pending_count} issue(s)...");
    } else {
        header!(
            shell,
            "Attempting to fix {pending_count} additional issue(s)... (pass {pass}/{MAX_AUTO_FIX_PASSES})"
        );
    }
}

fn should_stop_auto_fix(
    shell: &Shell,
    pass: usize,
    remaining_missing: usize,
    remaining_manual: usize,
    next_fixable: &[DoctorItem],
) -> bool {
    if next_fixable.is_empty() {
        if remaining_manual > 0 {
            warn!(
                shell,
                "{remaining_missing} issue(s) remain, including {remaining_manual} issue(s) that require manual steps."
            );
            note!(
                shell,
                "Follow the [manual] next-step guidance above, then run `water doctor` again."
            );
        } else {
            warn!(
                shell,
                "{remaining_missing} fixable issue(s) still remain. Re-run `water doctor --fix` or inspect failure logs above."
            );
        }
        return true;
    }

    if pass >= MAX_AUTO_FIX_PASSES {
        warn!(
            shell,
            "{remaining_missing} issue(s) remain after {MAX_AUTO_FIX_PASSES} auto-fix pass(es)."
        );
        if remaining_manual > 0 {
            note!(
                shell,
                "Some remaining issues require manual steps. Follow the [manual] guidance above, then re-run `water doctor --fix`."
            );
        } else {
            note!(
                shell,
                "Remaining issues are still fixable. Re-run `water doctor --fix` to continue."
            );
        }
        return true;
    }

    false
}

#[cfg(test)]
mod tests {
    use std::collections::VecDeque;
    use std::pin::Pin;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};

    use color_eyre::eyre;
    use waterui_cli::toolchain::doctor::BoxedInstallFn;

    use super::*;

    /// JSON-mode shells are never interactive, so installs never prompt.
    fn test_shell() -> Shell {
        Shell::new(true)
    }

    fn ok_item(id: &'static str) -> DoctorItem {
        DoctorItem {
            id,
            name: id,
            status: CheckStatus::Ok,
            message: None,
            install_fn: None,
        }
    }

    fn manual_item(id: &'static str) -> DoctorItem {
        DoctorItem {
            id,
            name: id,
            status: CheckStatus::Missing,
            message: Some(String::from("manual steps required")),
            install_fn: None,
        }
    }

    /// A missing item whose install records its id into `calls` and yields
    /// `outcome`.
    fn fixable_item(
        id: &'static str,
        calls: &Arc<Mutex<Vec<&'static str>>>,
        outcome: Result<()>,
    ) -> DoctorItem {
        let calls = Arc::clone(calls);
        let install_fn: BoxedInstallFn = Box::new(move || {
            calls.lock().expect("install call log").push(id);
            Box::pin(async move { outcome })
        });
        DoctorItem {
            id,
            name: id,
            status: CheckStatus::Missing,
            message: Some(String::from("fixable")),
            install_fn: Some(install_fn),
        }
    }

    /// A scripted `diagnose` step: replays one report per call.
    type ScriptedDiagnose =
        Box<dyn Fn() -> Pin<Box<dyn Future<Output = Vec<DoctorItem>> + Send>> + Sync>;

    /// A diagnose closure replaying `reports` in order; each entry is one
    /// diagnosis. Calling more times than scripted fails the test, so the
    /// call count itself is the assertion on stop conditions.
    fn scripted(reports: Vec<Vec<DoctorItem>>) -> (ScriptedDiagnose, Arc<AtomicUsize>) {
        let queue = Mutex::new(VecDeque::from(reports));
        let calls = Arc::new(AtomicUsize::new(0));
        let diagnose_calls = Arc::clone(&calls);
        let diagnose = move || {
            diagnose_calls.fetch_add(1, Ordering::SeqCst);
            let next = queue
                .lock()
                .expect("diagnose script lock")
                .pop_front()
                .expect("diagnose was called more times than the script provides");
            Box::pin(async move { next }) as Pin<Box<dyn Future<Output = Vec<DoctorItem>> + Send>>
        };
        (Box::new(diagnose), calls)
    }

    fn install_log() -> Arc<Mutex<Vec<&'static str>>> {
        Arc::new(Mutex::new(Vec::new()))
    }

    fn taken(log: &Arc<Mutex<Vec<&'static str>>>) -> Vec<&'static str> {
        log.lock().expect("install call log").clone()
    }

    #[test]
    fn all_ok_reports_never_attempts_fixes() {
        let (diagnose, diagnose_calls) = scripted(vec![vec![ok_item("a"), ok_item("b")]]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn fix_loop_repairs_and_rediagnoses_once() {
        let installs = install_log();
        let (diagnose, diagnose_calls) = scripted(vec![
            vec![fixable_item("a", &installs, Ok(()))],
            vec![ok_item("a")],
        ]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(taken(&installs), vec!["a"]);
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn fix_loop_stops_after_install_failure() {
        let installs = install_log();
        let (diagnose, diagnose_calls) = scripted(vec![
            vec![fixable_item("a", &installs, Err(eyre::eyre!("boom")))],
            vec![fixable_item("a", &installs, Ok(()))],
        ]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(
            taken(&installs),
            vec!["a"],
            "a failed install must stop the loop instead of retrying"
        );
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn fix_loop_manual_issues_stop_without_installing() {
        let (diagnose, diagnose_calls) = scripted(vec![vec![manual_item("m")]]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn fix_loop_stops_when_only_manual_issues_remain() {
        let installs = install_log();
        let (diagnose, diagnose_calls) = scripted(vec![
            vec![fixable_item("a", &installs, Ok(())), manual_item("m")],
            vec![ok_item("a"), manual_item("m")],
        ]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(taken(&installs), vec!["a"]);
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn fix_loop_rediagnosis_can_surface_new_fixable_items() {
        let installs = install_log();
        let (diagnose, diagnose_calls) = scripted(vec![
            vec![fixable_item("a", &installs, Ok(())), manual_item("m")],
            vec![
                ok_item("a"),
                fixable_item("b", &installs, Ok(())),
                manual_item("m"),
            ],
            vec![ok_item("a"), ok_item("b"), manual_item("m")],
        ]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(taken(&installs), vec!["a", "b"]);
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 3);
    }

    #[test]
    fn fix_loop_caps_at_three_passes() {
        let installs = install_log();
        // Install "succeeds" but the item stays missing on re-diagnosis —
        // the loop must give up after MAX_AUTO_FIX_PASSES, not spin forever.
        let (diagnose, diagnose_calls) = scripted(vec![
            vec![fixable_item("a", &installs, Ok(()))],
            vec![fixable_item("a", &installs, Ok(()))],
            vec![fixable_item("a", &installs, Ok(()))],
            vec![fixable_item("a", &installs, Ok(()))],
        ]);
        smol::block_on(run_with_diagnose(&test_shell(), true, diagnose))
            .expect("doctor run must succeed");
        assert_eq!(taken(&installs).len(), MAX_AUTO_FIX_PASSES);
        assert_eq!(
            diagnose_calls.load(Ordering::SeqCst),
            MAX_AUTO_FIX_PASSES + 1
        );
    }

    #[test]
    fn without_fix_flag_no_installs_run() {
        let installs = install_log();
        let (diagnose, diagnose_calls) = scripted(vec![vec![fixable_item("a", &installs, Ok(()))]]);
        smol::block_on(run_with_diagnose(&test_shell(), false, diagnose))
            .expect("doctor run must succeed");
        assert!(taken(&installs).is_empty());
        assert_eq!(diagnose_calls.load(Ordering::SeqCst), 1);
    }
}